|
| 1 | +// See https://aka.ms/new-console-template for more information |
| 2 | +using Syncfusion.Drawing; |
| 3 | +using Syncfusion.Pdf; |
| 4 | +using Syncfusion.Pdf.Graphics; |
| 5 | +using Syncfusion.Pdf.Interactive; |
| 6 | + |
| 7 | + |
| 8 | +using (PdfDocument document = new PdfDocument()) |
| 9 | +{ |
| 10 | + //Create the font to add the TOC title |
| 11 | + PdfStandardFont titleFont = new PdfStandardFont(PdfFontFamily.Helvetica, 20); |
| 12 | + |
| 13 | + //Add a section to create TOC |
| 14 | + PdfSection tocSection = document.Sections.Add(); |
| 15 | + |
| 16 | + //Add a page for creating TOC |
| 17 | + PdfPage tocPage = tocSection.Pages.Add(); |
| 18 | + |
| 19 | + tocPage.Graphics.DrawString("Table Of Contents", titleFont, PdfBrushes.Black, |
| 20 | + new RectangleF(0, 0, tocPage.GetClientSize().Width, tocPage.GetClientSize().Height), new PdfStringFormat(PdfTextAlignment.Center)); |
| 21 | + |
| 22 | + //Add a separate section to draw the content |
| 23 | + PdfSection contentSection = document.Sections.Add(); |
| 24 | + |
| 25 | + //Add page using the content section |
| 26 | + PdfPage contentPage = contentSection.Pages.Add(); |
| 27 | + |
| 28 | + //Create necessary fonts |
| 29 | + PdfStandardFont paragraphFont = new PdfStandardFont(titleFont, 10); |
| 30 | + PdfStandardFont chapterFont = new PdfStandardFont(paragraphFont, 18, PdfFontStyle.Bold); |
| 31 | + PdfStandardFont sectionFont = new PdfStandardFont(paragraphFont, 14, PdfFontStyle.Bold); |
| 32 | + PdfStandardFont paragraphFontBold = new PdfStandardFont(paragraphFont, 10, PdfFontStyle.Bold); |
| 33 | + |
| 34 | + PdfLayoutResult result = null; |
| 35 | + |
| 36 | + //Get the page size |
| 37 | + SizeF pageSize = contentPage.GetClientSize(); |
| 38 | + |
| 39 | + //initialize the TOC start height |
| 40 | + float y = 40; |
| 41 | + |
| 42 | + //Sample text to draw the paragraph |
| 43 | + string paragraphText = "Adobe Systems Incorporated's Portable Document Format (PDF) is the de facto standard for the accurate, reliable, and platform-independent representation of a paged document. It's the only universally accepted file format that allows pixel-perfect layouts. In addition, PDF supports user interaction and collaborative workflows that are not possible with printed documents."; |
| 44 | + |
| 45 | + for (int i = 1; i <= 3; i++) |
| 46 | + { |
| 47 | + RectangleF bounds = new RectangleF(0, 0, pageSize.Width, pageSize.Height); |
| 48 | + |
| 49 | + if (result != null) |
| 50 | + { |
| 51 | + bounds = new RectangleF(0, result.Bounds.Bottom + 10, pageSize.Width, pageSize.Height - result.Bounds.Bottom + 10); |
| 52 | + } |
| 53 | + |
| 54 | + //Draw TOC |
| 55 | + y = DrawTableOfContent(tocPage, result == null ? contentPage : result.Page, document, "Chapter " + i, paragraphFontBold, bounds.Location, 0, y); |
| 56 | + |
| 57 | + //Draw content |
| 58 | + result = AddParagraph(result == null ? contentPage : result.Page, "Chapter " + i, bounds, chapterFont); |
| 59 | + |
| 60 | + for (int j = 1; j <= 3; j++) |
| 61 | + { |
| 62 | + y = DrawTableOfContent(tocPage, result.Page, document, "Section " + i + "." + j, paragraphFontBold, new PointF(0, result.Bounds.Bottom + 10), 10, y); |
| 63 | + result = AddParagraph(result.Page, "Section " + i + "." + j, new RectangleF(0, result.Bounds.Bottom + 10, pageSize.Width, pageSize.Height - result.Bounds.Bottom + 10), sectionFont); |
| 64 | + |
| 65 | + for (int k = 1; k <= 3; k++) |
| 66 | + { |
| 67 | + y = DrawTableOfContent(tocPage, result.Page, document, "Paragraph " + i + "." + j + "." + k, paragraphFont, new PointF(0, result.Bounds.Bottom + 10), 20, y); |
| 68 | + result = AddParagraph(result.Page, "Paragraph " + i + "." + j + "." + k, new RectangleF(0, result.Bounds.Bottom + 10, pageSize.Width, pageSize.Height - result.Bounds.Bottom + 10), paragraphFontBold); |
| 69 | + result = AddParagraph(result.Page, paragraphText, new RectangleF(0, result.Bounds.Bottom + 10, pageSize.Width, pageSize.Height - result.Bounds.Bottom + 10), paragraphFont); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Save the PDF document |
| 75 | + using (FileStream outputStream = new FileStream("create-table-of-content.pdf", FileMode.Create, FileAccess.Write)) |
| 76 | + { |
| 77 | + document.Save(outputStream); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +PdfLayoutResult AddParagraph( |
| 82 | + PdfPage page, string text, RectangleF bounds, PdfFont font) |
| 83 | +{ |
| 84 | + // Create the text element |
| 85 | + PdfTextElement textElement = new PdfTextElement(text, font); |
| 86 | + |
| 87 | + PdfLayoutFormat format = new PdfLayoutFormat(); |
| 88 | + |
| 89 | + format.Layout = PdfLayoutType.Paginate; |
| 90 | + |
| 91 | + format.PaginateBounds = new RectangleF (0,0, page.GetClientSize().Width, page.GetClientSize().Height); |
| 92 | + |
| 93 | + // Draw the text on the page |
| 94 | + return textElement.Draw(page, bounds, format); |
| 95 | +} |
| 96 | + |
| 97 | +float DrawTableOfContent(PdfPage tocPage, PdfPage destPage, PdfDocument document, string text, PdfFont font, PointF location, int indent, float y) |
| 98 | +{ |
| 99 | + float textSize = font.MeasureString(text).Width; |
| 100 | + float pageWidth = tocPage.GetClientSize().Width; |
| 101 | + float dotSize = pageWidth - (30 + textSize + indent); |
| 102 | + |
| 103 | + |
| 104 | + string textToDraw = text; |
| 105 | + float dotWidth = font.MeasureString(".").Width; |
| 106 | + float n = 0; |
| 107 | + while (n < dotSize) |
| 108 | + { |
| 109 | + textToDraw += "."; |
| 110 | + n += dotWidth; // Dot spacing |
| 111 | + } |
| 112 | + textToDraw += document.Pages.IndexOf(destPage).ToString(); |
| 113 | + // Draw the page number on the page |
| 114 | + tocPage.Graphics.DrawString(textToDraw, font, PdfBrushes.Black, new PointF(indent, y)); |
| 115 | + |
| 116 | + // Create a link annotation |
| 117 | + RectangleF annotationBounds = new RectangleF(indent, y, pageWidth - (25 +indent), font.Height); |
| 118 | + |
| 119 | + PdfDocumentLinkAnnotation annotation = new PdfDocumentLinkAnnotation( |
| 120 | + annotationBounds, |
| 121 | + new PdfDestination(destPage, location)) |
| 122 | + { |
| 123 | + Border = { Width = 0 } |
| 124 | + }; |
| 125 | + tocPage.Annotations.Add(annotation); |
| 126 | + |
| 127 | + return y += font.Height + 2; |
| 128 | +} |
| 129 | + |
0 commit comments