Gerar PDF a partir de XML
Gerar um documento PDF a partir de um documento XML não é uma tarefa trivial porque o documento XML pode descrever diferentes conteúdos. Aspose.PDF para Java tem várias maneiras de gerar PDF com base em documento XML:
- usando transformação XSLT
- usando marcação XSL-FO (Objetos de Formatação XSL)
- usando o próprio Schema XML do Aspose.PDF
Gerando documento PDF usando transformação XSLT
XSL (eXtensible Stylesheet Language) é uma linguagem de estilo para transformar documentos XML em outros documentos XML ou HTMLs. No nosso caso, podemos usar a transformação de XML para HTML e então criar PDF com base nos dados HTML.
Assumimos que temos um arquivo XML com um catálogo de CDs simples (veja abaixo).
<?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>
Para converter este arquivo em PDF, devemos criar um XSL com layout HTML. Vamos renderizar nossos dados em uma tabela. O arquivo XSL que nos ajudará a fazer isso pode ser algo assim:
<?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>Minha Coleção de CDs</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Título</th>
<th style="text-align:left">Artista</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>
Portanto, precisamos transformar XML e carregar no documento PDF. O exemplo a seguir mostra este modo:
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");
}
}
Gerando documento PDF usando a marcação XSL-FO
XSL-FO é uma linguagem de marcação baseada em XML que descreve o formato dos dados XML para saída em tela, papel ou outros meios. Aspose.PDF tem uma classe especial que permite aplicar a marcação XSL-FO e obter um documento PDF.
Vamos pegar um exemplo. Aqui está um arquivo XML com dados de exemplo de funcionários.
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<companyname>ABC Inc.</companyname>
<employee>
<id>101</id>
<name>Andrew</name>
<designation>Gerente</designation>
</employee>
<employee>
<id>102</id>
<name>Eduard</name>
<designation>Executivo</designation>
</employee>
<employee>
<id>103</id>
<name>Peter</name>
<designation>Executivo</designation>
</employee>
</employees>
Vamos criar ainda outro arquivo - o arquivo de marcação XSL-FO para transformar os dados dos funcionários em uma tabela.
<?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">
Nome da Empresa: <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 = 'Gerente'">
<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 tem uma classe especial XslFoLoadOptions que permite aplicar transformação XSL-FO. O trecho a seguir mostra como usar esta classe com os arquivos de exemplo descritos acima.
public static void Example_XSLFO_to_PDF() {
// Instanciar objeto XslFoLoadOption
com.aspose.pdf.XslFoLoadOptions options = new com.aspose.pdf.XslFoLoadOptions(_dataDir+"employees.xslt");
// Criar objeto Document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(_dataDir+"employees.xml", options);
pdfDocument.save(_dataDir + "data_xml.pdf");
}
Gerando documento PDF com base no esquema XML do Aspose.PDF
Outra maneira de criar um documento PDF a partir de XML é usando o esquema XML do Aspose.PDF. Usando este diagrama, você pode descrever o layout da página da mesma forma que faria se estivesse usando um layout de tabela em HTML. Vamos considerar o trabalho deste método com mais detalhes.
Definindo a página
Vamos definir a página com parâmetros padrão. Nossa página terá o tamanho de uma folha A4 e conterá apenas um pedaço de texto.
<?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>
Para gerar o documento PDF, usaremos o método bindXml.
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");
}
Para definir um novo tamanho de página, devemos adicionar um elemento PageInfo
. No exemplo a seguir, definimos o tamanho de página A5 e margens de 25mm e 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>
Adicionando elemento HtmlFragment em arquivo XML
Como o HTML contém tags semelhantes ao XML, quando você escreve HTML dentro de qualquer tag XML, o analisador o trata como marcações XML e elas simplesmente não podem ser reconhecidas como tags XML. O problema pode ser superado usando a Seção “CDATA” no XML. A Seção CDATA contém texto que não é analisado pelo analisador ou, em outras palavras, não é tratado como marcações XML. O modelo XML de exemplo a seguir mostra como adicionar HtmlFragment dentro de marcações XML usando 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;">Esta é uma string HTML.</font>
]]>
</HtmlFragment>
</Page>
</Document>
Adicionando elemento Table em arquivo XML
Os elementos Table
, Row
, Cell
são usados para descrever tabelas. O trecho a seguir mostra o uso de uma tabela simples. Neste exemplo, algumas células têm o atributo Alignment
e esse atributo tem valor numérico:
- Alinhamento à esquerda
- Alinhamento ao centro
- Alinhamento à direita.
- Alinhamento justificado. O texto será alinhado nas margens esquerda e direita.
- Justificação total. Similar ao alinhamento ‘Justificado’, exceto que a última linha será apenas alinhada à esquerda no modo ‘Justificado’, enquanto no modo ‘Justificação total’ todas as linhas serão alinhadas à esquerda e à direita.
<?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;">HORÁRIOS NA ROTA GREENTOWN-BLUEBERG</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>Partida</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Chegada</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Dia da Semana</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Navio</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>Seg-Sáb</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Estrela</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>todos os dias</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megaestrela</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>todos os dias</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Estrela</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>todos os dias</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megaestrela</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>todos os dias</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Estrela</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>Seg-Sex, Dom</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megaestrela</TextSegment>
</TextFragment>
</Cell>
</Row>
</Table>
</Page>
</Document>
Tables são usadas para layout de documentos. Por exemplo, podemos personalizar um cabeçalho de página. Neste caso, a tabela foi usada para dividir o cabeçalho em 2 colunas.
<?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>Data: 01/01/2021</TextSegment>
</TextFragment>
</Cell>
<Cell Alignment="3">
<TextFragment>
<TextSegment>Página $p / $P</TextSegment>
</TextFragment>
</Cell>
</Row>
</Table>
</Header>
<HtmlFragment>
<![CDATA[
<h1 style="font-family:Tahoma; font-size:16pt;">HORÁRIOS NA ROTA GREENTOWN-BLUEBERG</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>Partida</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Chegada</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Dia da semana</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Navio</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>Seg-Sáb</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Estrela</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>todos os dias</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megaestrela</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>todos os dias</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Estrela</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>todos os dias</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megaestrela</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>todos os dias</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Estrela</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>Seg-Sex, Dom</TextSegment>
</TextFragment>
</Cell>
<Cell>
<TextFragment>
<TextSegment>Megaestrela</TextSegment>
</TextFragment>
</Cell>
</Row>
</Table>
</Page>
</Document>
Atualizando conteúdo dinamicamente
O método BindXML() oferece o recurso de carregar conteúdos de arquivo XML e o método Document.save() pode ser usado para salvar a saída em formato PDF. No entanto, durante a conversão, também podemos acessar elementos individuais dentro do XML e usar o XML como modelo. O trecho de código a seguir mostra as etapas para acessar TextSegments de um arquivo XML.
<?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 AtualizandoConteudoDinamicamente() {
// Instanciar objeto Document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document();
// Vincular arquivo XML de origem
pdfDocument.bindXml(_dataDir + "log.xml");
// Obter referência do primeiro TextSegment com ID boldHtml
TextSegment segment = (TextSegment)pdfDocument.getObjectById("boldHtml");
segment.setText("Demo 1");
// Obter referência do segundo TextSegment com ID strongHtml
segment = (TextSegment)pdfDocument.getObjectById("strongHtml");
segment.setText("Demo 2");
// Salvar arquivo PDF resultante
pdfDocument.save(_dataDir + "XMLToPDF_out.pdf");
}
Adicionando elementos gráficos à página
Podemos adicionar outros elementos adicionais ao documento XML: objetos de Imagem ou Gráfico. O trecho a seguir mostra como adicionar esses elementos ao documento
<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>
Definir o Caminho da Imagem ao Converter XML para PDF
O modelo XML a seguir contém uma tag <Image>
com um ID “testImg”. Caso você queira definir o caminho da imagem a partir do seu código, você pode acessar o elemento Image do modelo XML durante o processo de conversão e definir o caminho para o endereço desejado para a imagem.
<?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>Página $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>ID do Pedido</TextSegment>
<TextState FontSize="14" ForegroundColor="#0e4f9c" FontStyle="1" />
</TextFragment>
<TextFragment>
<TextSegment></TextSegment>
</TextFragment>
<TextFragment>
<TextSegment id="boldtext">Algum Texto em Negrito</TextSegment>
<TextState FontSize="14" FontStyle="1"></TextState>
</TextFragment>
</Cell>
</Row>
</Table>
</Page>
</Document>
Code para definir o caminho da imagem no modelo XML é o seguinte:
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);
}