使用 iTextSharp 的表格中的字体格式设置



我只想在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();
}

创建一个 iTextSharpFont实例:

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

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

最新更新