使用Aspose.Pdf處理XML文檔生成PDF文件

單個對象會被轉化爲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文檔生成和輸出之前,用於訪問和修改這些單個對象。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章