We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert multiple HTML documents to a single PDF?

Platform: WinForms |
Control: PDF
Tags: pdf, htmltopdf

You can convert multiple HTML documents into a single PDF document by converting each HTML as a separate PDF and then merging them together. Sample for this workaround is provided as follows.

C#

// Create PDF Generator
PdfDocument doc = new PdfDocument();
// Add new Page
PdfPage page = doc.Pages.Add();
PdfUnitConvertor convertor = new PdfUnitConvertor();
float width = convertor.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point);
float height = -1;
using (HtmlConverter html = new HtmlConverter())
{
//Give the link where the Chart present
HtmlToPdfResult result = html.Convert("www.google.com", ImageType.Metafile, (int)width, (int)height, AspectRatio.FitPageSize);
if (result != null)
{
PdfMetafile mf = new PdfMetafile(result.RenderedImage as Metafile);
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
doc.PageSettings.Height = result.RenderedImage.Size.Height;
format.SplitTextLines = false;
format.SplitImages = false;
result.Render(page, format);
}
}
// Save PDF document
doc.Save(doc1Stream);
doc.Close(true);
doc = new PdfDocument();
page = doc.Pages.Add();
MemoryStream doc2Stream = new MemoryStream();
using (HtmlConverter html = new HtmlConverter())
{
HtmlToPdfResult result = html.Convert(htmlString, "", ImageType.Metafile, (int)width, (int)height, AspectRatio.FitPageSize);
if (result != null)
{
PdfMetafile mf = new PdfMetafile(result.RenderedImage as Metafile);
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
doc.PageSettings.Height = result.RenderedImage.Size.Height;
format.SplitTextLines = false;
format.SplitImages = false;
result.Render(page, format);
}
}
doc.Save(doc2Stream);
doc.Close(true);
//Importing 2nd PDF to 1st PDF
PdfLoadedDocument srcDoc1 = new PdfLoadedDocument(doc1Stream);
PdfLoadedDocument srcDoc2 = new PdfLoadedDocument(doc2Stream);
srcDoc1.ImportPageRange(srcDoc2, 0, srcDoc2.Pages.Count - 1);
//Save the Merged PDF
srcDoc1.Save("Sample.pdf");

Sample link

https://www.syncfusion.com/downloads/support/directtrac/general/MultipleHtmlToPdf1484926822.zip

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile