Reputation: 15
I have created pdf file with itextsharp and I create Header ok but when I create table with two column. I don't have spacing from Header to my two column
The code
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);
Upvotes: 2
Views: 1255
Reputation: 96064
The cause of the issue is that you mix automatic layout by iText (when you add the document header and title using doc.Add
) and manual layout (when you add the content using a ColumnText
).
The automatic layout by iText does not take any content added by manual layout into account. Thus, you have to take the content added via automatic layout into account for your manual layout (probably it would even be easier to use manual layout for everything).
Furthermore it is a bit surprising that you add the document title after all the content. Given enough content this pushes your title onto a page beyond the first!
And your while (paragraphs < 28)
does not make any sense as in the first run through the while
body you immediately push that variable beyond 28 in the inner loop for (int i = 0; i < 33; i++) { ...; ++paragraphs; }
.
As in your code you add the header (table1
, TitleReport
, Info
) only once, I assume you want to use this header on the first page only.
I changed your code by:
adding the header to the Document
first,
requesting the current y position from the PdfWriter
,
float afterHeaderY = writer.GetVerticalPosition(true);
assuming the PdfWriter
being named writer
,
using this y position as the top y coordinate of the columns and the center line on the first page reverting to the hard coded 806 on later pages, and
removing the while
loop and code in comments.
The new code:
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]);
}
The result:
The big gap between header and content is due to your setting
Info.SpacingAfter = 200;
If you don't want that gap, reduce the value accordingly.
Upvotes: 1