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.


How to create PDF documents from XML templates


In addition to Fixed layout API, the Apitron PDF Kit for .NET component provides a Flow layout API which supports css-like styling, and is very similar to HTML in terms of elements structure. The difference between the two is described in the following blog post.

The Flow layout API also supports XML export and import, making it possible to create XML templates for documents or PDF forms and later use them as a base, adding the necessary data or filling the PDF fields for example.

In this article we'll show how to work with XML templates and produce PDF documents using Flow layout API.

Creating the base template

Usually, we start with the template by creating it from code, so it’s the code first approach. Of course, it’s possible to create the XML template from scratch, but starting from code is a bit more convenient and speeds up the initial development.

private static void CreateTemplate()
    // create resource manager and register image resource
    ResourceManager resourceManager = new ResourceManager();
    resourceManager.RegisterResource(new Image("logo", "../../images/logo.png", true));

    // create document object and register styles
    FlowDocument doc = new FlowDocument()
          Margin = new Thickness(30, 20, 30, 20) 

    // style for image logo
    doc.StyleManager.RegisterStyle(".logo", new Style() 
          Margin = new Thickness(0, 0, 10, 10)

    // style for note below the header
    doc.StyleManager.RegisterStyle(".headerNote", new Style()
    Display = Display.InlineBlock,
    Align = Align.Justify

    // style for header
       new Style()
           Font = new Font(StandardFonts.HelveticaBold, 18),
           Display = Display.InlineBlock,
           Align = Align.Right,
           VerticalAlign = VerticalAlign.Bottom,
           Margin = new Thickness(0, 0, 0, 10)

    // style for unordered list
    doc.StyleManager.RegisterStyle(".ul", new Style()
            ListStyle = ListStyle.Unordered,
            ListMarker = ListMarker.Circle,
            ListMarkerPadding = new Thickness(0, 0, 10, 0),
            Margin = new Thickness(0, 20, 0, 0)

    //style for list items
    doc.StyleManager.RegisterStyle(".ul > *", new Style()
            ListStyle = ListStyle.ListItem,
            Margin = new Thickness(0, 10, 0, 10)
    doc.Add(new Image("logo") {Class = "logo"});

    doc.Add(new TextBlock("Sample Interview Questions for Candidates"
          Class = "header"

    doc.Add(new Br());

    doc.Add(new TextBlock(
            "To help facilitate the interview process the Human Resources Department " +
            "has compiled a list of questions that might be used during the phone " +
            "and/or on-campus interviews. " +
            "Some of the questions deal with the same content, " +
            "but are phrased differently while other questions may not pertain " +
            "to a specific discipline; however all of the questions" +
            " are unbiased and appropriate to ask. We hope you'll find " +
            "this helpful.") {Class = "headerNote"});

    // add questions list
    Section list = new Section() {Class = "ul"};

    list.Add(new TextBlock("What do you consider to be one of your greatest 
        achievements? Why?"));
    list.Add(new TextBlock("What's the latest technology innovation you're aware of?"));
    list.Add(new TextBlock("What motivates you to do your best?"));
    list.Add(new TextBlock("Why did you choose to apply to this position?"));
    list.Add(new TextBlock("What do you consider to be your particular strength(s)?"));
    list.Add(new TextBlock("What areas would you like to improve during the next two
    list.Add(new TextBlock("Why are you interested in working in the COMPANY?"));
    list.Add(new TextBlock("What do you consider to be your particular weakness(es)?"));
    list.Add(new TextBlock("What is the most exciting thing you've been working on
    list.Add(new TextBlock("Do you like to be a part of the team or prefer to work 
    list.Add(new TextBlock("Are you able to work remotely?"));
    list.Add(new TextBlock("Why should we hire you?"));
    list.Add(new TextBlock("Where do you see yourself in a two years?"));
    list.Add(new TextBlock("Why are you leaving your current job?"));
    list.Add(new TextBlock("What type of work environment do you prefer?"));


    // export as PDF
    using (Stream stream = File.Create("out.pdf"))
        doc.Write(stream, resourceManager);

    // create XML template
    using (Stream stream = File.Create("template.xml"))
        doc.SaveToXml(stream, resourceManager);

As you can see from the code sample above, we create a very simple questionnaire for an imaginable HR department. First come the company logo and header, then additional note, and lastly a list of questions. When the document is filled up, we create a PDF for previewing and corresponding XML file for later use.

The generated document looks as follows:

Pic. 1 Created PDF document

Pic. 1 Created PDF document

The resulting XML template

<?xml version="1.0" encoding="utf-8"?>
<FlowDocument xmlns="Apitron.PDF.Kit.FlowLayout.v1">
    <Image resourceId="logo" fileName="../../images/logo.png" />
    <Style selector="flowdocument">
      <Color value="Black" />
    <Style selector="grid">
      <InnerBorder thickness="1" />
      <InnerBorderColor value="Black" />
    <Style selector=".logo">
      <Margin value="0,0,10,10" />
    <Style selector=".headerNote">
      <Align value="Justify" />
      <Display value="InlineBlock" />
    <Style selector=".header">
      <Align value="Right" />
      <Display value="InlineBlock" />
      <Font resourceId="Helvetica-Bold" size="18" />
      <Margin value="0,0,0,10" />
      <VerticalAlign value="Bottom" />
    <Style selector=".ul">
      <ListMarker value="Circle" />
      <ListMarkerPadding value="0,0,10,0" />
      <ListStyle value="Unordered" />
      <Margin value="0,20,0,0" />
    <Style selector=".ul > *">
      <ListStyle value="ListItem" />
      <Margin value="0,10,0,10" />
        <Class value="logo" />
        <ResourceId value="logo" />
        <Class value="header" />
        <Text value="Sample Interview Questions for Candidates" />
    <Br />
        <Class value="headerNote" />
        <Text value="To help facilitate the interview process the Human Resources     Department has compiled a list of questions that might be used during the phone and/or on-campus interviews. Some of the questions deal with the same content, but are phrased differently while other questions may not pertain to a specific discipline; however all of the questions are unbiased and appropriate to ask. 
We hope you'll find this helpful." />
            <Text value="What do you consider to be one of your greatest 
                achievements? Why?" />


            <Text value="What type of work environment do you prefer?" />
        <Class value="ul" />
    <Margin value="30,20,30,20" />

You see that the resulting XML contains styles and values for elements’ properties as well as the path to the image resource registered in document’s resource manager. One of the benefits of having the XML template is the ability to alter documents without recompiling the app. See the next section to learn how to do it.

Changing template

Let’s design our app to accept XML templates as command line argument; in order to do it we’ll implement the entry point of the program as follows:

static void Main(string[] args)
    if (args == null || args.Length==0)

and add the template loading part:

private static void LoadTemplate(string templatePath)
    using (Stream stream = File.OpenRead(templatePath),
        outputStream = File.Create("fromTemplate.pdf"))
        ResourceManager resourceManager = new ResourceManager();

        FlowDocument doc = FlowDocument.LoadFromXml(stream, resourceManager );

        doc.Write(outputStream, resourceManager);

After that, let’s add new content to the xml template we have. Insert the following piece of XML that creates new textblock in the Elements collection of the Section element:

        <Color value="Red" />    
        <Text value="Do you have any questions for us?" />

It adds new question and makes it drawn in red color. Execute the program with command line parameter “template.xml” and new PDF file will be created for you.

The complete example can be found in our GitHub repository (link).

Resulting PDF document generated from xml template looks as follows:

Pic. 2 Modified PDF file created from XML template

Pic. 2 Modified PDF file created from XML template


Apitron PDF Kit for .NET component is a reliable PDF library that can be used to process PDF documents of any origin and manipulate their content in any desired way. Its easy to learn and well-structured API speeds up the development process and helps to achieve the desired results saving development time for other tasks.

Downloadable version of this article can be found by the following link [PDF].

No comments:

Post a Comment