Itextpdf文档包含文本,矩形和表格

时间:2016-04-06 13:43:57

标签: c# itextpdf

抱歉我的英文, 我有创建文档的代码。在第三个位置是带有文本的矩形,表示标题。 我的代码:

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


    }

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

// tabulka

// konec tabulky

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您在绝对位置添加了大量文本行。你的最后一行文字被添加到位置Y = 633.你现在想要在最后一行位置下面的绝对位置添加一个表,让我们说位置X = 36,Y = 620.你想要表要居中,所以桌子的宽度为523;那个gdoc.PageSize.Width - 36(左边距) - 36(右边距)。

官方文档中解释了如何执行此操作。例如,请参阅问题How to add a table to the bottom of the last page?的答案(请浏览文档并进行一些搜索,例如WriteSelectedRows() method)。

如果您已创建名为PdfPTable的{​​{1}},则需要以下内容:

table

table.TotalWidth = 523; float y = table.writeSelectedRows(0, -1, 36, 620, writer.DirectContent); 的值将是表格底部的位置。