簡體   English   中英

itext7 添加形狀和文本

[英]itext7 adding shapes and text

是否可以在 itext7 中創建形狀和文本組合,如下面的鏈接:

我創建了一個包含 3 個單元格的表格並創建了一條水平線。

private Table AverageTable()
{
    float[] widths = new float[] { 30f,1.125f,30f };
    Table planTable = new Table(UnitValue.CreatePointArray(widths));

    PdfFont TaubRegular = PdfFontFactory.CreateFont(System.IO.Path.Combine(ConfigurationManager.AppSettings["FontsLocation"], "TaubSans-Regular.ttf"), PdfEncodings.IDENTITY_H, true);

    SolidLine line = new SolidLine(.5f);
    line.SetColor(new DeviceCmyk(59, 66, 0, 0));
    LineSeparator ls = new LineSeparator(line);

    Cell cellPlanInvestmentsHeading = new Cell()
      .Add(new Paragraph("Average Annual Total Returns(NAV)").SetFixedLeading(8))
      .SetFont(PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN))
      .SetFontSize(7.5f)
      .SetFontColor(new DeviceCmyk(59, 66, 0, 0))
      .SetBackgroundColor(DeviceGray.WHITE)
      .SetTextAlignment(TextAlignment.LEFT)
      .SetVerticalAlignment(VerticalAlignment.TOP).SetPaddingRight(0f).SetMarginRight(0f);


    Cell cellIcon = new Cell().Add(ls)
        .SetHorizontalAlignment(HorizontalAlignment.LEFT)
        .SetVerticalAlignment(VerticalAlignment.MIDDLE)
        .SetPaddingTop(5f);

    planTable.AddCell(cellIcon);
    planTable.AddCell(cellPlanInvestmentsHeading);
    planTable.AddCell(cellIcon);
    return planTable;
}

您可以通過以下方式獲得所需的結果:

private Table AverageTable()
{
    float[] widths = new float[] { 10, 70, 10 };
    Table planTable = new Table(UnitValue.CreatePointArray(widths));

    SolidLine line = new SolidLine(.5f);
    line.SetColor(new DeviceCmyk(59, 66, 0, 0));

    Cell cell = new Cell().SetBorderLeft(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER)
        .SetBorderTop(Border.NO_BORDER);
    Paragraph p = new Paragraph("Average Annual Total Returns(NAV)").SetFixedLeading(8)
        .SetFont(PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN))
        .SetTextAlignment(TextAlignment.CENTER)
        .SetFontSize(7.5f)
        .SetRelativePosition(0, -10, 0, 0);
    cell.Add(p);

    planTable.AddCell(new Cell().SetBorderRight(Border.NO_BORDER));
    planTable.AddCell(cell);
    planTable.AddCell(new Cell().SetBorderLeft(Border.NO_BORDER));
    return planTable;
}

在這里完成這項工作的主要技巧是SetRelativePosition - 它在繪圖階段應用額外的轉換。

視覺結果:

結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM