Click or drag to resize
Pdf Library for .NET

Multiple Urls to one Pdf Document

Select.Pdf Library for .NET can be used to convert multiple urls (or html strings) into the same pdf document. There are 2 approaches to this task:

  • Using HtmlToPdf main converter object to generate individual documents for each web page and then using the method described here to merge the the individual pdf documents into a final pdf document.

  • Using PdfHtmlElement described here, adding several objects of this type, one for each conversion, to the same PdfDocument object.

The first method has the disadvange that all individual web page conversions will appear in the final pdf starting with a new page, while using the second method, the successive conversions can be added on new pages or at the end of the previous content on the same page of the pdf document.

Sample Code

This sample code shows how to convert 3 individual urls into the same pdf document using HtmlToPdf approach.

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// convert individual urls
PdfDocument doc1 = converter.ConvertUrl(url1);
PdfDocument doc2 = converter.ConvertUrl(url2);
PdfDocument doc3 = converter.ConvertUrl(url3);

// create a new pdf document
PdfDocument doc = new PdfDocument();

// add the pages of those 3 documents to the new document
doc.Append(doc1);
doc.Append(doc2);
doc.Append(doc3);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();

// close the original pdf documents
doc1.Close();
doc2.Close();
doc3.Close();

This sample code shows how to convert 3 individual urls into the same pdf document using PdfHtmlElement approach.

// create a new pdf document 
PdfDocument doc = new PdfDocument();

// add a new page to the document
PdfPage page = doc.AddPage();

// add the first url
PdfHtmlElement html1 = new PdfHtmlElement(url1);
PdfRenderingResult result = page.Add(html1);

// get the last page of the previous conversion
PdfPage page2 = doc.Pages[result.PdfPageLastIndex];

// add the second url
PdfHtmlElement html2 = new PdfHtmlElement(result.PdfPageLastRectangle.Left, 
    result.PdfPageLastRectangle.Bottom, url2);
result = page2.Add(html2);

// get the last page of the previous conversion
PdfPage page3 = doc.Pages[result.PdfPageLastIndex];

// add the third url
PdfHtmlElement html3 = new PdfHtmlElement(result.PdfPageLastRectangle.Left, 
    result.PdfPageLastRectangle.Bottom, url3);
result = page3.Add(html3);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();
See Also