Generate PDF from XML
Generating a PDF document from an XML document is not a trivial task because XML document can describes different content Aspose.PDF for Java has several ways to genereate PDF based on XML document:
- using XSLT transformation
- using XSL-FO (XSL Formatting Objects) markup
- using own Aspose.PDF XML Schema
Generating PDF document using XSLT transformation
XSL (eXtensible Stylesheet Language) is a styling language for transforming XML documents into other XML documents or HTMLs. In our case we can use XML to HTML transformation and then create PDF based on HTML data.
Assume we have an XML file with simple CD catalog (see below).
<?xml version="1.0" encoding="utf-8" ?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>One night only</title>
<artist>Bee Gees</artist>
<country>UK</country>
<company>Polydor</company>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
<company>Pickwick</company>
<price>8.50</price>
<year>1990</year>
</cd>
<cd>
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
<cd>
<title>When a man loves a woman</title>
<artist>Percy Sledge</artist>
<country>USA</country>
<company>Atlantic</company>
<price>8.70</price>
<year>1987</year>
</cd>
<cd>
<title>Black angel</title>
<artist>Savage Rose</artist>
<country>EU</country>
<company>Mega</company>
<price>10.90</price>
<year>1995</year>
</cd>
<cd>
<title>1999 Grammy Nominees</title>
<artist>Many</artist>
<country>USA</country>
<company>Grammy</company>
<price>10.20</price>
<year>1999</year>
</cd>
<cd>
<title>For the good times</title>
<artist>Kenny Rogers</artist>
<country>UK</country>
<company>Mucik Master</company>
<price>8.70</price>
<year>1995</year>
</cd>
<cd>
<title>Big Willie style</title>
<artist>Will Smith</artist>
<country>USA</country>
<company>Columbia</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>Tupelo Honey</title>
<artist>Van Morrison</artist>
<country>UK</country>
<company>Polydor</company>
<price>8.20</price>
<year>1971</year>
</cd>
<cd>
<title>Soulsville</title>
<artist>Jorn Hoel</artist>
<country>Norway</country>
<company>WEA</company>
<price>7.90</price>
<year>1996</year>
</cd>
<cd>
<title>The very best of</title>
<artist>Cat Stevens</artist>
<country>UK</country>
<company>Island</company>
<price>8.90</price>
<year>1990</year>
</cd>
<cd>
<title>Stop</title>
<artist>Sam Brown</artist>
<country>UK</country>
<company>A and M</company>
<price>8.90</price>
<year>1988</year>
</cd>
<cd>
<title>Bridge of Spies</title>
<artist>T`Pau</artist>
<country>UK</country>
<company>Siren</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Private Dancer</title>
<artist>Tina Turner</artist>
<country>UK</country>
<company>Capitol</company>
<price>8.90</price>
<year>1983</year>
</cd>
<cd>
<title>Midt om natten</title>
<artist>Kim Larsen</artist>
<country>EU</country>
<company>Medley</company>
<price>7.80</price>
<year>1983</year>
</cd>
<cd>
<title>Pavarotti Gala Concert</title>
<artist>Luciano Pavarotti</artist>
<country>UK</country>
<company>DECCA</company>
<price>9.90</price>
<year>1991</year>
</cd>
<cd>
<title>The dock of the bay</title>
<artist>Otis Redding</artist>
<country>USA</country>
<company>Stax Records</company>
<price>7.90</price>
<year>1968</year>
</cd>
<cd>
<title>Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
<company>Elektra</company>
<price>7.20</price>
<year>1985</year>
</cd>
<cd>
<title>Red</title>
<artist>The Communards</artist>
<country>UK</country>
<company>London</company>
<price>7.80</price>
<year>1987</year>
</cd>
<cd>
<title>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>
To convert this file to PDF we should create an XSL with HTML layout. Let’s render our data in table. The XSL file that will help us do this might look something like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
So, we need to transform XML and load into PDF document. The following example shows this way:
package com.aspose.pdf.examples;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class WorkingWithXML {
private static String _dataDir = "/home/admin1/pdf-examples/Samples/";
public static void ExampleXSLTtoPDF() throws TransformerException {
String xslFile = _dataDir + "XMLFile1.xml", xmlFile = _dataDir + "XSLTFile1.xslt";
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer =
factory.newTransformer( new StreamSource( xslFile ) );
StreamSource xmlsource = new StreamSource( xmlFile );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult output = new StreamResult( baos );
transformer.transform( xmlsource, output );
com.aspose.pdf.HtmlLoadOptions options = new com.aspose.pdf.HtmlLoadOptions();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(bais, options);
pdfDocument.save(_dataDir + "data_xml.pdf");
}
}
Generating PDF document using XSL-FO markup
XSL-FO is an XML-based markup language describing the formatting of XML data for output to screen, paper, or other media. Aspose.PDF has a special class which allows to applying XSL-FO markup and getting PDF document.
Let’s take an example. Here is an XML file with sample data of employees.
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<companyname>ABC Inc.</companyname>
<employee>
<id>101</id>
<name>Andrew</name>
<designation>Manager</designation>
</employee>
<employee>
<id>102</id>
<name>Eduard</name>
<designation>Executive</designation>
</employee>
<employee>
<id>103</id>
<name>Peter</name>
<designation>Executive</designation>
</employee>
</employees>
Let’s create yet another file - the XSL-FO markup file to transform employees’ data into the table.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
<xsl:template match="employees">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simpleA4">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="16pt" font-weight="bold" space-after="5mm">
Company Name: <xsl:value-of select="companyname"/>
</fo:block>
<fo:block font-size="10pt">
<fo:table table-layout="fixed" width="100%" border-collapse="separate">
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="5cm"/>
<fo:table-body>
<xsl:apply-templates select="employee"/>
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="employee">
<fo:table-row>
<xsl:if test="designation = 'Manager'">
<xsl:attribute name="font-weight">bold</xsl:attribute>
</xsl:if>
<fo:table-cell>
<fo:block>
<xsl:value-of select="id"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="name"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="designation"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
</xsl:stylesheet>
Aspose.PDF has a special XslFoLoadOptions class that allows to apply XSL-FO tranformation. The following snippet shows how to use this class with the sample files described above.
public static void Example_XSLFO_to_PDF() {
// Instantiate XslFoLoadOption object
com.aspose.pdf.XslFoLoadOptions options = new com.aspose.pdf.XslFoLoadOptions(_dataDir+"employees.xslt");
// Create Document object
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(_dataDir+"employees.xml", options);
pdfDocument.save(_dataDir + "data_xml.pdf");
}
Generating PDF document based on Aspose.PDF XML Schema
Another way to create a PDF document from XML is using the Aspose.PDF XML Schema. Using this diagram, you can describe the page layout in the same way as if you were using a table layout in HTML. Let’s consider the work of this method in more detail.
Defining page
Let’s define the page with default parameters. Our page will have an A4 page size and contain only one piece of text.
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
<Page>
<TextFragment>
<TextSegment>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla odio lorem, luctus in lorem vitae, accumsan semper lectus. Cras a auctor leo, et tincidunt lacus.</TextSegment>
</TextFragment>
</Page>
</Document>
To generate PDF document we will use bindXml method.
public static void Example_XML_to_PDF() {
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document();
pdfDocument.bindXml(_dataDir + "aspose_pdf_demo.xml");
pdfDocument.save(_dataDir + "data_xml.pdf");
}
To define a new page size we should add a PageInfo
element. In the following example, we were set A5 page size and margins 25mm and 10mm.
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
<Page>
<PageInfo IsLandscape="true" Height="595" Width="420">
<Margin Top="70.8661" Bottom="70.8661" Left="28.3465" Right="28.3465" />
</PageInfo>
<TextFragment>
<TextSegment>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla odio lorem, luctus in lorem vitae, accumsan semper lectus. Cras a auctor leo, et tincidunt lacus.</TextSegment>
</TextFragment>
</Page>
</Document>
Adding HtmlFragment element in XML file
As HTML contains tags similar to XML, so when you write HTML inside any XML tag, the parser treats it as XML markups and they simply cannot be recognized as XML tags. The issue can be overcome by using “CDATA” Section in XML. The CDATA Section contains text which is not parsed by the parser or in other words, it is not treated as XML markups. Following sample XML template shows how to add HtmlFragment inside XML markups by using CDATA.
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
<Page id="mainSection">
<HtmlFragment>
<![CDATA[
<font style="font-family:Tahoma; font-size:40px;">This is Html String.</font>
]]>
</HtmlFragment>
</Page>
</Document>
Adding Table element in XML file
The elements Table
, Row
, Cell
are used to describe tables. The following snippet shows using a simple table. In this example some cells have Alignment
attribute and this attribute has numeric value:
- Left alignment
- Center alignment
- Right alightmnet.
- Justify alignment. Text will be aligned on both left and right margins.
- Full justify. Similar to ‘Justify’ alignment, except that the very last line will only be left-aligned in ‘Justify’ mode, while in ‘FullJustify’ mode all lines will be left- and right-aligned.
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
<Page>
<PageInfo IsLandscape="false" Height="595" Width="420">
<Margin Top="71" Bottom="71" Left="28" Right="28" />
</PageInfo>
<HtmlFragment>
<![CDATA[
<h1 style="font-family:Tahoma; font-size:16pt;">TIMETABLES ON GREENTOWN-BLUEBERG ROUTE</h1>
]]>
</HtmlFragment>
<TextFragment>
<TextSegment>4.1.-28.3.2021 | GREENTOWN → BLUEBERG</TextSegment>
</TextFragment>
<Table ColumnAdjustment="AutoFitToWindow" ColumnWidths ="10 10 10 10">
<DefaultCellPadding Top="5" Left="0" Right="0" Bottom="5" />
<Border>
<Top Color="Black"></Top>
<Bottom Color="Black"></Bottom>
<Left Color="Black"></Left>
<Right Color="Black"></Right>
</Border>
<Margin Top="15" />
<Row BackgroundColor="LightGray" MinRowHeight="20">
<Border>
<Bottom Color="Black"></Bottom>
</Border>
<Cell Alignment="2">
<TextFragment>
<TextSegment>Departure</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Arrival</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Weekday</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Ship</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>07.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>09.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Mon-Sat</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Star</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>10.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>12.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>every day</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megastar</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>13.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>15.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>every day</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Star</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>16.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>18.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>every day</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megastar</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>19.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>21.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>every day</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Star</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>22.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>00.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Mon-Fri, Sun</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megastar</TextSegment>
</TextFragment>
</Cell>
</Row>
</Table>
</Page>
</Document>
Tables are used for layout of documents. For example, we can customize a page header. In this case, table were used to divide the header into 2 columns.
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
<Page>
<PageInfo IsLandscape="false" Height="595" Width="420">
<Margin Top="71" Bottom="71" Left="28" Right="28" />
</PageInfo>
<Header>
<Margin Top="20" />
<Table ColumnAdjustment="AutoFitToWindow">
<Row>
<Cell Alignment="1">
<TextFragment>
<TextSegment>Date: 01/01/2021</TextSegment>
</TextFragment>
</Cell>
<Cell Alignment="3">
<TextFragment>
<TextSegment>Page $p / $P</TextSegment>
</TextFragment>
</Cell>
</Row>
</Table>
</Header>
<HtmlFragment>
<![CDATA[
<h1 style="font-family:Tahoma; font-size:16pt;">TIMETABLES ON GREENTOWN-BLUEBERG ROUTE</h1>
]]>
</HtmlFragment>
<TextFragment>
<TextSegment>4.1.-28.3.2021 | GREENTOWN → BLUEBERG</TextSegment>
</TextFragment>
<Table ColumnAdjustment="AutoFitToWindow" ColumnWidths ="10 10 10 10">
<DefaultCellPadding Top="5" Left="0" Right="0" Bottom="5" />
<Border>
<Top Color="Black"></Top>
<Bottom Color="Black"></Bottom>
<Left Color="Black"></Left>
<Right Color="Black"></Right>
</Border>
<Margin Top="15" />
<Row BackgroundColor="LightGray" MinRowHeight="20">
<Border>
<Bottom Color="Black"></Bottom>
</Border>
<Cell Alignment="2">
<TextFragment>
<TextSegment>Departure</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Arrival</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Weekday</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Ship</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>07.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>09.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Mon-Sat</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Star</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>10.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>12.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>every day</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megastar</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>13.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>15.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>every day</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Star</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>16.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>18.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>every day</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megastar</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>19.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>21.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>every day</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Star</TextSegment>
</TextFragment>
</Cell>
</Row>
<Row>
<Cell Alignment="2">
<TextFragment>
<TextSegment>22.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>00.30</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Mon-Fri, Sun</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megastar</TextSegment>
</TextFragment>
</Cell>
</Row>
</Table>
</Page>
</Document>
Updating content dynamically
BindXML() method offers the feature to load XML file contents and Document.save() method can be used to save the output in PDF format. However during conversion, we can also access individual elements inside XML and use XML as template. The following code snippet shows the steps to access TextSegments from XML file.
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
<Page id="mainSection">
<TextFragment>
<TextSegment id="boldHtml">segment1</TextSegment>
</TextFragment>
<TextFragment>
<TextSegment id="strongHtml">segment2</TextSegment>
</TextFragment>
</Page>
</Document>
public static void UpdatingContentDynamically() {
// Instantiate Document object
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document();
// Bind source XML file
pdfDocument.bindXml(_dataDir + "log.xml");
// Get reference of first TextSegment with ID boldHtml
TextSegment segment = (TextSegment)pdfDocument.getObjectById("boldHtml");
segment.setText("Demo 1");
// Get reference of second TextSegment with ID strongHtml
segment = (TextSegment)pdfDocument.getObjectById("strongHtml");
segment.setText("Demo 2");
// Save resultant PDF file
pdfDocument.save(_dataDir + "XMLToPDF_out.pdf");
}
Adding graphics elements to the page
We can add other additionital elements to XML document: Image or Graph objects. The following snippet shows how to add those elements to the document
<Graph Width="20" Height="20">
<Circle PosX="30" PosY="30" Radius="10">
<GraphInfo Color="Red" FillColor="Blue"></GraphInfo>
</Circle>
</Graph>
<Image File="logo.png" Id = "testImg"></Image>
Set Image Path While Converting XML to PDF
Following XML template contains an <Image>
tag in it with an ID “testImg”. In case you want to set image path from your code, you can access Image element from XML template during conversion process and set path to your desired address for image.
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
<Page id="mainSection">
<PageInfo IsLandscape="true">
<Margin Left="20" Right="20" Top="10" Bottom="30" />
</PageInfo>
<Header>
<Margin Top="20" />
<Table ColumnAdjustment="AutoFitToWindow">
<Row>
<Cell Alignment="1">
<Image File="logo.png" Id = "testImg"></Image>
</Cell>
<Cell Alignment="3">
<TextFragment>
<TextSegment>Page $p / $P</TextSegment>
</TextFragment>
</Cell>
</Row>
</Table>
</Header>
<Table ColumnAdjustment="AutoFitToWindow" ColumnWidths="8 10">
<DefaultCellPadding Top="0" Left="0" Right="0" Bottom="0" />
<Margin Top="15" />
<Row>
<Cell Alignment="1">
<!--Logo-->
<TextFragment>
<TextSegment> Request ID</TextSegment>
<TextState FontSize="14" ForegroundColor="#0e4f9c" FontStyle="1" />
</TextFragment>
<TextFragment>
<TextSegment></TextSegment>
</TextFragment>
<TextFragment>
<TextSegment id="boldtext">Some Bold Text</TextSegment>
<TextState FontSize="14" FontStyle="1"></TextState>
</TextFragment>
</Cell>
</Row>
</Table>
</Page>
</Document>
Code to set image path in XML template is as follows:
public static void Example_XML_to_PDF_01(){
String inXml = _dataDir + "input.xml";
String inFile = _dataDir + "aspose-logo.jpg";
String outFile = _dataDir + "output_out.pdf";
com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
doc.bindXml(inXml);
com.aspose.pdf.Image image = (com.aspose.pdf.Image)doc.getObjectById("testImg");
image.setFile(inFile);
doc.save(outFile);
}