【问题标题】:Itextsharp Header spacing with multiple column具有多列的 Itextsharp 标题间距
【发布时间】:2017-05-19 03:34:19
【问题描述】:

我已经使用 itextsharp 创建了 pdf 文件,并且创建了 Header 确定,但是当我创建具有两列的表时。我没有从标题到我的两列的间距

代码

PdfPTable table1 = new PdfPTable(2);
table1.DefaultCell.Border = Rectangle.NO_BORDER;
table1.WidthPercentage = 100;

PdfPCell cell11 = new PdfPCell();
Paragraph UniName = new Paragraph(@"TRƯỜNG ĐẠI HỌC CÔNG NGHỆ ĐỒNG NAI", fontBold);
UniName.Alignment = Element.ALIGN_CENTER;
Paragraph paragraph = new Paragraph(@"PHÒNG ĐÀO TẠO", times);
paragraph.Alignment = Element.ALIGN_CENTER;
cell11.AddElement(UniName);
cell11.AddElement(paragraph);
cell11.BorderColor = BaseColor.WHITE;

PdfPCell cell12 = new PdfPCell();
Paragraph QH1 = new Paragraph(@"CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM", fontBold);
QH1.Alignment = Element.ALIGN_CENTER;
Paragraph QH = new Paragraph(@"Độc lập - Tự do - Hạnh phúc", fontBold);
QH.Alignment = Element.ALIGN_CENTER;
Paragraph Symbol = new Paragraph(@"----------oOo----------", fontBold);
Symbol.Alignment = Element.ALIGN_CENTER;
cell12.AddElement(QH1);
cell12.AddElement(QH);
cell12.AddElement(Symbol);
cell12.BorderColor = BaseColor.WHITE;
table1.AddCell(cell11);
table1.AddCell(cell12);

Paragraph TitleReport = new Paragraph(@"TỔNG HỢP COI CHẤM THI", titleFont);
TitleReport.Alignment = Element.ALIGN_CENTER;

Paragraph Info = new Paragraph(string.Format(@"Từ ngày: {0}     đến ngày: {1}        Đơn vị: {2}", DateTime.Now.AddDays(-2).ToString("dd/MM/yyyy"), DateTime.Now.ToString("dd/MM/yyyy"), "Trung tâm quản lý chất lượng"), normalFont);

Info.Alignment = Element.ALIGN_LEFT;
Info.SpacingBefore = 20;
Info.SpacingAfter = 200;
PdfContentByte canvas = writer.DirectContent;
ColumnText ct = new ColumnText(canvas);
int side_of_the_page = 0;
ct.SetSimpleColumn(COLUMNS[side_of_the_page]);
int paragraphs = 0;
while (paragraphs < 28)
{
    PdfPTable table = new PdfPTable(5);
    table.AddCell("STT");
    table.AddCell("Số Phách");
    table.AddCell("Điểm bằng số");
    table.AddCell("Điểm bằng chữ");
    table.AddCell("Ghi chú");

    for (int i = 0; i < 33; i++)
    {
        table.AddCell((i + 1).ToString());
        table.AddCell("209292");
        table.AddCell("4");
        table.AddCell("Điểm bằng chữ");
        table.AddCell("");
        ++paragraphs;
    }
    table.HeaderRows = 1;

    //table.LockedWidth = true;
    ct.AddElement(table);
    //ct.AddElement(new Paragraph(String.Format("Paragraph {0}: {1}", , TEXT)));
    while (ColumnText.HasMoreText(ct.Go()))
    {
        if (side_of_the_page == 0)
        {
            side_of_the_page = 1;
            canvas.MoveTo(297.5f, 36);
            canvas.LineTo(297.5f, 806);
            canvas.Stroke();
        }
        else
        {
            side_of_the_page = 0;
            doc.NewPage();

        }
        ct.SetSimpleColumn(COLUMNS[side_of_the_page]);
    }
}

doc.Add(table1);
doc.Add(TitleReport);
doc.Add(Info);

(Also here)

【问题讨论】:

  • 请出示关键代码。
  • 尽可能多的堆栈溢出问题应该是有意义的,而不需要咨询外部资源。因此,我将您的代码复制到您的问题中。
  • 谢谢。但我无法解决我的问题。如果你能解决它,请告诉我
  • 好的,问题的原因是您混合了 iText 的自动布局(当您使用 doc.Add 添加文档标题和标题时)和手动布局(当您使用 @ 添加内容表时987654327@)。 iText 的自动布局不考虑手动布局添加的任何内容。因此,您必须将通过自动布局添加的内容考虑到您的手动布局中;可能对所有内容使用手动布局会更容易。顺便说一句,我有点惊讶您在所有内容之后添加文档标题。这可以将您的标题推到后面的页面上。

标签: c# itext multiple-columns


【解决方案1】:

问题的原因是您混合了 iText 的自动布局(当您使用 doc.Add 添加文档标题和标题时)和手动布局(当您使用 ColumnText 添加内容时)。

iText 的自动布局不考虑手动布局添加的任何内容。因此,必须将通过自动布局添加的内容考虑到您的手动布局中(可能对所有内容都使用手动布局会更容易)。

此外,您在所有内容之后添加文档标题 有点令人惊讶。给定足够的内容,这会将您的标题推到第一个页面之外的页面上!

