2017-05-19 67 views
1

我已經用itextsharp創建了pdf文件,並且創建了標題確定但是當我創建包含兩列的表時。我沒有從頭間距我的兩列Itextsharp包含多列的標題間距

Images itextsharp

代碼

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

+2

請顯示關鍵代碼。 – mkl

+0

我的代碼在這裏:https://gist.github.com/anonymous/4ad2bf982a84342ae3b4d5ef527829c5 –

+0

儘可能多的堆棧溢出問題應該是有道理的,而不需要諮詢外部資源。因此,我將你的代碼複製到你的問題中。 – mkl

回答

0

問題的原因是,你混的自動佈局由iText(當您使用doc.Add添加文檔標題和標題時)和手動佈局(當您使用ColumnText添加內容時)。

iText的自動佈局不會將通過手動佈局添加的任何內容考慮在內。因此,必須將通過自動佈局添加的內容考慮到你的手動佈局(可能它甚至會更容易使用手動佈局來實現)。

此外,您在所有內容之後添加文檔標題有點令人驚訝。給定足夠的內容,這會將你的標題推到第一頁以外的頁面上!

而您的while (paragraphs < 28)沒有任何意義,因爲在第一次運行while正文時,您立即將該變量推到內部循環for (int i = 0; i < 33; i++) { ...; ++paragraphs; }以上的28以上。

由於在您的代碼中只添加標頭(table1,TitleReport, Info),我假設您只想在第一頁上使用此標頭。

我通過改變您的代碼:

  • 添加報頭添加到Document第一,

  • 請求來自PdfWriter當前ÿ位置,

    float afterHeaderY = writer.GetVerticalPosition(true); 
    

    假設PdfWriter被命名爲writer

  • 使用該ý位置作爲頂部ÿ列的座標和第一頁上的中心線恢復到上後頁的硬編碼806和

  • 除去while環和代碼在評論中。

新代碼:

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

結果:

Screenshot

標題和內容之間的巨大差距是由於你設置

Info.SpacingAfter = 200; 

如果你不想要那個差距,相應地減少價值。

+0

感謝您的幫助。我會更換我的代碼;) –

+0

@ChungPhạmBáTuấn請問您有沒有接受此答案的原因?使用它遇到麻煩了嗎? – mkl

+1

不,我接受你的代碼。我記得我點擊接受(V),但現在我復出,我看不到這:( –