This sample shows how to build PDF document's outline (also known as "bookmarks") with colored, bold and italic items.
Docotic.Pdf library provides methods and properties to manipulate document's outline. You can set PdfDocument.PageMode to a PdfPageMode.UseOutlines value to instruct a PDF viewer application to show "Bookmarks" panel when your document is opened.
PdfDocument.OutlineRoot property provides access to document's outline root. Value of this property is a PdfOutlineItem object. All outline items are instances of this class. Please use PdfOutlineItem methods and properties to access or setup outline items.
You can use PdfOutlineItem.AddChild method to add child items to an outline item. Second parameter for AddChild method is an index of page to associate with added child outline item.
using System.Diagnostics; using System.Drawing; namespace BitMiracle.Docotic.Pdf.Samples { public static class OutlineWithStyles { public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. PdfDocument pdf = new PdfDocument(); pdf.PageMode = PdfPageMode.UseOutlines; pdf.PageLayout = PdfPageLayout.OneColumn; BuildTestOutline(pdf); PdfOutlineItem root = pdf.OutlineRoot; for (int i = 0; i < root.ChildCount; i++) { PdfOutlineItem child = root.GetChild(i); if (i % 2 == 0) child.Bold = true; else child.Italic = true; for (int j = 0; j < child.ChildCount; j++) { PdfOutlineItem childOfChild = child.GetChild(j); if (j % 2 == 0) childOfChild.Color = new PdfRgbColor(Color.DarkGreen); else childOfChild.Color = new PdfRgbColor(Color.BlueViolet); childOfChild.Bold = true; childOfChild.Italic = true; } } string pathToFile = "OutlineWithStyles.pdf"; pdf.Save(pathToFile); pdf.Dispose(); Process.Start(pathToFile); } private static void BuildTestOutline(PdfDocument pdf) { PdfOutlineItem root = pdf.OutlineRoot; PdfOutlineItem lastParent = null; PdfFont times = pdf.AddFont("Times", false, true, false, false); double pageWidth = pdf.GetPage(0).Width; PdfPage page = pdf.GetPage(pdf.PageCount - 1); for (int i = 1; i < 30; i++) { if (i > 1) page = pdf.AddPage(); page.Canvas.Font = times; page.Canvas.FontSize = 16; string titleFormat = string.Format("Page {0}", i); double textWidth = page.Canvas.GetTextWidth(titleFormat); page.Canvas.DrawString(new PdfPoint((pageWidth - textWidth) / 2, 100), titleFormat); PdfGoToAction action = pdf.CreateGoToPageAction(i - 1, 0); if (i == 1 || i == 10 || i == 20) lastParent = root.AddChild(titleFormat, action); else lastParent.AddChild(titleFormat, action); } } } }
Imports System.Diagnostics Imports System.Drawing Imports BitMiracle.Docotic.Pdf Namespace BitMiracle.Docotic.Pdf.Samples Public NotInheritable Class OutlineWithStyles Public Shared Sub Main() ' NOTE: ' When used in trial mode, the library imposes some restrictions. ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx ' for more information. Dim pdf As New PdfDocument() pdf.PageMode = PdfPageMode.UseOutlines pdf.PageLayout = PdfPageLayout.OneColumn BuildTestOutline(pdf) Dim root As PdfOutlineItem = pdf.OutlineRoot For i As Integer = 0 To root.ChildCount - 1 Dim child As PdfOutlineItem = root.GetChild(i) If i Mod 2 = 0 Then child.Bold = True Else child.Italic = True End If For j As Integer = 0 To child.ChildCount - 1 Dim childOfChild As PdfOutlineItem = child.GetChild(j) If j Mod 2 = 0 Then childOfChild.Color = New PdfRgbColor(Color.DarkGreen) Else childOfChild.Color = New PdfRgbColor(Color.BlueViolet) End If childOfChild.Bold = True childOfChild.Italic = True Next Next Dim pathToFile As String = "OutlineWithStyles.pdf" pdf.Save(pathToFile) pdf.Dispose() Process.Start(pathToFile) End Sub Private Shared Sub BuildTestOutline(pdf As PdfDocument) Dim root As PdfOutlineItem = pdf.OutlineRoot Dim lastParent As PdfOutlineItem = Nothing Dim times As PdfFont = pdf.AddFont("Times", False, True, False, False) Dim pageWidth As Single = pdf.GetPage(0).Width Dim page As PdfPage = pdf.GetPage(pdf.PageCount - 1) For i As Integer = 1 To 29 If i > 1 Then page = pdf.AddPage() End If page.Canvas.Font = times page.Canvas.FontSize = 16 Dim titleFormat As String = String.Format("Page {0}", i) Dim textWidth As Single = page.Canvas.GetTextWidth(titleFormat) page.Canvas.DrawString(New PointF((pageWidth - textWidth) / 2, 100), titleFormat) Dim action As PdfGoToAction = pdf.CreateGoToPageAction(i - 1, 0) If i = 1 OrElse i = 10 OrElse i = 20 Then lastParent = root.AddChild(titleFormat, action) Else lastParent.AddChild(titleFormat, action) End If Next End Sub End Class End Namespace
