Search code examples
c#itextpdf

Itextpdf document with text, rectangles and tables


sorry for my english, i have code which creates a document. In first third is located rectangle with text, representing header. My code:

private void createGPDFbutton_Click(object sender, EventArgs e)
    {
        string PDFcesta = AppDomain.CurrentDomain.BaseDirectory; 
        Document gdoc = new Document(iTextSharp.text.PageSize.A4, 42, 10, 42, 35);
        //***** definice fontů *****
        //Nastavení fontu, aby to tisklo řádně česky
        string ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
        string CONSOLA_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "consola.ttf");
        string COUR_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "cour.ttf");
        string COURBD_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "courbd.ttf"); // courier tučné
        string TIMES_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "times.ttf");
        string TIMESBD_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "timesbd.ttf"); // courier tučné
        //Create a base font object making sure to specify IDENTITY-H
        BaseFont BF_ARIAL = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont BF_CONSOLAS = BaseFont.CreateFont(CONSOLA_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont BF_COURIER = BaseFont.CreateFont(COUR_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont BF_COURIER_BOLD = BaseFont.CreateFont(COURBD_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont BF_TIMES = BaseFont.CreateFont(TIMES_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont BF_TIMES_BOLD = BaseFont.CreateFont(TIMESBD_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //Create a specific font object
        iTextSharp.text.Font f = new iTextSharp.text.Font(BF_ARIAL, 12, iTextSharp.text.Font.NORMAL);
        iTextSharp.text.Font f1 = new iTextSharp.text.Font(BF_CONSOLAS, 10, iTextSharp.text.Font.NORMAL);
        //***** konec fontů *****
        try
        { 
            PdfWriter gwriter = PdfWriter.GetInstance(gdoc, new FileStream(PDFcesta + "graphicsPDF.pdf", FileMode.Create));
            gdoc.Open();
            PdfContentByte cb = gwriter.DirectContent;
            cb.SaveState(); // uložení aktuálního nastavení grafiky
            cb.SetColorStroke(new BaseColor(0,0,0));
            cb.SetColorFill(new BaseColor(255,192,203));
            cb.SetLineWidth(1.5f);
            // Bottom left coordinates x & y, followed by width, height and radius of corners.
            cb.RoundRectangle(48f, 600f, 495.833f, 192.4f, 5f);
            cb.FillStroke();
            cb.SetLineWidth(0.5f);
            cb.Rectangle((gdoc.PageSize.Width / 2) - 35f, 630f,70f,10f);
            cb.Stroke();
            cb.Rectangle((gdoc.PageSize.Width / 2) - 35f, 640f, 70f, 10f);
            cb.Stroke();
            //zápis textu
            cb.BeginText();
            cb.SetColorFill(BaseColor.BLACK);
            cb.SetFontAndSize(BF_TIMES_BOLD, 15f);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "VÝKAZ", gdoc.PageSize.Width / 2, 753f, 0);
            cb.SetFontAndSize(BF_TIMES_BOLD, 15f);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "ZISKU A ZTRÁTY", gdoc.PageSize.Width / 2, 733f, 0);
            cb.SetFontAndSize(BF_TIMES_BOLD, 5f);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "v plném rozsahu", gdoc.PageSize.Width / 2, 713f, 0);
            cb.SetFontAndSize(BF_TIMES_BOLD, 12f);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "31.12.2015", gdoc.PageSize.Width / 2, 683f, 0);
            cb.SetFontAndSize(BF_TIMES_BOLD, 7f);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "v tisících Kč", gdoc.PageSize.Width / 2, 663f, 0);
            cb.SetFontAndSize(BF_TIMES, 5f);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "IČO", gdoc.PageSize.Width / 2, 643f, 0);
            cb.SetFontAndSize(BF_TIMES_BOLD, 5f);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "00020711", gdoc.PageSize.Width / 2, 633f, 0);
            cb.EndText();
            //konec textu
            cb.RestoreState();  // obnovení původního nastavení grafiky
            // tabulka


            // konec tabulky
            gdoc.Close();
            System.Diagnostics.Process.Start(PDFcesta + "graphicsPDF.pdf");
        }
        catch (Exception ex)
        { 
            MessageBox.Show(ex.Message); 
        }


    }

I need to insert the table after this rectangle. Between comments

// tabulka

// konec tabulky

How to solve this?


Solution

  • You're adding a lot of lines of text at absolute positions. Your last line of text was added at position Y = 633. You now want to add a table at an absolute position below that last line position, let's say at position X = 36, Y = 620. You want the table to be centered, so the width of the table will be 523; that's gdoc.PageSize.Width - 36 (left margin) - 36 (right margin).

    How to do this is explained in the official documentation. See for instance the answer to the question How to add a table to the bottom of the last page? (Please browse the documentation and do some searches, for instance for the WriteSelectedRows() method).

    If you've created a PdfPTable named table, you'd need something like this:

    table.TotalWidth = 523;
    float y = table.writeSelectedRows(0, -1, 36, 620, writer.DirectContent);
    

    The value of y will be the position of the bottom of the table.