views:

963

answers:

2

in vb.net i filled up the first page of a pdf document, how do i start from the second page?

+3  A: 
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();
gmcalab
please show me how do you add a second page?
I__
Line 13 of gmcalab's answer adds a new page, and lines 14 through 28 add content to that page. Lines 12 and 29 cause that code to loop 5 times, illustrating how this can be done an arbitrary number of times.
jball
The above example is in CSharp, and you did tag your question with vb.net. However, it is remarkably trivial to convert code from C# to VB.Net (see sites like http://www.developerfusion.com/tools/convert/csharp-to-vb/ ), and you should spend some time learning enough CSharp syntax to understand the examples that are not available in VB.Net. It's not that hard, and will save you many headaches down the road.
jball
cool thank you very much., can you recommend me a site which shows differences
I__
http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Visual_Basic_.NET , particularly http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Visual_Basic_.NET#Syntax_comparisons . Also, just compare examples in VB.Net and CSharp on MSDN and the like, and you'll get a feel for the analogs between the two.
jball
A: 

Do not mark this as the answer, this is just gmcalab's code converted to VB for your conveniance. His example answers your question quite handily.

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]() 
jball