2013-10-24 62 views
1

如何使用iTextSharp將XML轉換爲PDF? iTextSharp的當前XML到PDF格式顯然是過時的,不起作用。所以我開始解決這個問題,但我無法轉化它。任何人都可以幫助我。如何使用iTextSharp將XML轉換爲PDF?

<?xml version="1.0" encoding="utf-8" ?> 
<catalog> 
    <cd> 
    <SR.No>14</SR.No> 
    <test>loss test</test> 
    <code>ISO-133</code> 
    <unit>gm</unit> 
    <sampleid>36</sampleid> 
    <boreholeid>21</boreholeid> 
    <pieceno>63</pieceno> 
    </cd> 
    <cd> 
    <SR.No>24</SR.No> 
    <test>sand</test> 
    <code>ISO-133</code> 
    <unit>gm</unit> 
    <sampleid>71</sampleid> 
    <boreholeid>22</boreholeid> 
    <pieceno>23</pieceno> 
    </cd> 
    <cd> 
    <SR.No>25</SR.No> 
    <test>clay</test> 
    <code>ISO-133</code> 
    <unit>mg</unit> 
    <sampleid>52</sampleid> 
    <boreholeid>21</boreholeid> 
    <pieceno>36</pieceno> 
    </cd> 
</catalog> 
+0

這是一個非常討厭的API(或至少是5年前)。我多年前在大學時使用過它。當我回家時,會嘗試挖掘一些代碼示例。如果你的Google足夠努力,你應該能夠找到可以拼湊在一起的多個例子。這絕對是在那裏。 – Goober

+0

@Goober謝謝。但是沒有太多的元素和屬性可以將XML轉換爲適當的表格格式,因爲我們可以在不使用Xml的情況下創建表格結構並向其中添加數據,而這些數據是我們想要的格式。 –

+0

我不確定你的意思。從根本上說,您需要對XML有很好的理解才能執行此任務。 – Goober

回答

2

這對你自己來說確實很微不足道。你沒有指定一種語言,所以下面的示例使用VB.Net,因爲(我認爲)它更容易處理XML。查看代碼評論以獲取更多詳細信息。這是針對iTextSharp 5.4.4,但應該幾乎適用於任何版本。

''//Sample XML 
Dim TextXML = <?xml version="1.0" encoding="utf-8"?> 
       <catalog> 
        <cd> 
         <SR.No>14</SR.No> 
         <test>loss test</test> 
         <code>ISO-133</code> 
         <unit>gm</unit> 
         <sampleid>36</sampleid> 
         <boreholeid>21</boreholeid> 
         <pieceno>63</pieceno> 
        </cd> 
        <cd> 
         <SR.No>24</SR.No> 
         <test>sand</test> 
         <code>ISO-133</code> 
         <unit>gm</unit> 
         <sampleid>71</sampleid> 
         <boreholeid>22</boreholeid> 
         <pieceno>23</pieceno> 
        </cd> 
        <cd> 
         <SR.No>25</SR.No> 
         <test>clay</test> 
         <code>ISO-133</code> 
         <unit>mg</unit> 
         <sampleid>52</sampleid> 
         <boreholeid>21</boreholeid> 
         <pieceno>36</pieceno> 
        </cd> 
       </catalog> 

''//File to write to 
Dim TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf") 

''//Standard PDF creation, nothing special here 
Using fs As New FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None) 
    Using doc As New Document() 
     Using writer = PdfWriter.GetInstance(doc, fs) 
      doc.Open() 

      ''//Create a table with one column for every child node of <cd> 
      Dim T As New PdfPTable(TextXML.<catalog>.<cd>.First.Nodes.Count) 

      ''//Loop through the first item to output column headers 
      For Each N In TextXML.<catalog>.<cd>.First.Elements 
       T.AddCell(N.Name.ToString()) 
      Next 

      ''//Loop through each CD row (this is so we can call complete later on) 
      For Each CD In TextXML.<catalog>.Elements 
       ''//Loop through each child of the current CD 
       For Each N In CD.Elements 
        T.AddCell(N.Value) 
       Next 

       ''//Just in case any rows have too few cells fill in any blanks 
       T.CompleteRow() 
      Next 

      ''//Add the table to the document 
      doc.Add(T) 

      doc.Close() 
     End Using 
    End Using 
End Using 

編輯

這裏有一個C#版本。我已經包含了一個幫助器方法來根據您的模板創建一個大的XML文檔來顯示頁面溢出。 PdfPTable會自動發送多個頁面。您可以指定應被視爲「標題」的行數,以便在後續頁面上重複。你可能會想也適用於一些格式規則,但你應該能夠找到那些在線(尋找PdfPTable.DefaultCell

private XDocument createXml() { 
    //Create our sample XML document 
    var xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); 

    //Add our root node 
    var root = new XElement("catalog"); 
    //All child nodes 
    var nodeNames = new[] { "SR.No", "test", "code", "unit", "sampleid", "boreholeid", "pieceno" }; 
    XElement cd; 

    //Create a bunch of <cd> items 
    for (var i = 0; i < 1000; i++) { 
     cd = new XElement("cd"); 
     foreach (var nn in nodeNames) { 
      cd.Add(new XElement(nn) { Value = String.Format("{0}:{1}", nn, i.ToString()) }); 
     } 
     root.Add(cd); 
    } 

    xml.Add(root); 

    return xml; 
} 

private void doWork() { 
    //Sample XML 
    var xml = createXml(); 

    //File to write to 
    var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf"); 

    //Standard PDF creation, nothing special here 
    using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) { 
     using (var doc = new Document()) { 
      using (var writer = PdfWriter.GetInstance(doc, fs)) { 
       doc.Open(); 

       //Count the columns 
       var columnCount = xml.Root.Elements("cd").First().Nodes().Count(); 

       //Create a table with one column for every child node of <cd> 
       var t = new PdfPTable(columnCount); 

       //Flag that the first row should be repeated on each page break 
       t.HeaderRows = 1; 

       //Loop through the first item to output column headers 
       foreach (var N in xml.Root.Elements("cd").First().Elements()) { 
        t.AddCell(N.Name.ToString()); 
       } 

       //Loop through each CD row (this is so we can call complete later on) 
       foreach (var CD in xml.Root.Elements()) { 
        //Loop through each child of the current CD. Limit the number of children to our initial count just in case there are extra nodes. 
        foreach (var N in CD.Elements().Take(columnCount)) { 
         t.AddCell(N.Value); 
        } 
        //Just in case any rows have too few cells fill in any blanks 
        t.CompleteRow(); 
       } 

       //Add the table to the document 
       doc.Add(t); 

       doc.Close(); 
      } 
     } 
    } 
} 
+0

謝謝。我使用C#所以我想我需要在C#中的代碼。仍然有一個疑問,在我的情況下靜態字段保持相同的PDF生成過程中,只有數據(像測試號,單位,值是代碼將動態附加到這些靜態字段或標籤,所以我應該如何繼續添加新頁面,因爲我在XMl中的數據非常大,並且需要多個PDF頁面才能生成報告,因此定義的表格應該保持不變數據應該正確地附加到特定的標籤或字段 –

+0

感謝您分享c#版本,我必須做很多修改,因爲我的標題是垂直格式,並且在聲明標題後每列的頁數不應超過6個。非常感謝克里斯。 –

相關問題