带有iTextSharp的表中字体格式的设置

问题描述 投票:0回答:1

我只想更改pdfTable中字体的大小,这样就不会出现换行符。一个问题是System.Drawing和iTextSharp.Text中存在字体。我想念该信息iTextSharp.Text.Font方法的语法应该是什么样子。之后,我将对如何将字体应用于整个表格感兴趣。

private void Cmd_Protocoll_Click(object sender, EventArgs e)
{
    Document pProtocoll = new Document(iTextSharp.text.PageSize.A4.Rotate(), 10, 10, 10, 10);
    PdfWriter.GetInstance(pProtocoll, new FileStream("TestPDF.pdf", FileMode.Create));
    pProtocoll.Open();
    pProtocoll.SetPageSize(PageSize.A4.Rotate());
    pProtocoll.AddTitle("PDF-Erstellung");
    string author = Txt_PreName.Text + Txt_LastName.Text;
    pProtocoll.AddAuthor(author);
    pProtocoll.AddSubject("Was ist das Subject");

    PdfPTable pdfPTable = new PdfPTable(7);
    pdfPTable.TotalWidth = 750f;
    pdfPTable.LockedWidth = true;

    float[] widths = new float[] { 3f, 1f, 1f, 5f, 1f, 2f, 1f };
    pdfPTable.SetWidths(widths);

    PdfPCell cell = new PdfPCell(new Phrase("Prüfprotokol zum Hardwaredatenpunkttest"));

    cell.Colspan = 3;
    cell.HorizontalAlignment = 1;

    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    iTextSharp.text.Font font = new iTextSharp.text.Font(bfTimes, 6, 2,{ 0, 0, 0 });

    try
    {
        foreach (DataGridViewRow row in Dgv_Data_List.Rows)
        {
            foreach (DataGridViewCell celle in row.Cells)
            {
                if (celle.Value.ToString() != null)
                {
                    pdfPTable.AddCell(celle.Value.ToString());
                }
                else
                {
                    pdfPTable.AddCell(string.Empty);
                }
            }
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    pProtocoll.Add(pdfPTable);
    pProtocoll.Close();
}
c# fonts itext
1个回答
0
投票

创建iTextSharp Font实例:

BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font f = new iTextSharp.text.Font(bfTimes, 6,
    iTextSharp.text.Font.ITALIC, BaseColor.BLACK);

将字体应用于每个单元格的内容:

String[][] content = {
    new String[] {"1", "2", "3", "4", "5", "6", "7" },
    new String[] {"A", "B", "C", "D", "E", "F", "G" },
};

foreach (String[] row in content)
{
    foreach (String celle in row)
    {
        pdfPTable.AddCell(new Phrase(celle, f));
    }
}

而且,在您的代码示例中,我看到您正在创建内容为Cell"Prüfprotokol zum Hardwaredatenpunkttest",但您没有将其添加到表中。

© www.soinside.com 2019 - 2024. All rights reserved.