从任何 XML 格式创建 PDF 文档
本文档解释了如何从任何 XML 格式创建 PDF 文档。
TallPDF.NET 是一个用于从基于布局的文档对象模型创建 PDF 文档的商业类库。文档对象模型可以以编程方式或从 XML 构建。如果从 XML 创建,可以使用 XSL 将任何 XML 转换为符合 TallPDF.NET 规范的 XML。
布局类
TallPDF.NET 类库包含诸如 Document
、Section
、Footer
和 Paragraph
等类。这是完整的文档。下图显示了如何从布局对象构建 PDF 文档
以编程方式创建 PDF
这是一个“Hello world!”示例,展示了如何以编程方式创建 PDF
// create a new document with a single section
Document document = new Document();
Section section = document.Sections.Add();
document.Sections.Add(section);
// add a new textparagraph to the section
TextParagraph textParagraph = new TextParagraph();
section.Paragraphs.Add(textParagraph);
// create a fragment, set some text and add it to the paragraph
Fragment fragment = new Fragment();
fragment.Text = "Hello world!";
textParagraph.Fragments.Add(fragment);
using (FileStream file = new FileStream("out1.pdf", FileMode.Create))
{
document.Write(file);
}
虽然这看起来只是为了“Hello world!”而编写了大量的代码,但其灵活性在构建更复杂的文档时确实会得到回报。
Xamarin.iOS 和 Xamarin.Android 版本
TallPDF.NET 的新版本 5 包含适用于 iOS 和 Android 的 Xamarin 版本。第一个版本已经可用。如果您感兴趣,可以 加入 Beta 测试计划
从 XML 创建 PDF
如果您熟悉 XAML,那么您就知道 XAML 是一种指定如何实例化对象以及如何分配属性的方式。使用 TallPDF.NET 从 XML 创建 PDF 的方式类似。顺便说一句:我们早在 2002 年就实现了这种方法,远早于最初于 2008 年发布的 XAML。
这是完全相同的“Hello world!”示例,但现在以 XML 形式指定
<document xmlns="http://www.tallcomponents.com/schemas/tallpdf/v3">
<section>
<paragraph type="textparagraph">
<fragment>Hello World</fragment>
</paragraph>
</section>
</document>
这是将此 XML 转换为 PDF 的代码
// create a new document from xml
Document document = new Document();
document.Read("helloworld.xml");
using (FileStream file = new FileStream("out2.pdf", FileMode.Create))
{
document.Write(file);
}
从 XML 和 XSL 创建 PDF
考虑以下描述两个客户的简单 XML 文档
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer id="1">
<Name>Chris Sharp</Name>
</Customer>
<Customer id="2">
<Name>Mike Jones</Name>
</Customer>
</Customers>
显然,由于该文档不包含 XML 元素到布局元素的映射,因此无法将其转换为 PDF。为此,我们编写一个 XSL,将此 XML 转换为我们之前看到的符合 TalPDF.NET 规范的 XML
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.tallcomponents.com/schemas/tallpdf/v3">
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="/">
<document>
<section>
<paragraph type="textparagraph">
<fragment font="helveticabold" fontsize="14">
Simple XSL Transformation
</fragment>
</paragraph>
<xsl:apply-templates select="/Customers/Customer"/>
</section>
</document>
</xsl:template>
<xsl:template match="/Customers/Customer">
<paragraph type="textparagraph">
<fragment font="helveticabold" fontsize="10">
<xsl:value-of select="@id"/>.
</fragment>
<fragment font="helvetica" fontsize="10">
<xsl:value-of select="Name"/>
</fragment>
</paragraph>
</xsl:template>
</xsl:stylesheet>
这是使用 XSL 将客户 XML 转换为 PDF 的代码
// load xml
XmlReader xml = new XmlTextReader("data.xml");
// load xsl
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load("transform.xslt");
// transform
Stream converted = new MemoryStream();
xsl.Transform(xml, null, converted);
converted.Position = 0;
// construct the pdf from transformed xml
Document document = new Document();
XmlReader reader = new XmlTextReader(converted);
document.Read(reader);
using (FileStream file = new FileStream("out3.pdf", FileMode.Create))
{
document.Write(file);
}
结果