2010-03-22 276 views
2

我正在使用iTextsharp庫創建PDF文件。我可以申報這樣的A4風景紙:iTextSharp自定義紙張尺寸

Dim pdfTable As New PdfPTable(9) 
pdfTable.WidthPercentage = 100 
Dim pdfDoc As New Document(PageSize.A4.Rotate()) 

我想知道如何手動設置pdfTable或A4高度的高度。由於底部留有更多餘量,因此我需要在該餘量處放置一些文本。現在,我在底部放置了一行文本,該行被推送到了新頁面。

問題1:如何覆蓋iTextsharp提供的A4紙的高度? Q2:如何創建自定義尺寸的紙張,例如寬度= 29釐米,高度= 22釐米?

謝謝。

回答

0

您可以使用自定義PdfpageEvent將文本或表格或任何內容添加到頁腳。

下面是一些代碼,增加了一個4列的表格頁腳(抱歉,這是在C#):

public override void OnEndPage(PdfWriter writer, iTextSharp.text.Document document) 
{ 
    base.OnEndPage(writer, document); 

    PdfContentByte cb = writer.DirectContent; 

    var footerTable = new PdfPTable(4); 

    var columnWidth = (document.Right - document.LeftMargin)/4; 

    footerTable.SetTotalWidth(new float[] { columnWidth, columnWidth, columnWidth, columnWidth }); 

    var cell1 = new PdfPCell(); 
    cell1.AddElement(new Paragraph("Date:")); 
    cell1.AddElement(new Paragraph(DateTime.Now.ToShortDateString())); 
    footerTable.AddCell(cell1); 

    var cell2 = new PdfPCell(); 
    cell2.AddElement(new Paragraph("Data:")); 
    cell2.AddElement(new Paragraph("123456789")); 
    footerTable.AddCell(cell2); 

    var cell3 = new PdfPCell(); 
    cell3.AddElement(new Paragraph("Date:")); 
    cell3.AddElement(new Paragraph(DateTime.Now.ToShortDateString())); 
    footerTable.AddCell(cell3); 

    var cell4 = new PdfPCell(); 
    cell4.AddElement(new Paragraph("Page:")); 
    cell4.AddElement(new Paragraph(document.PageNumber.ToString())); 
    footerTable.AddCell(cell4); 

    footerTable.WriteSelectedRows(0, -1, document.LeftMargin, cell4.Height + 50, cb); 
} 

,這是將調用上面的代碼的代碼:

var pdfWriter = PdfWriter.GetInstance(pdf, new FileStream(fileName, FileMode.Create)); 
pdfWriter.PageEvent = new CustomPdfPageEvent(); 
+1

嗨,我現在添加像Dim pdfDoc作爲新文檔(PageSize.A4.Rotate(),15,35,15,3)的參數。我也會嘗試你的建議。 – Narazana

8

自定義頁面大小iTextSharp的:

Dim pgSize As New iTextSharp.text.Rectangle(myWidth, myHeight) 
Dim doc As New iTextSharp.text.Document(pgSize, leftMargin, rightMargin, topMargin, bottomMargin) 

iTextSharp的使用每英寸72個像素,所以如果你知道的高度和寬度以英寸爲單位的所需頁面大小,只需將這些數字乘以72即可獲得myWidth和myHeight。