itextsharp:如何添加新页面并写入?

时间:2009-12-21 14:17:45

标签: vb.net pdf itextsharp

在vb.net中,我填写了pdf文档的第一页,如何从第二页开始?

2 个答案:

答案 0 :(得分:14)

Document document = new Document(PageSize.A4, 0, 0, 50, 50);
System.IO.MemoryStream msReport = new System.IO.MemoryStream();

try {
    // creation of the different writers
    PdfWriter writer = PdfWriter.GetInstance(document, msReport);

    // we add some meta information to the document
    document.AddTitle("My Title");  
    document.AddAuthor("Me");
    document.Open();

    for (int i = 1; i <= 5; i++)
    {
        document.NewPage();
        iTextSharp.text.Table datatable = new iTextSharp.text.Table(3);
        datatable.Padding = 2;
        datatable.Spacing = 0;
        float[] headerwidths = { 6, 20, 32 };
        datatable.Widths = headerwidths;
        datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
        datatable.AddCell(i.ToString());
        datatable.AddCell("This is my name.");
        datatable.AddCell("0123456789");

        datatable.AddCell("No");
        datatable.AddCell("Yes");
        datatable.AddCell("No");

        document.Add(datatable);
     } 
} 
catch (Exception e) { 
    Console.Error.WriteLine(e.Message); 
} 

// we close the document 
document.Close(); 

Response.Clear(); 
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf"); 
Response.ContentType = "application/pdf"; 
Response.BinaryWrite(msReport.ToArray()); 
Response.End(); 

答案 1 :(得分:7)

不要将此标记为答案,这只是将gmcalab的代码转换为VB以实现您的便利性。他的例子非常轻松地回答了你的问题。

Dim document As New Document(PageSize.A4, 0, 0, 50, 50) 
Dim msReport As New System.IO.MemoryStream() 

Try 
    ' creation of the different writers 
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, msReport) 

    ' we add some meta information to the document 
    document.AddTitle("My Title") 
    document.AddAuthor("Me") 
    document.Open() 

    For i As Integer = 1 To 5 
        document.NewPage() 
        Dim datatable As New iTextSharp.text.Table(3) 
        datatable.Padding = 2 
        datatable.Spacing = 0 
        Dim headerwidths As Single() = {6, 20, 32} 
        datatable.Widths = headerwidths 
        datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT 
        datatable.AddCell(i.ToString()) 
        datatable.AddCell("This is my name.") 
        datatable.AddCell("0123456789") 

        datatable.AddCell("No") 
        datatable.AddCell("Yes") 
        datatable.AddCell("No") 

        document.Add(datatable) 
    Next 
Catch e As Exception 
    Console.[Error].WriteLine(e.Message) 
End Try 

' we close the document 
document.Close() 

Response.Clear() 
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf") 
Response.ContentType = "application/pdf" 
Response.BinaryWrite(msReport.ToArray()) 
Response.[End]()