Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Add_line_number_HTML_to_PDF_conversion</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Sample.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>Page1 This is an example paragraph with some placeholder text. <strong>It includes bold formatting for emphasis.</strong></p>
<p>Another sample sentence follows with italicized text for emphasis. <em>This part is highlighted.</em></p>
<p>This paragraph contains random words to simulate a real sentence structure.</p>
<p>Additional content goes here...</p>
<p>More text can be added in this section.</p>
<p>Page2 This is the beginning of a new section with more example text.</p>
<p>Here is another sample paragraph with generic placeholder content.</p>
<p>Text continues with additional details and structured sentences.</p>
<p>More words are added here to maintain flow and readability.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -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);
}
}
}

/// <summary>
/// Splits the document content into sections at a placeholder and enables line numbering.
/// </summary>
/// <param name="document">The Word document to modify.</param>
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;
}
Loading