diff --git a/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion.sln b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion.sln new file mode 100644 index 000000000..6f1e2ea65 --- /dev/null +++ b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34322.80 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-line-number-HTML-to-PDF-conversion", "Add-line-number-HTML-to-PDF-conversion\Add-line-number-HTML-to-PDF-conversion.csproj", "{081A1582-C9CA-45BB-8165-2CFD3CAC5E83}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {081A1582-C9CA-45BB-8165-2CFD3CAC5E83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {081A1582-C9CA-45BB-8165-2CFD3CAC5E83}.Debug|Any CPU.Build.0 = Debug|Any CPU + {081A1582-C9CA-45BB-8165-2CFD3CAC5E83}.Release|Any CPU.ActiveCfg = Release|Any CPU + {081A1582-C9CA-45BB-8165-2CFD3CAC5E83}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3904D757-E1D1-4AF0-908D-B7C731C2D459} + EndGlobalSection +EndGlobal diff --git a/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Add-line-number-HTML-to-PDF-conversion.csproj b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Add-line-number-HTML-to-PDF-conversion.csproj new file mode 100644 index 000000000..95a0f4086 --- /dev/null +++ b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Add-line-number-HTML-to-PDF-conversion.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Add_line_number_HTML_to_PDF_conversion + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Data/Sample.html b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Data/Sample.html new file mode 100644 index 000000000..b40531874 --- /dev/null +++ b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Data/Sample.html @@ -0,0 +1,9 @@ +

Page1 This is an example paragraph with some placeholder text. It includes bold formatting for emphasis.

+

Another sample sentence follows with italicized text for emphasis. This part is highlighted.

+

This paragraph contains random words to simulate a real sentence structure.

+

Additional content goes here...

+

More text can be added in this section.

+

Page2 This is the beginning of a new section with more example text.

+

Here is another sample paragraph with generic placeholder content.

+

Text continues with additional details and structured sentences.

+

More words are added here to maintain flow and readability.

diff --git a/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Output/.gitkeep b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Program.cs b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Program.cs new file mode 100644 index 000000000..224a92859 --- /dev/null +++ b/HTML-conversions/Add-line-number-HTML-to-PDF-conversion/.NET/Add-line-number-HTML-to-PDF-conversion/Program.cs @@ -0,0 +1,62 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIO; +using Syncfusion.DocIORenderer; +using Syncfusion.Pdf; + +// Open the HTML file as a stream. +using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Sample.html"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) +{ + // Load the file stream into a Word document in HTML format. + using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Html)) + { + // Instantiate DocIORenderer to handle Word to PDF conversion. + DocIORenderer render = new DocIORenderer(); + + // Split the HTML content into sections and add line numbers where necessary. + SplitSectionAndSetLineNumber(document); + + // Convert the modified Word document into a PDF document. + PdfDocument pdfDocument = render.ConvertToPDF(document); + + // Create a file stream for saving the output PDF file. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + // Save the generated PDF document to the output file stream. + pdfDocument.Save(outputFileStream); + } + } +} + +/// +/// Splits the document content into sections at a placeholder and enables line numbering. +/// +/// The Word document to modify. +void SplitSectionAndSetLineNumber(WordDocument document) +{ + // Get the first and only section of the document. + WSection section = document.Sections[0]; + + // Find the text range for the placeholder text ("Page2"). + WTextRange placeHolder = document.Find("Page2", true, true).GetAsOneRange(); + + // Get the index of the paragraph containing the placeholder. + int index = section.Body.ChildEntities.IndexOf(placeHolder.OwnerParagraph); + + // Clone the entire section to create a new section starting from the placeholder. + WSection clonedSection = section.Clone(); + + // Remove all content before the placeholder paragraph in the cloned section. + for (int i = index - 1; i >= 0; i--) + clonedSection.Body.ChildEntities.RemoveAt(i); + + // Add the modified cloned section to the document. + document.ChildEntities.Add(clonedSection); + + // Remove all content from the placeholder paragraph to the end in the original section. + for (int i = index; i < section.Body.ChildEntities.Count;) + section.Body.ChildEntities.RemoveAt(i); + + // Enable line numbering to restart at each section. + section.PageSetup.LineNumberingMode = LineNumberingMode.RestartSection; + clonedSection.PageSetup.LineNumberingMode = LineNumberingMode.RestartSection; +}