如何使用 iTextSharp 减小单元格中文本的大小?

How can I reduce the size of text in a cell with iTextSharp?

我正在向 table 添加文本和文本框,如下所示:

PdfPTable tblFirstRow = new PdfPTable(7);
tblFirstRow.WidthPercentage = 100;
tblFirstRow.SpacingBefore = 4f;
//tblFirstRow.LockedWidth = true;
float[] FirstRowWidths = new float[] { 137f, 138f, 175f, 100f, 50f, 175f, 225f };
tblFirstRow.SetWidths(FirstRowWidths);
tblFirstRow.HorizontalAlignment = Element.ALIGN_LEFT;

// "Required Date"
Chunk reqDate = new Chunk("Required Date: ", timesRoman9Font);
Paragraph parReqDate = new Paragraph();
parReqDate.Add(reqDate);
PdfPCell cellReqDate = new PdfPCell(parReqDate);
cellReqDate.BorderWidth = PdfPCell.NO_BORDER;
tblFirstRow.AddCell(cellReqDate);

PdfPCell cellReqDateTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("textBoxReqDate"),
    Phrase = new Phrase("4/16/2015") //, // TODO: get current date
    //FixedHeight = 16
    // set borders, or other cell options here
};
tblFirstRow.AddCell(cellReqDateTextBox);

这非常有效,但如您所见:

...文本 ("4/16/2015") 过大。如何减小文本的大小?我是否需要以某种方式更改 CellLayout 中的矩形大小,即:

public class DynamicTextbox : IPdfPCellEvent
{
    private string fieldname;

    public DynamicTextbox(string name)
    {
        fieldname = name;
    }

    public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
    {
        PdfWriter writer = canvases[0].PdfWriter;

        // rectangle holds the dimensions and coordinates of the cell that was created
        // which you can then use to place the textfield in the correct location
        // and optionally fit the textfield to the size of the cell


        float textboxheight = 11f;
        // modify the rectangle so the textfield isn't the full height of the cell
        // in case the cell ends up being tall due to the table layout
        Rectangle rect = rectangle;
        rect.Bottom = rect.Top - textboxheight;

        TextField text = new TextField(writer, rect, fieldname);
        // set and options, font etc here

        PdfFormField field = text.GetTextField();

        writer.AddAnnotation(field);
    }
}

?

我认为你犯了两个错误(我只能重复一遍,你应该在过去一周问了一个又一个微不足道的问题后开始阅读文档)。

  1. 您正在使用 Phrase 添加日期。由于你没有定义Font,默认的字体系列是Helvetica,样式是regular,字体大小是12。改变单元格的高度不会改变任何东西。更改字体将。如何更改字体?这在我的书的第 2 章中有解释。例如:new Phrase("Font of 9 pt", new Font(FontFamily.HELVETICA, 9)) 将创建字体大小为 9 磅的文本。
  2. 您将日期添加为 Phrase,但您用文本字段覆盖了该文本。这样做的理由是什么?您是否打算允许人们更改文本字段中的日期?在这种情况下,您需要在文本字段级别设置文本、字体和字体大小。如果您创建字体大小为 0 的文本字段,则字体大小将适应 Rectangle 的大小,但在所有其他情况下,将遵循定义的字体大小。

将 DynamicTextbox() 代码更改为:

PdfPCell cellReqDateTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("textBoxReqDate"),
    Phrase = new Phrase(GetCurrentDate(), timesRoman9Font) // <= tip to add explicit font from Bruno, the iText Whisperer
};
tblFirstRow.AddCell(cellReqDateTextBox);

public class DynamicTextbox : IPdfPCellEvent
{
    private string fieldname;

    public DynamicTextbox(string name)
    {
        fieldname = name;
    }

    public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
    {
        PdfWriter writer = canvases[0].PdfWriter;
        TextField text = new TextField(writer, rectangle, fieldname);
        PdfFormField field = text.GetTextField();
        writer.AddAnnotation(field);
    }
}

...工作正常: