PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

How To Write Data From XML to PDF Files

PDFOne .NET offers a very simple method to read XML data and render it in PDF files. Using it, you can convert XML data to PDF easily.
By Shijin C. A. and M. V. Niranjan

XML is a great way for enabling data interchange. PDF offers a great way for presenting data.

That brings us to the point of how to take data from XML and present it in PDF. PDFOne .NET provides a very simple method to read XML data and render it in PDF files.

In this article, you will see a project that converts XML data to PDF.

Specifically, PDFOne .NET supports place holders in text strings that are written to PDF documents. Users can define their own place holders and then supply the values from XML through an event.

This project supplies data stored in XML nodes through place holders. The main method Button1_Click renders the PDF document. The GetXmlData method supplies the value for the place holders.

GetXmlData gets data through XmlTextReader in case of Microsoft Visual Studio 2005®. (Visual Studio 2005 has all the methods necessary for reading values of each node.)

using Gnostice.PDFOne;
using System.IO;
using System.Xml;
// Creates a string containing XML-formatted data
static string xmlString =
    "<?xml version='1.0' encoding='UTF-8'?>" +
    "<myfile>" +
    "<record><name>Rita</name><age>23</age>" +
    "<gender>female</gender></record>" +
    "<record><name>Bob</name><age>25</age>" +
    "<gender>male</gender></record>" +
    "<record><name>Sam</name><age>32</age>" +
    "<gender>male</gender></record>" +
    "<record><name>Julie</name><age>20</age>" +
    "<gender>female</gender></record>" +
    "</myfile>";
// Creates an xml text reader objct for reading the
// xml input data
XmlTextReader textReader =
    new XmlTextReader(new StringReader(xmlString));
private void Button1_Click(object sender, EventArgs e)
{
  PDFDocument doc = new PDFDocument();
  // Fills the variables with data (performed
  // by the delegate under this method)
  doc.ReplaceVariable +=
       new ReplaceVariableEventHandler(GetXMLData);
  ...
  for (i = 0; i < noOfRecords; i++, yLocation += 0.4f)
  {
    doc.WriteText("Name", 1, yLocation);
    // Replaces placeholder <%name%> with value read from
    // the XML string
    doc.WriteText("<%name%>", 2, yLocation);
    yLocation += 0.2f;
    doc.WriteText("Age", 1, yLocation);
    // Replaces placeholder <%age%> with value read from
    // the XML string
    doc.WriteText("<%age%>", 2, yLocation);
    yLocation += 0.2f;
    doc.WriteText("Gender", 1, yLocation);
    // Replaces placeholder <%gender%> with value read from
    // the XML string
    doc.WriteText("<%gender%>", 2, yLocation);
  }
  ...
}
void GetXMLData(object sender,
                string variable,
                ref string newValue,
                ref bool replace)
{
  textReader.ReadToFollowing(variable);
  newValue = textReader.ReadString();
}
---o0O0o---

Downloads:

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.