Bookmarks, Links and Notes
Creates a document with a bookmark tree (root and child bookmarks, a destination with a zoom level), an internal link to another page, a link to a web page, a text note and a file attachment.
C# code of this sample
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ExpertPdf.PdfCreator;
namespace ExpertPdf.PdfCreator.Samples.Pages
{
public class BookmarksDemoModel : PageModel
{
[BindProperty]
public bool ShowBookmarks { get; set; } = true;
public string Message { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
try
{
return File(CreatePdf(), "application/pdf", "Bookmarks.pdf");
}
catch (Exception ex)
{
Message = "The PDF document was not created. " + ex.Message;
return Page();
}
}
private byte[] CreatePdf()
{
// LicensingManager.LicenseKey = "your license key";
Document document = new Document();
PdfFont font = document.AddFont(new Font("Arial", 12, GraphicsUnit.Point), true);
PdfFont linkFont = document.AddFont(new Font("Arial", 12, FontStyle.Underline, GraphicsUnit.Point), true);
string imagesFolder = Path.Combine(Helper.FilesFolder, "Images");
// the first page, with a root bookmark that goes to it
PdfPage firstPage = document.AddPage();
Bookmark firstPageBookmark = document.AddBookmark("First page", new ExplicitDestination(firstPage));
AddElementResult result = firstPage.AddElement(new ImageElement(0, 0, 150, Path.Combine(imagesFolder, "box.jpg")));
// a child bookmark that goes to a point of the page: the top of the image
document.AddBookmark("First image", new ExplicitDestination(firstPage, new PointF(0, 0)), firstPageBookmark);
float y = result.EndPageBounds.Bottom + 20;
firstPage.AddElement(new ImageElement(0, y, 150, Path.Combine(imagesFolder, "box2.jpg")));
document.AddBookmark("Second image", new ExplicitDestination(firstPage, new PointF(0, y)), firstPageBookmark);
// the second page; its bookmark opens the page at a zoom of 150%
PdfPage secondPage = document.AddPage();
ExplicitDestination secondPageDestination = new ExplicitDestination(secondPage);
secondPageDestination.ZoomPercentage = 150;
Bookmark secondPageBookmark = document.AddBookmark("Second page (150% zoom)", secondPageDestination);
secondPageBookmark.Style = PdfBookmarkStyle.Bold;
secondPageBookmark.Color = Color.DarkBlue;
secondPage.AddElement(new ImageElement(0, 0, 150, Path.Combine(imagesFolder, "box3.jpg")));
// an internal link: a clickable rectangle over a text, with a destination in the document
TextElement goToText = new TextElement(250, 0, "Go to the second page", linkFont);
goToText.ForeColor = Color.Blue;
result = firstPage.AddElement(goToText);
firstPage.AddElement(new InternalLinkElement(result.EndPageBounds, new ExplicitDestination(secondPage)));
// a link to a web page
TextElement webText = new TextElement(250, 30, "Visit www.html-to-pdf.net", linkFont);
webText.ForeColor = Color.Blue;
result = firstPage.AddElement(webText);
firstPage.AddElement(new LinkUrlElement(result.EndPageBounds, "https://www.html-to-pdf.info"));
// a link back to the first page
TextElement backText = new TextElement(250, 0, "Back to the first page", linkFont);
backText.ForeColor = Color.Blue;
result = secondPage.AddElement(backText);
secondPage.AddElement(new InternalLinkElement(result.EndPageBounds, new ExplicitDestination(firstPage)));
// a text note (a sticky note icon) and a file attached to the page
TextNoteElement note = new TextNoteElement(new RectangleF(250, 60, 20, 20), "A text note: the PDF viewer shows it when you click the icon.");
note.NoteIcon = TextNoteIcon.Comment;
firstPage.AddElement(note);
firstPage.AddElement(new FileAttachmentElement(new RectangleF(280, 60, 20, 20), Path.Combine(imagesFolder, "box3.jpg")));
// a table of contents at the start: an entry for each place the bookmarks go to, with its page number and a link
TableOfContents toc = new TableOfContents();
toc.AddEntry("First page", 1, firstPage, 0);
toc.AddEntry("First image", 2, firstPage, 0);
toc.AddEntry("Second image", 2, firstPage, y);
toc.AddEntry("Second page", 1, secondPage, 0);
// page labels: the PDF viewer numbers the content pages 1, 2 and the table of contents page i (the decimal range
// moves with the content pages when the table is inserted before them)
document.AddPageLabelRange(0, PageLabelStyle.Decimal);
document.InsertTableOfContents(toc, 0);
document.AddPageLabelRange(0, PageLabelStyle.LowercaseRoman);
// a named destination: a link to Bookmarks.pdf#second-page opens the document at the second page
document.AddNamedDestination("second-page", new ExplicitDestination(secondPage));
// open the document with the bookmarks pane visible
if (ShowBookmarks)
document.ViewerPreferences.PageMode = ViewerPageMode.UseOutlines;
byte[] pdf = document.Save();
document.Close();
return pdf;
}
}
}