ITextSharp exports PDF tables and images (C #),
This article describes how to use ITextSharp to export PDF tables and images. The following is the download link of ITextSharp. dll.
Share link: http://pan.baidu.com/s/1nuc6glj password: Drawing XW
I. Process
Ii. Simple Example:
1) create a table instance
Program:
/// <Summary> /// create a PDF file /// </summary> private void CreatPDFTable () {// print the PDF table string using name = string. empty; SaveFileDialog dlg = new SaveFileDialog (); dlg. fileName = "PDF table"; dlg. defaultExt = ". pdf "; dlg. filter = "Text documents (items) | *. pdf "; if (dlg. showDialog () = DialogResult. OK) {rule name = dlg. fileName; FileStream fs = new FileStream (partition name, FileMode. create); // Create a file stream Document document = new Document (PageSize. a7.Rotate (); // create the file PageSize. a7.Rotate () indicates the A7 paper horizontal output using writer = Using writer. getInstance (document, fs); // instantiate document. open (); // Open the file document. add (new Paragraph ("1"); document. add (PDFTable1 (); // Add Table document. setPageSize (PageSize. a6); // A6 vertical output document. newPage (); // create a new document. add (new Paragraph ("2"); document. add (PDFTable2 (); document. add (new Paragraph ("3"); document. add (PDFTable3 (); document. close (); // Close the file fs. close () ;}/// <summary> // create table 1 /// </summary> /// <returns> </returns> private PdfPTable PDFTable1 () {var table1 = new PdfPTable (4); // create table instance 4 columns int [] a = {1, 2, 3, 4 }; // set the ratio of column width to table1.SetWidths (a); for (int I = 0; I <16; I ++) {table1.AddCell (I + 1 ). toString (); // Add a cell} return table1 ;} /// <summary> /// create table 2 /// </summary> /// <returns> </returns> private PdfPTable PDFTable2 () {// font definition var bfchinese = BaseFont. createFont (@ "c: \ windows \ fonts \ simkai. ttf ", BaseFont. IDENTITY_H, BaseFont. NOT_EMBEDDED); // simkai. ttf var ChFont_12 = new iTextSharp. text. font (bfchinese, 12); var ChFont_10 = new iTextSharp. text. font (bfchinese, 10); var ChFont_8 = new iTextSharp. text. font (bfchinese, 8); var ChFont_12_red = new iTextSharp. text. font (bfchinese, 12, iTextSharp. text. font. ITALIC, BaseColor. RED); var table2 = new PdfPTable (4); // create a table instance using pcell cell; cell = new using pcell (new Phrase (Convert. toString (1), ChFont_10); cell. horizontalAlignment = 1; // The Center input defaults to 0: Center 1: center cell. colspan = 2; // horizontally merge the cells table2.AddCell (cell); cell = new PdfPCell (new Phrase (Convert. toString (2), ChFont_8); cell. horizontalAlignment = 1; cell. rowspan = 2; // vertically merge the cells table2.AddCell (cell); cell = new PdfPCell (new Phrase (Convert. toString (3), ChFont_10); cell. backgroundColor = BaseColor. GRAY; // set the background color table2.AddCell (cell); cell = new PdfPCell (new Phrase (Convert. toString (4), ChFont_12_red); // set the font color table2.AddCell (cell); for (int I = 0; I <16; I ++) {table2.AddCell (I + 1 ). toString (); // Add a cell} return table2 ;} /// <summary> /// Add a table to the cell /// </summary> /// <returns> </returns> private PdfPTable PDFTable3 () {var table3 = new PdfPTable (4); int [] a = {1, 1, 4, 1}; // set the column width ratio table3.SetWidths (); for (int I = 0; I <16; I ++) {if (I = 10) {export pcell cell = new export pcell (PDFTable2 ()); // Add Table 2 cells to cells. padding = 0; // The distance between the table and cells is 0; table3.AddCell (cell); continue;} table3.AddCell ("3"); // Add cells} return table3 ;}
Style:
2) create an image
Program:
Private void btn1_image_click (object sender, EventArgs e) {string region name = string. empty; SaveFileDialog dlg = new SaveFileDialog (); dlg. fileName = "PDF image"; dlg. defaultExt = ". pdf "; dlg. filter = "Text documents (items) | *. pdf "; if (dlg. showDialog () = DialogResult. OK) {rule name = dlg. fileName; FileStream fs = new FileStream (partition name, FileMode. create); // Create a file stream Document document = new Document (PageSize. a5.Rotate (); // create the file PageSize. a5.Rotate () indicates the horizontal output of A5 paper using writer = Using writer. getInstance (document, fs); // instantiate document. open (); // Open the file document. add (addImage (document); // Add an image document. add (addImage2 (document); // Add an image document. close (); // Close the file fs. close () ;}/// <summary> // Add an image in PDF /// </summary> /// <returns> </returns> private iTextSharp. text. image addImage (Document document) {iTextSharp. text. image hgLogo = iTextSharp. text. image. getInstance ("yijing.jpg"); hgLogo. scalePercent (4f); // picture ratio hgLogo. setAbsolutePosition (40f, document. pageSize. height-100f); // iamge position return hgLogo ;} /// <summary> /// add Image 2 in PDF /// </summary> /// <param name = "document"> </param> /// <returns> </returns> private iTextSharp. text. image addImage2 (Document document) {iTextSharp. text. image hgLogo = iTextSharp. text. image. getInstance ("yijing.jpg"); hgLogo. scalePercent (12f); // picture ratio hgLogo. setAbsolutePosition (200f, document. pageSize. height-400f); // iamge position return hgLogo ;}
Style: