[ASP.NET] iTextSharp產生PDF檔...Part_4(建立表格)

  • 32065
  • 0
  • 2012-07-28

摘要:[ASP.NET] iTextSharp產生PDF檔...Part_4(建立表格)

下午剛好有時間順便作了 PDF 表格

使用 iTextSharp 產生表格是十分直覺且容易的,類似CSS的寫法

在建立 table 時,可以很輕易用欄位相對寬度做設定,也可以給絕對寬度,或者單純的給予欄位數做平均等分


PdfPTable table = new PdfPTable(4);

PdfPTable 表格裡面,每一格叫做 cell ,因此在塞資料時,必須注意填寫方式是由左而右,由上而下

PdfpCell 有合併欄位的功能Colspan ,也有合併列的功能 Rowspan , 也可用 HorizontalAlignment 來做置中/靠右/靠左...等等

也可用Rotation文字翻轉屬性,需為 90 倍數,要不然會出錯

分享給各位網友~多多指教!


/// 
/// PDF產生表格
/// 
private void GetPDF_3()
{
    Document doc = new Document(PageSize.A4, 50, 50, 80, 50); // 設定PageSize, Margin, left, right, top, bottom
    MemoryStream ms = new MemoryStream();
    PdfWriter pw = PdfWriter.GetInstance(doc, ms);

    ////    字型設定
    // 在PDF檔案內容中要顯示中文,最重要的是字型設定,如果沒有正確設定中文字型,會造成中文無法顯示的問題。
    // 首先設定基本字型:kaiu.ttf 是作業系統系統提供的標楷體字型,IDENTITY_H 是指編碼(The Unicode encoding with horizontal writing),及是否要將字型嵌入PDF 檔中。
    // 再來針對基本字型做變化,例如Font Size、粗體斜體以及顏色等。當然你也可以採用其他中文字體字型。
    BaseFont bfChinese = BaseFont.CreateFont("C:\\Windows\\Fonts\\kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font ChFont = new Font(bfChinese, 12);
    Font ChFont_green = new Font(bfChinese, 40, Font.NORMAL, BaseColor.GREEN);
    Font ChFont_msg = new Font(bfChinese, 12, Font.ITALIC, BaseColor.RED);

    // 開啟檔案寫入內容後,將檔案關閉。
    doc.Open();

    // 產生表格 -- START
    // 建立4個欄位表格之相對寬度
    PdfPTable pt = new PdfPTable(new float[] { 2, 1, 1, 3 });
    // 表格總寬
    pt.TotalWidth = 400f;
    pt.LockedWidth = true;
    // 塞入資料 -- START
    // 設定表頭
    PdfPCell header = new PdfPCell(new Phrase("Y2J Header", new Font(Font.FontFamily.HELVETICA, 30f, Font.BOLD)));
    header.Colspan = 4;
    header.HorizontalAlignment = Element.ALIGN_CENTER;// 表頭內文置中
    pt.AddCell(header);
    
    pt.AddCell("R_2/C_1");
    pt.AddCell("R_2/C_2");
    pt.AddCell("R_2/C_3");
    pt.AddCell("R_2/C_4");
    
    PdfPCell itemname = new PdfPCell(new Phrase("測試123", ChFont));
    itemname.Colspan = 1;
    pt.AddCell(itemname);
    
    PdfPCell content = new PdfPCell(new Phrase("Y2J_Y2J_Y2J", ChFont));
    content.Colspan = 3;
    content.HorizontalAlignment = Element.ALIGN_RIGHT;// 內文靠右
    pt.AddCell(content);
    
    PdfPCell rows = new PdfPCell(new Phrase("ROW_4", ChFont_green));
    rows.Rowspan = 3;
    pt.AddCell(rows);

    for (int i = 0; i <= 3; i++)
    {
        pt.AddCell("Cell " + i.ToString() + "1");
        pt.AddCell("Cell " + i.ToString() + "2");
        pt.AddCell("Cell " + i.ToString() + "3");
    }

    //pt.AddCell("Row 1");
    PdfPCell left = new PdfPCell(new Paragraph("Y2J:90"));
    left.Rotation = 90;
    pt.AddCell(left);

    PdfPCell row = new PdfPCell(new Phrase("合併3行3列", ChFont));

    row.Rowspan = 3;
    row.Colspan = 3;
    pt.AddCell(row);

    // Rotation文字翻轉屬性,需為 90 倍數,要不然會出錯
    PdfPCell middle_left = new PdfPCell(new Paragraph("Y2J:180"));
    middle_left.Rotation = 180;
    pt.AddCell(middle_left);

    PdfPCell middle_right = new PdfPCell(new Paragraph("Y2J:270"));
    middle_right.Rotation = 270; // -90跟270是相同
    pt.AddCell(middle_right);

    PdfPCell right = new PdfPCell(new Paragraph("Y2J:360"));
    right.Rotation = 360; // 360為預設,可不寫
    pt.AddCell(right);

    doc.Add(pt);
    // 塞入資料 -- END
    doc.Close();

    // 在Client端顯示PDF檔,讓USER可以下載
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=Y2J.pdf");
    Response.ContentType = "application/octet-stream";
    Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
    Response.OutputStream.Flush();
    Response.OutputStream.Close();
    Response.Flush();
    Response.End();
}

 

 






Y2J's Life:http://kimenyeh.blogspot.tw/