|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIORenderer; |
| 4 | +using Syncfusion.Pdf; |
| 5 | + |
| 6 | +// Open the HTML file as a stream. |
| 7 | +using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Sample.html"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 8 | +{ |
| 9 | + // Load the file stream into a Word document in HTML format. |
| 10 | + using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Html)) |
| 11 | + { |
| 12 | + // Instantiate DocIORenderer to handle Word to PDF conversion. |
| 13 | + DocIORenderer render = new DocIORenderer(); |
| 14 | + |
| 15 | + // Split the HTML content into sections and add line numbers where necessary. |
| 16 | + SplitSectionAndSetLineNumber(document); |
| 17 | + |
| 18 | + // Convert the modified Word document into a PDF document. |
| 19 | + PdfDocument pdfDocument = render.ConvertToPDF(document); |
| 20 | + |
| 21 | + // Create a file stream for saving the output PDF file. |
| 22 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) |
| 23 | + { |
| 24 | + // Save the generated PDF document to the output file stream. |
| 25 | + pdfDocument.Save(outputFileStream); |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// <summary> |
| 31 | +/// Splits the document content into sections at a placeholder and enables line numbering. |
| 32 | +/// </summary> |
| 33 | +/// <param name="document">The Word document to modify.</param> |
| 34 | +void SplitSectionAndSetLineNumber(WordDocument document) |
| 35 | +{ |
| 36 | + // Get the first and only section of the document. |
| 37 | + WSection section = document.Sections[0]; |
| 38 | + |
| 39 | + // Find the text range for the placeholder text ("Page2"). |
| 40 | + WTextRange placeHolder = document.Find("Page2", true, true).GetAsOneRange(); |
| 41 | + |
| 42 | + // Get the index of the paragraph containing the placeholder. |
| 43 | + int index = section.Body.ChildEntities.IndexOf(placeHolder.OwnerParagraph); |
| 44 | + |
| 45 | + // Clone the entire section to create a new section starting from the placeholder. |
| 46 | + WSection clonedSection = section.Clone(); |
| 47 | + |
| 48 | + // Remove all content before the placeholder paragraph in the cloned section. |
| 49 | + for (int i = index - 1; i >= 0; i--) |
| 50 | + clonedSection.Body.ChildEntities.RemoveAt(i); |
| 51 | + |
| 52 | + // Add the modified cloned section to the document. |
| 53 | + document.ChildEntities.Add(clonedSection); |
| 54 | + |
| 55 | + // Remove all content from the placeholder paragraph to the end in the original section. |
| 56 | + for (int i = index; i < section.Body.ChildEntities.Count;) |
| 57 | + section.Body.ChildEntities.RemoveAt(i); |
| 58 | + |
| 59 | + // Enable line numbering to restart at each section. |
| 60 | + section.PageSetup.LineNumberingMode = LineNumberingMode.RestartSection; |
| 61 | + clonedSection.PageSetup.LineNumberingMode = LineNumberingMode.RestartSection; |
| 62 | +} |
0 commit comments