Skip to content

Commit 2ac264c

Browse files
ES-872650-Add-line-number-HTML-to-PDF-conversion
1 parent 3d293e6 commit 2ac264c

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34322.80
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{081A1582-C9CA-45BB-8165-2CFD3CAC5E83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{081A1582-C9CA-45BB-8165-2CFD3CAC5E83}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{081A1582-C9CA-45BB-8165-2CFD3CAC5E83}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{081A1582-C9CA-45BB-8165-2CFD3CAC5E83}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {3904D757-E1D1-4AF0-908D-B7C731C2D459}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Add_line_number_HTML_to_PDF_conversion</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Sample.html">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<p>Page1 This is an example paragraph with some placeholder text. <strong>It includes bold formatting for emphasis.</strong></p>
2+
<p>Another sample sentence follows with italicized text for emphasis. <em>This part is highlighted.</em></p>
3+
<p>This paragraph contains random words to simulate a real sentence structure.</p>
4+
<p>Additional content goes here...</p>
5+
<p>More text can be added in this section.</p>
6+
<p>Page2 This is the beginning of a new section with more example text.</p>
7+
<p>Here is another sample paragraph with generic placeholder content.</p>
8+
<p>Text continues with additional details and structured sentences.</p>
9+
<p>More words are added here to maintain flow and readability.</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)