pdf links

PDF Rendering
Convert PDF to Image (.NET)
Convert PDF to image on Android (Xamarin)
Convert PDF to image on iOS (Xamarin)
Convert PDF to image in Windows Store apps (.NET)
Convert PDF to image in Windows Phone apps (.NET)
PDF to image in Universal Windows Store apps (.NET)
Free PDF Viewer control for Windows Forms (.NET)
How to integrate PDF Viewer control in WPF app (.NET)
Creating WPF PDF Viewer supporting bookmarks (.NET)
Cross-platform PDF Viewer using GTK# (MONO)
Silverlight PDF viewer control (Silverlight 5)
Multithreaded PDF rendering (.NET)
Convert pdf to image in Silverlight app (C# sample)
How to set fallback fonts for PDF rendering (C#)
Avoiding the out-of-memory exception on rendering (C#)
PDF viewer single page application (WebAPI, AngularJS)
PDF viewer control for Windows 10 universal applications
Use custom ICC profile for CMYK to RGB conversion
PDF layers - separate images, text, annotations, graphics

PDF Forms Creation PDF Security
Conversion to PDF/A
Other topics
PDF Document Manipulation
PDF Content Generation
Fixed and Flow layout document API (.NET)
Creation of grids and tables in PDF (C# sample)
How to create interactive documents using Actions (C# sample)
Text flow effects in PDF (C# sample)
How to generate ordered and bulleted lists in PDF (C# sample)
Convert HTML to PDF using flow layout API (C# sample)
How to use custom fonts for PDF generation (.NET)
Create document with differently sized pages (C#)
Create PDF documents using MONO (C#/MONO/Windows/OSX)
How to use background images for content elements (C#/PDF Kit/FlowLayout)
Add transparent images to PDF document (C#)
Draw round rect borders in PDF documents(C#)
ICC color profiles and and ICC based colors in PDF (C#)
How to use bidirectional and right to left text in PDF (C#)
Create PDF documents from XML templates (C# sample)
How to resize PDF pages and use custom stamps (C#)
Add header and footer to PDF page (.NET sample)
How to use clipping mask for drawing on PDF page
Fill graphics path with gradient brushes in PDF (Shadings)
Apitron PDF Kit and Rasterizer engine settings
Add layers to PDF page (optional content, C# sample)
How to create free text annotation with custom appearance

PDF Content Extraction
PDF Navigation

PDF to TIFF conversion
Contact us if you have a PDF related question and we'll cover it in our blog.

2015-05-26

Converting html to pdf using markup parser (C# sample)

Introduction


Sometimes, you have a pre-built HTML fragment and would like to convert it to PDF with minimal changes in its appearance. You could manually split it down to atomic elements like spans, DIVs et cetera, create corresponding pdf objects and get the job done. However, it may become a time consuming and boring task which could be easily automated using Apitron PDF Kit for .NET component. Strictly speaking, it’s not an HTML to PDF converter in its traditional meaning, but it provides you with an API to parse any custom markup (provided it’s XML-based) into pdf, and HTML is just the one of the possible markups.

Content wrapped by a tag will be converted to a flow layout content element with its Class property set to the tag’s name. You can define appropriate styles, and style these elements using class selectors. Images can be added using reserved <img> tag. Attributes link and bookmark are reserved to produce internal or external links and bookmarks. Id attribute can be used to assign an Id to the element. While it all may sound a bit complicated it’s actually pretty simple and convenient. Let’s see the code sample from the next section.

If you’d like to read more about custom markup parsing, we have a free book for you - Apitron PDF Kit in action.

Basic HTML to PDF conversion


Source HTML


Consider the following html fragment:

...
<img src='myimage.jpg' height='48px' width='48px' style='float: left;'/>
Apitron PDF Kit for .NET component can be used to convert html fragments to PDF.
<br/>
Here you can see <b>bold</b> or <i>italic</i> text elements. Next sentence contains a hyperlink.
<br/>
Read the complete description of library's capabilities on the <a href='www.apitron.com/product/pdf-kit'>product page</a>
...


It has bold and italic text, image element, and a link to other web page. All these elements will be converted to content elements defined in flow layout API subset and styled according to defined styles. You may also note the use of additional <br> element - it adds a line break.

In web browser it looks like this:

Pic. 1 HTML fragment sample pdf

Pic. 1 HTML fragment sample

Conversion implementation


The following code performs the actual conversion:

public void ConvertHtmlFragmentToPDF()
{
    // prepare resources
    ResourceManager resourceManager = new ResourceManager();   

    // create and fill document
    FlowDocument doc = new FlowDocument() { Margin = new Thickness(5, 5, 5, 5) };
   
    // register styles for different elements
    doc.StyleManager.RegisterStyle(".img", new Style(){Float = Float.Left});
    doc.StyleManager.RegisterStyle(".b",new Style()
    {Font=new Font(StandardFonts.HelveticaBold,12)});
    doc.StyleManager.RegisterStyle(".i",new Style()
    {Font=new Font(StandardFonts.HelveticaOblique,12)});
    doc.StyleManager.RegisterStyle(".a", new Style(){Color=RgbColors.Blue,
    TextDecoration = new TextDecoration(TextDecorationOptions.Underline)});   

    // parse html markup
    doc.AddItems(ContentElement.FromMarkup(
    "<img src='myimage.jpg' height='48px' width='48px'"+
    "style='float: left;'/> Apitron PDF Kit for .NET component can be used to convert html"+
    " fragments to PDF.<br/> Here you can see <b>bold</b> or <i>italic</i> text elements. "+
    "Next sentence contains a hyperlink.<br/>Read the complete description of library's"+
    " capabilities on the <a href='www.apitron.com/product/pdf-kit'>product page</a>."));

    // save document
    using (Stream stream = File.Create("ConvertHtmlFragmentToPDF.pdf"))
    {
        doc.Write(stream, resourceManager);
    }
}


Results of the conversion are as follows:

Pic. 2 Markup parsing results pdf
Pic. 2 Markup parsing results

Conclusion


This post demonstrates how to create basic html to pdf converter and how to work with markup parsing API provided by Apitron PDF Kit. Create apps using your favorite .NET programming language: C#, VB .NET or any other. This library is available for many modern platforms, and you’ll most likely find yours supported. Check out the samples included in download package and let us know if you have any questions or comments.

2 comments:

  1. Do you have an example of converting a PDF to HTML? I want to have the users upload a PDF and it will create a Web form. Is that possible? We are using your Apitron.PDF.Kit.Net_1.0.89 Thank you

    ReplyDelete
    Replies
    1. Sure, please contact our support to get sample code.

      Delete