单个对象会被转化为Aspose.Pdf DOM(文档对象模型),Aspose.Pdf提供了一个非常惊人的功能,可以访问这些单个对象。假设需要通过XML文件生成一个PDF文档(在PDF生成之前需要对这些单个对象提供某些特定的格式),或者是想要从一个XML文档中导入标题,然后将它们转换成所生成的PDF书签。你将如何实现呢?下面是个很简单的方法:
C#
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
//Object xmlDoc contains all contents from original word document in XML format defined in Aspose.PDF
//XML Schema.
pdf.BindXML(xmlDoc, null);
//Before saving, to add bookmarks from headings.
pdf.IsBookmarked = true;
foreach (Aspose.Pdf.Generator.Section sec in pdf.Sections)
{
foreach (Aspose.Pdf.Generator.Paragraph para in sec.Paragraphs)
{
if (para is Aspose.Pdf.Generator.Heading)
{
Aspose.Pdf.Generator.Heading h = para as Aspose.Pdf.Generator.Heading;
h.IsInList = true;
}
}
}
pdf.Save(outputFile);
VB.NET
Dim pdf As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
'Object xmlDoc contains all contents from original word document in XML format defined in Aspose.PDF
'XML Schema.
pdf.BindXML(xmlDoc,Nothing)
'Before saving, to add bookmarks from headings.
pdf.IsBookmarked = True
Dim sec As Aspose.Pdf.Generator.Section
For Each sec In pdf.Sections
Dim para As Aspose.Pdf.Generator.Paragraph
For Each para In sec.Paragraphs
If TypeOf para Is Aspose.Pdf.Generator.Heading Then
Dim h As Aspose.Pdf.Generator.Heading = para as Aspose.Pdf.Generator.Heading
h.IsInList = True
End If
Next
Next
pdf.Save(outputFile)
结论
基于上述示例中,可以看到,在轻松实现的XML文件转换成PDF格式的同时,Aspose.Pdf也提供了一个简单但强大的API,在PDF文档生成和输出之前,用于访问和修改这些单个对象。