Home > Code > C# > Split CSS File into Multiple Pages When Converting PDF to HTML in .NET Apps

Split CSS File into Multiple Pages When Converting PDF to HTML in .NET Apps

by sher azam   on Jan 16, 2014   Category: C#   |  Views: 1383    |  Points: 25   |  Gold 


This technical tip shows how .NET Developers can split CSS into multiple pages when converting PDF to HTML. When converting PDF files to HTML format, a CSS file containing formatting information is created. Aspose.Pdf for .NET also offers the feature to split the resultant HTML to pages; so you can also split the CSS into multiple pages. HtmlSaveOptions class has property named SplitIntoPages which supports the feature split resultant HTML file to pages when generating the output. Recently one of the customers has the requirement to split the CSS file based on individual page instead of generating a single CSS file. In order to accomplish this requirement, we have introduced a new flag SplitCssIntoPages in HtmlSaveOptions class. When the value of this property is set to true, the converter will split resultant CSS into parts/pages based on individual HTML page created. Please take a look over following code snippet.

The following code snippet shows how to Split CSS File Based on Individual Page When Converting PDF to HTML

//[C#]

//1) clean-up target folders
string htmlFile = Path.GetFullPath("c:/pdftest/ResultantFile_files/" + "resultant.html");
string imagesDir = Path.GetDirectoryName(htmlFile) + @"\35942_files";
string cssDir = Path.GetDirectoryName(htmlFile) + @"\35942_css_files";
if (Directory.Exists(imagesDir)) { Directory.Delete(imagesDir, true); };
if (Directory.Exists(cssDir)) { Directory.Delete(cssDir, true); };

//2) create document for conversion
Document pdfDocument = new Document("c:/pdftest/antrag.pdf");

//3) tune conversion options
HtmlSaveOptions options = new HtmlSaveOptions();
options.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsPngImagesEmbeddedIntoSvg;//<- to get compatibility with previous behavior and therefore same result of tests
// split HTML output into pages
options.SplitIntoPages = true;
// split css into pages
options.SplitCssIntoPages = true;
options.CustomCssSavingStrategy = new HtmlSaveOptions.CssSavingStrategy(Strategy_4_CSS_MULTIPAGE_SAVING_RIGHT_WAY);
options.CustomStrategyOfCssUrlCreation = new HtmlSaveOptions.CssUrlMakingStrategy(Strategy_5_CSS_MAKING_CUSTOM_URL_FOR_MULTIPAGING);
//3) do conversion
pdfDocument.Save(htmlFile, options);

private static void Strategy_4_CSS_MULTIPAGE_SAVING_RIGHT_WAY(HtmlSaveOptions.CssSavingInfo partSavingInfo)
{
string cssOutFolder = @"c:\pdftest\ResultantFile_files\";
if (!Directory.Exists(cssOutFolder))
{
Directory.CreateDirectory(cssOutFolder);
}

string outPath = cssOutFolder + "style_xyz_page" + partSavingInfo.HtmlPageNumber.ToString() + ".css";
System.IO.BinaryReader reader = new BinaryReader(partSavingInfo.ContentStream);
System.IO.File.WriteAllBytes(outPath, reader.ReadBytes((int)partSavingInfo.ContentStream.Length));
}

private static string Strategy_5_CSS_MAKING_CUSTOM_URL_FOR_MULTIPAGING(HtmlSaveOptions.CssUrlRequestInfo requestInfo)
{
return "/document-viewer/GetCss?cssId=4544554445_page{0}";
}

//[VB.NET]

'1) clean-up target folders
Dim htmlFile As String = Path.GetFullPath("c:/pdftest/" + "resultant.html")
Dim imagesDir As String = Path.GetDirectoryName(htmlFile) + "\35942_files"
Dim cssDir As String = Path.GetDirectoryName(htmlFile) + "\35942_css_files"
If (Directory.Exists(imagesDir)) Then
Directory.Delete(imagesDir, True)
End If
If (Directory.Exists(cssDir)) Then
Directory.Delete(cssDir, True)
End If

'2) create document for conversion
Dim pdfDocument As Document = New Document("c:/pdftest/demo.pdf")

'3) tune conversion options
Dim options As HtmlSaveOptions = New HtmlSaveOptions()
options.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsPngImagesEmbeddedIntoSvg '<- to get compatibility with previous behavior and therefore same result of tests
' split HTML output into pages
options.SplitIntoPages = True
' split css into pages
options.SplitCssIntoPages = True
options.CustomCssSavingStrategy = New HtmlSaveOptions.CssSavingStrategy(AddressOf Strategy_4_CSS_MULTIPAGE_SAVING_RIGHT_WAY)
options.CustomStrategyOfCssUrlCreation = New HtmlSaveOptions.CssUrlMakingStrategy(AddressOf Strategy_5_CSS_MAKING_CUSTOM_URL_FOR_MULTIPAGING)
'3) do conversion
pdfDocument.Save(htmlFile, options)

Private Sub Strategy_4_CSS_MULTIPAGE_SAVING_RIGHT_WAY(partSavingInfo As HtmlSaveOptions.CssSavingInfo)

' folder to save CSS files
Dim cssOutFolder As String = "C:\pdftest\Sample_ResultantFile_files\"
If Not Directory.Exists(cssOutFolder) Then
Directory.CreateDirectory(cssOutFolder)
End If

Dim outPath As String = cssOutFolder + "style_xyz_page" + partSavingInfo.HtmlPageNumber.ToString() + ".css"
Dim reader As System.IO.BinaryReader = New BinaryReader(partSavingInfo.ContentStream)
System.IO.File.WriteAllBytes(outPath, reader.ReadBytes(partSavingInfo.ContentStream.Length))

End Sub
Private Function Strategy_5_CSS_MAKING_CUSTOM_URL_FOR_MULTIPAGING(requestInfo As HtmlSaveOptions.CssUrlRequestInfo) As String

Return "/document-viewer/GetCss?cssId=4544554445_page{0}"


End Function

More about Aspose.Pdf for .NET

- Homepage of Aspose.Pdf for .NET: http://www.aspose.com/.net/pdf-component.aspx

- Download Aspose.Pdf for .NET at: http://www.aspose.com/community/files/51/.net-components/aspose.pdf-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts