Itextsharp: how to place a position in a table?

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

here is 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 is working, and the second is not

+3
source share
2 answers

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);
+9
source

Make sure your code closes the document and initializes PdfWriter.

An example of what I'm using (the output path is a variable passed to a function in C #):

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

//write stuff here

document.Close();
+1
source

Source: https://habr.com/ru/post/1716735/


All Articles