這對你自己來說確實很微不足道。你沒有指定一種語言,所以下面的示例使用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();
}
}
}
}
這是一個非常討厭的API(或至少是5年前)。我多年前在大學時使用過它。當我回家時,會嘗試挖掘一些代碼示例。如果你的Google足夠努力,你應該能夠找到可以拼湊在一起的多個例子。這絕對是在那裏。 – Goober
@Goober謝謝。但是沒有太多的元素和屬性可以將XML轉換爲適當的表格格式,因爲我們可以在不使用Xml的情況下創建表格結構並向其中添加數據,而這些數據是我們想要的格式。 –
我不確定你的意思。從根本上說,您需要對XML有很好的理解才能執行此任務。 – Goober