而您的 while (paragraphs &lt; 28) 没有任何意义,因为在第一次运行 while 主体时,您会立即将内部循环中的变量推到 28 之外 for (int i = 0; i &lt; 33; i++) { ...; ++paragraphs; }

在您的代码中,您只需添加一次标题(table1TitleReportInfo),我假设您只想在第一页上使用此标题。

我通过以下方式更改了您的代码:

  • 首先将标头添加到Document

  • PdfWriter请求当前y位置,

    float afterHeaderY = writer.GetVerticalPosition(true);
    

    假设PdfWriter 被命名为writer

  • 使用这个 y 位置作为列的顶部 y 坐标,第一页上的中心线在后面的页面中恢复为硬编码 806,和

  • 删除 while 循环和 cmets 中的代码。

新代码:

PdfPTable table1 = new PdfPTable(2);
table1.DefaultCell.Border = Rectangle.NO_BORDER;
table1.WidthPercentage = 100;

PdfPCell cell11 = new PdfPCell();
Paragraph UniName = new Paragraph(@"TRƯỜNG ĐẠI HỌC CÔNG NGHỆ ĐỒNG NAI", fontBold);
UniName.Alignment = Element.ALIGN_CENTER;
Paragraph paragraph = new Paragraph(@"PHÒNG ĐÀO TẠO", times);
paragraph.Alignment = Element.ALIGN_CENTER;
cell11.AddElement(UniName);
cell11.AddElement(paragraph);
cell11.BorderColor = BaseColor.WHITE;

PdfPCell cell12 = new PdfPCell();
Paragraph QH1 = new Paragraph(@"CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM", fontBold);
QH1.Alignment = Element.ALIGN_CENTER;
Paragraph QH = new Paragraph(@"Độc lập - Tự do - Hạnh phúc", fontBold);
QH.Alignment = Element.ALIGN_CENTER;
Paragraph Symbol = new Paragraph(@"----------oOo----------", fontBold);
Symbol.Alignment = Element.ALIGN_CENTER;
cell12.AddElement(QH1);
cell12.AddElement(QH);
cell12.AddElement(Symbol);
cell12.BorderColor = BaseColor.WHITE;
table1.AddCell(cell11);
table1.AddCell(cell12);

Paragraph TitleReport = new Paragraph(@"TỔNG HỢP COI CHẤM THI", titleFont);
TitleReport.Alignment = Element.ALIGN_CENTER;

Paragraph Info = new Paragraph(string.Format(@"Từ ngày: {0}     đến ngày: {1}        Đơn vị: {2}", DateTime.Now.AddDays(-2).ToString("dd/MM/yyyy"), DateTime.Now.ToString("dd/MM/yyyy"), "Trung tâm quản lý chất lượng"), normalFont);

Info.Alignment = Element.ALIGN_LEFT;
Info.SpacingBefore = 20;
Info.SpacingAfter = 200;

doc.Add(table1);
doc.Add(TitleReport);
doc.Add(Info);

float afterHeaderY = writer.GetVerticalPosition(true);

Rectangle[] COLUMNS = new Rectangle[] {
    new Rectangle(36, 36, 290, afterHeaderY),
    new Rectangle(305, 36, 559, afterHeaderY)
};

PdfContentByte canvas = writer.DirectContent;
ColumnText ct = new ColumnText(canvas);
int side_of_the_page = 0;
ct.SetSimpleColumn(COLUMNS[side_of_the_page]);

PdfPTable table = new PdfPTable(5);
table.AddCell("STT");
table.AddCell("Số Phách");
table.AddCell("Điểm bằng số");
table.AddCell("Điểm bằng chữ");
table.AddCell("Ghi chú");

for (int i = 0; i < 33; i++)
{
    table.AddCell((i + 1).ToString());
    table.AddCell("209292");
    table.AddCell("4");
    table.AddCell("Điểm bằng chữ");
    table.AddCell("");
}
table.HeaderRows = 1;

ct.AddElement(table);
while (ColumnText.HasMoreText(ct.Go()))
{
    if (side_of_the_page == 0)
    {
        side_of_the_page = 1;
        canvas.MoveTo(297.5f, 36);
        canvas.LineTo(297.5f, afterHeaderY);
        canvas.Stroke();
    }
    else
    {
        side_of_the_page = 0;
        doc.NewPage();

        COLUMNS = new Rectangle[] {
            new Rectangle(36, 36, 290, 806),
            new Rectangle(305, 36, 559, 806)
        };
        afterHeaderY = 806;
    }
    ct.SetSimpleColumn(COLUMNS[side_of_the_page]);
}

结果:

标题和内容之间的巨大差距是由于您的设置

Info.SpacingAfter = 200;

如果您不想要那个差距,请相应地减小该值。

【讨论】:

  • @ChungPhạmBáTuấn 您不接受这个答案有什么原因吗?您在使用时遇到问题了吗?
  • 没有。我接受了你的代码。我记得我点击接受(V)但现在我回来了我看不到这个:(
  • @mkl 非常感谢,这是整个网络中唯一使用 iTextSharp 创建 2 列布局的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 2014-05-09
  • 2015-12-21
  • 1970-01-01
相关资源
最近更新 更多