views:

2732

answers:

2

I have created a simple table and i need to position. does anyone have experience with tables in itextsharp?

here's my code

   Private Sub generate_PDF()
    Directory.SetCurrentDirectory("C:\Users\alexluvsdanielle\Desktop\")
    Console.WriteLine("Chapter 6 example 1: Adding a Wmf, Gif, Jpeg and Png-file using urls")
    Dim document As Document = New Document
    Try
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream("Chap1002.pdf", FileMode.Create))


        document.Open()
        'Dim wmf As Image = Image.GetInstance("harbour.wmf")
        'Dim gif As Image = Image.GetInstance("vonnegut.gif")
        Dim jpeg As Image = Image.GetInstance("C:\Users\alexluvsdanielle\Desktop\test.jpg")
        'Dim png As Image = Image.GetInstance("hitchcock.png")
        'document.Add(wmf)
        'document.Add(gif)
        jpeg.ScalePercent(50)
        'jpeg.Alignment = Image.TOP_BORDER
        jpeg.SetAbsolutePosition(0, 562)
        document.Add(jpeg)
        'document.Add(png)
        Dim cb As PdfContentByte = writer.DirectContent

        cb.BeginText()
        Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
        cb.SetFontAndSize(bf, 12)
        'Dim text As String = "Sample text for alignment"
        'cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250, 700, 0)
        'cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250, 650, 0)
        'cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, 600, 0)
        cb.SetTextMatrix(150, 652)
        cb.ShowText(patient_name)

        cb.SetTextMatrix(150, 637)
        cb.ShowText(doc_name)

        cb.SetFontAndSize(bf, 8)

        cb.SetTextMatrix(150, 620)
        cb.ShowText(lot__no)

        cb.SetTextMatrix(150, 611)
        cb.ShowText(patient_id)


        Dim i As Integer
        For i = 1 To 10
            cb.SetTextMatrix(150, 600 - (i * 10))
            cb.ShowText(DataGridView1.Item(3, i).Value)
        Next
        cb.EndText()


        Dim aTable As Table = New Table(2, 2)

        aTable.Offset = 10

        aTable.Width = 100




        aTable.AddCell("0.0")
        aTable.AddCell("0.1")
        aTable.AddCell("1.0")
        aTable.AddCell("1.1")
        document.Add(aTable)

        Dim datatable As PdfPTable = New PdfPTable(12)
        Dim page As Rectangle = document.PageSize
        datatable.TotalWidth = 100


        datatable.AddCell("hello")


        datatable.WriteSelectedRows(0, -1, document.LeftMargin, document.BottomMargin, writer.DirectContent)
        document.Add(datatable)

    Catch de As DocumentException
        Console.Error.WriteLine(de.Message)
        MessageBox.Show(de.Message)
    Catch ioe As IOException
        Console.Error.WriteLine(ioe.Message)
        MessageBox.Show(ioe.Message)
    Catch e As Exception
        Console.Error.WriteLine(e.Message)
        MessageBox.Show(e.Message)

    End Try
    document.Close()
End Sub

the first table works but the second does not

+1  A: 

You can do something like this:

PdfPTable foot = new PdfPTable(2);
foot.TotalWidth = page.Width - document.LeftMargin - document.RightMargin;
foot.WriteSelectedRows(0, -1, document.LeftMargin, document.BottomMargin, writer.DirectContent);
Matthew Talbert
hey matthew thank you so much for your help. which language are you writing in? im using vb.net and this is how i declare a table Dim aTable As Table = New Table(2, 2)
I__
I'm using C#. Notice that I'm using PdfPTable rather than Table. I believe it allows better positioning; you would declare it in VB as Dim aTable as PdfPTable = new PdfPTable(2);
Matthew Talbert
cool what is foot and page?
I__
pdftable doesnt allow total width
I__
not pdftable, PdfPTable. Notice the 'P' between pdf and table. Page is the current page size. Get it with "Dim page as Rectangle = document.PageSize". foot is simply the name of the table; in my case, it's functioning as a footer, hence the name.
Matthew Talbert
tried your code but got nothing
I__
+1  A: 

Make sure you code is closing the document and initializing the pdfwriter.

Example of what I use (output path is a variable passed into the function in C#):

Document document = new Document();            
var writer = PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create));
document.Open();

//write stuff here

document.Close();
llamaoo7
yes i do have the opening and closing. i have a regular table which works, but this one doesnt seem to work
I__
lama may i contact u directly?
I__
Sure, though be warned: I'm not an iText expert. I've only used in a pinch as an alternative to other reporting techniques.
llamaoo7