These sample source codes on this page below are displaying how to convert xml to pdf with pdf sdk in C#. ByteScout Premium Suite is the bundle that includes twelve SDK products from ByteScout including tools and components for PDF, barcodes, spreadsheets, screen video recording and you can use it to convert xml to pdf with pdf sdk with C#.
Want to quickly learn? This fast application programming interfaces of ByteScout Premium Suite for C# plus the guidelines and the code below will help you quickly learn how to convert xml to pdf with pdf sdk. Just copy and paste the code into your C# application’s code and follow the instructions. Use of ByteScout Premium Suite in C# is also described in the documentation included along with the product.
Trial version of ByteScout Premium Suite is available for free. Source code samples are included to help you with your C# app.
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 Premium Suite Home Page
Explore ByteScout Premium Suite Documentation
Explore Samples
Sign Up for ByteScout Premium Suite Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
60 Day Free Trial or Visit ByteScout Premium Suite Home Page
Explore ByteScout Premium Suite Documentation
Explore Samples
Sign Up for ByteScout Premium Suite Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
also available as: