EvoPDF – Merging PDFs, Editing existing PDF, and Giving internal link

You may have faced several scenarios where you need a functionality to be develop in which you need to convert html – a web page into PDF.

There are several third party dlls available for conversion from html to pdf in Asp.Net. Among those you can consider EvoPDF/Winnovative as one of the best option having large variety of functionality available. Click here for more information on EvoPDF.

You can see from the link provided above for EvoPDF, they have covered many functionalities with example and sample code. still it’s a large globe and we may find some situation for which we may need to struggle a little bit. I am sharing here few functionality which i have encountered recently and at the end everything working like a charm.

Merging PDFs

Here, we will see basic functionality to create PDF from html and merging it with some existing PDF available to generate a new merged PDF.

You needed to import any of below libraries to begin with:


using Winnovative;
Or
using EvoPdf.HtmlToPdf;

Document MergeResultPdfDocument = new Document();
Document ExistingPdfDocument = new Document();
Document HtmlToPDFDocument = new Document(); 

//Reading Existing file in Document object
Byte[] ExistingPDF = File.ReadAllBytes(fileName);
using (Stream Stream = new MemoryStream(ExistingPDF))
{
   ExistingPdfDocument = new Document(loStream);
   //Adding existing PDF file to Merged PDF 
   MergeResultPdfDocument.AppendDocument(ExistingPdfDocument);
} 

//Convert HtmlToPdf and Merge PDF
PdfConverter Converter = new PdfConverter();
Converter.LicenseKey = LICENSEKEY;
HtmlToPDFDocument = Converter.GetPdfDocumentObjectFromUrl("URL to convert");
MergeResultPdfDocument.AppendDocument(HtmlToPDFDocument); 

Generate PDF from Html, Edit Runtime, and Give InternalLink

This is an interesting task. Where you want to generate a PDF from Html and wanted to edit it using code based on some conditions before generating PDF. We will see here how in two steps we can achieve this.

For ex: create a PDF Document from URL (as shown above) having below html:

<table>
<tbody>
<tr>
<td>
              <span data-mapping-enabled="true" style="font-size: 18px; font-weight: bold; color: navy">
			#INDEX1#
              </span></td>
</tr>
<tr>
<td>
              <span data-mapping-enabled="true" style="font-size: 18px; font-weight: bold; color: navy">
			#INDEX2#
              </span></td>
</tr>
<tr>
<td>
              <span data-mapping-enabled="true" style="font-size: 18px; font-weight: bold; color: navy">
			#INDEX3#
              </span></td>
</tr>
</tbody>
</table>

Here, data-mapping-enabled=”true” is very important attribute, using which we can extract those element from PDF and we can update it.

Now, edit the response as per requirement and generate PDF from the final response.

string response = GetResponse("URL to convert");
//Make required changes in response text. We will update here #INDEX# with proper title.

For(int i=0; i<3; i++) // As we have three records in html. this can be any number dynamically.
{
   response = response.replace("#Index" + (i + 1) + "#", Title[i]);
}

//After editing response, create the PDF from final response
PdfConverter htmlToPdfConverter = new PdfConverter();
htmlToPdfConverter.LicenseKey = LICENSEKEY;

Document IndexPdfDocument = htmlToPdfConverter.GetPdfDocumentObjectFromHtmlString(response);

After generating PDF, now we will give internal link to some page to the title. So, we can create a functionality similar to the index page. where we can click on index title to navigate to that page.


//get all html elements which are marked using <strong>data-mapping-enabled="true"</strong>

foreach (HtmlElementMapping htmlElementInfo in htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult)
                {
                    HtmlElementPdfRectangle[] htmlElementRectanglesInPdf = htmlElementInfo.PdfRectangles;
                    for (int i = 0; i < htmlElementRectanglesInPdf.Length; i++)
                    {
                        int pdfPageIndex = htmlElementRectanglesInPdf[i].PageIndex;
                        RectangleF rectangleInPdfPage = htmlElementRectanglesInPdf[i].Rectangle;

                        //add a rectangle of height 10px and with 1000px at given title to create link
                        RectangleF linkRectangle = new RectangleF(0, rectangleInPdfPage.Y, 1000, 10);

                        //Create explicit destination object to the page number you want to link
                        ExplicitDestination PageDestination = new ExplicitDestination(MergeResultPdfDocument.Pages[pageindex[count]], new PointF(0, 0));

                        //Create Internal Link element and add this element to index page
                        InternalLinkElement internalLink = new InternalLinkElement(linkRectangle, PageDestination);
                       
        IndexPdfDocument.Pages[pdfPageIndex].AddElement(internalLink);
                    }
                    count++;
                }

MergeResultPdfDocument.InsertDocument(0, IndexPdfDocument);

This is very important section of code where you identify the html elements from existing PDF, Creates a rectangle at place of html element, and insert an internal link element.

That’s it, And Wish you all a very happy new year 2016.