带有文本、矩形和表格的 itextpdf 文档

Itextpdf document with text, rectangles and tables

对不起我的英语, 我有创建文档的代码。在前三分之一处是带有文本的矩形,代表 header。 我的代码:

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); 
        }


    }

我需要在这个矩形之后插入 table。 评论之间

// tabulka

// konec tabulky

如何解决?

您在绝对位置添加了很多行文本。您的最后一行文本已添加到位置 Y = 633。您现在想要在最后一行位置下方的绝对位置添加一个 table,比方说在位置 X = 36,Y = 620。您想要 table 居中,所以 table 的宽度为 523;那是 gdoc.PageSize.Width - 36(左边距)- 36(右边距)。

官方文档中解释了如何执行此操作。例如,参见问题 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).

的答案

如果您创建了一个名为 tablePdfPTable,您需要这样的东西:

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

y的值将是table底部的位置。