The code below will help you to implement an C# app to convert XML to PDF. ByteScout PDF SDK is the SDK for pdf documents generation, modification and updates. Can also generate and fill PDF forms. Provides support for text (fonts, style, size, font family), layers, pdf form fields, vector and raster drawings and you can use it to convert XML to PDF with C#.
This code snippet below for ByteScout PDF SDK works best when you need to quickly convert XML to PDF in your C# application. In order to implement the functionality, you should copy and paste this code for C# below into your code editor with your app, compile and run your application. Implementing C# application typically includes multiple stages of the software development so even if the functionality works please test it with your data and the production environment.
Free trial version of ByteScout PDF SDK is available on our website. Documentation and source code samples are included.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
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);
}
}
}
60 Day Free Trial or Visit ByteScout PDF SDK Home Page
Explore ByteScout PDF SDK Documentation
Explore Samples
Sign Up for ByteScout PDF SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
60 Day Free Trial or Visit ByteScout PDF SDK Home Page
Explore ByteScout PDF SDK Documentation
Explore Samples
Sign Up for ByteScout PDF SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
also available as: