ByteScout PDF Suite - C# - Convert xml to pdf with pdf sdk - ByteScout

ByteScout PDF Suite – C# – Convert xml to pdf with pdf sdk

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF Suite – C# – Convert xml to pdf with pdf sdk

How to convert xml to pdf with pdf sdk in C# and ByteScout PDF Suite

Learn to code in C# to convert xml to pdf with pdf sdk with this step-by-step tutorial

The sample source code below will teach you how to convert xml to pdf with pdf sdk in C#. ByteScout PDF Suite is the bundle that provides six different SDK libraries to work with PDF from generating rich PDF reports to extracting data from PDF documents and converting them to HTML. This bundle includes PDF (Generator) SDK, PDF Renderer SDK, PDF Extractor SDK, PDF to HTML SDK, PDF Viewer SDK and PDF Generator SDK for Javascript and you can use it to convert xml to pdf with pdf sdk with C#.

The SDK samples given below describe how to quickly make your application do convert xml to pdf with pdf sdk in C# with the help of ByteScout PDF Suite. This C# sample code is all you need for your app. Just copy and paste the code, add references (if needs to) and you are all set! Applying C# application mostly includes various stages of the software development so even if the functionality works please test it with your data and the production environment.

If you want to try other source code samples then the free trial version of ByteScout PDF Suite is available for download from our website. Just try other source code samples for C#.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Program.cs
      
using System; using System.Collections.Generic; using System.Diagnostics; using System.Xml; using Bytescout.PDF; namespace ConvertXmlToPdfExample { /// <summary> /// This example demonstrates how to create table from some XML data. /// Since your XML file has different structure the example just shows technique of XML data reading // and PDF table creation. /// </summary> class Program { static void Main() { // Load XML document XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(@"sample.xml"); // Read columns information from XML data List<string> columns = new List<string>(); XmlNodeList columnNodeList = xmlDocument.SelectNodes("/Report/Columns/Column"); foreach (XmlNode node in columnNodeList) columns.Add(node.Attributes["Name"].Value); // Read row nodes from XML data XmlNodeList rowNodeList = xmlDocument.SelectNodes("/Report/ReportData"); // Create new PDF document Document pdfDocument = new Document(); pdfDocument.RegistrationName = "demo"; pdfDocument.RegistrationKey = "demo"; // Add page Page page = new Page(PaperFormat.A4); pdfDocument.Pages.Add(page); DeviceColor lightGrayColor = new ColorGray(200); DeviceColor whiteColor = new ColorGray(255); // Create PDF table Table table = new Table(); table.BackgroundColor = lightGrayColor; // Add columns for (int c = 0; c < columns.Count; c++) { TableColumn column = new TableColumn(columns, columns); // Set column width column.Width = (c == 0)? 100 : 60; table.Columns.Add(column); } // Add rows foreach (XmlNode rowNode in rowNodeList) { // Create new row and set its background color TableRow row = table.NewRow(); row.BackgroundColor = whiteColor; // Get cell values from XML data foreach (XmlNode childNode in rowNode.ChildNodes) { // Get cell info from XML data string columnName = childNode.Name; int columnIndex = columns.IndexOf(childNode.Name); string cellValue = childNode.InnerText; // Set cell text row[columnName].Text = cellValue; // Set cell text alignment row[columnName].TextFormat.HorizontalAlign = (columnIndex == 0) ? HorizontalAlign.Left : HorizontalAlign.Right; } // Add the row to the table table.Rows.Add(row); } // Draw the table on canvas page.Canvas.DrawTable(table, 20, 20); // Save document to file pdfDocument.Save("result.pdf"); // Cleanup pdfDocument.Dispose(); // Open result document in default associated application (for demo purpose) ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf"); processStartInfo.UseShellExecute = true; Process.Start(processStartInfo); } } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Tutorials:

prev
next