Itextsharp exporting PDF tables and pictures (C #)

Source: Internet
Author: User
Tags set background

This article mainly introduces the simple operation instructions of using ITEXTSHARP to export PDF forms and pictures, the following is the ITextSharp.dll download link

Share Link: http://pan.baidu.com/s/1nuc6glj Password: 3gxw

First, the process

Second, simple example:

1) Create a table instance

Program:

        /// <summary>        ///Create a PDF file/// </summary>        Private voidcreatpdftable () {//Print a PDF table            stringPdfname =string.            Empty; SaveFileDialog Dlg=NewSaveFileDialog (); Dlg. FileName="PDF Table"; Dlg. DefaultExt=". pdf"; Dlg. Filter="Text documents (. pdf) |*.pdf"; if(DLG. ShowDialog () = =DialogResult.OK) {pdfname=dlg.                FileName; FileStream FS=NewFileStream (Pdfname, FileMode.Create);//Create a file streamDocument document =NewDocument (PageSize.A7.Rotate ());//Create File PageSize.A7.Rotate () indicates A7 paper transverse outputPDFWriter PDFWriter = pdfwriter.getinstance (document, FS);//instantiation ofDocument. Open ();//Open FileDocument. ADD (NewParagraph ("1")); Document.               ADD (PDFTable1 ()); //Add a tabledocument.       SetPageSize (PAGESIZE.A6); //A6 paper longitudinal outputDocument. NewPage ();//a new pageDocument. ADD (NewParagraph ("2")); Document.                ADD (PDFTable2 ()); Document. ADD (NewParagraph ("3")); Document.                ADD (PDFTable3 ()); Document.                        Close (); //Close FileFS.            Close (); }        }        /// <summary>        ///CREATE table 1/// </summary>        /// <returns></returns>        Privatepdfptable PDFTable1 () {varTable1 =NewPdfptable (4);//Create a Table instance 4 column            int[] A = {1,2,3,4};//set the column width scaletable1.            Setwidths (a);  for(inti =0; I < -; i++) {table1. Addcell ((i+1). ToString ());//Add Cells            }             returntable1; }        /// <summary>        ///CREATE TABLE 2/// </summary>        /// <returns></returns>        Privatepdfptable PDFTable2 () {//Font Definition            varBfchinese = Basefont.createfont (@"C:\windows\fonts\simkai.ttf", Basefont.identity_h, basefont.not_embedded);//Simkai.ttf            varChfont_12 =NewITextSharp.text.Font (Bfchinese, A); varChfont_10 =NewITextSharp.text.Font (Bfchinese,Ten); varChfont_8 =NewITextSharp.text.Font (Bfchinese,8); varchfont_12_red =NewITextSharp.text.Font (Bfchinese, A, ITextSharp.text.Font.ITALIC, basecolor.red); varTable2 =NewPdfptable (4);//Create a Table instancePdfpcell Cell; Cell=NewPdfpcell (NewPhrase (Convert.ToString (1) , chfont_10)); Cell. HorizontalAlignment=1;//Center input Default 0: Left 1: CenterCell. Colspan =2;//Merge cells Horizontallytable2.            Addcell (cell); Cell=NewPdfpcell (NewPhrase (Convert.ToString (2) , chfont_8)); Cell. HorizontalAlignment=1; Cell. Rowspan=2;//Merge Cells verticallytable2.            Addcell (cell); Cell=NewPdfpcell (NewPhrase (Convert.ToString (3) , chfont_10)); Cell. BackgroundColor= Basecolor.gray;//Set Background colortable2.            Addcell (cell); Cell=NewPdfpcell (NewPhrase (Convert.ToString (4), chfont_12_red));//Set Font Colortable2.            Addcell (cell);  for(inti =0; I < -; i++) {table2. Addcell ((i+1). ToString ());//Add Cells            }              returntable2; }        /// <summary>        ///Add a table to the cell/// </summary>        /// <returns></returns>        Privatepdfptable PDFTable3 () {varTable3 =NewPdfptable (4); int[] A = {1,1,4,1};//set the column width scaleTable3.            Setwidths (a);  for(inti =0; I < -; i++)            {                if(i = =Ten) {Pdfpcell cell=NewPdfpcell (PDFTable2 ());//Add Table 2 to a cellCell. Padding =0;//table and cell spacing are 0;Table3.                    Addcell (cell); Continue; } table3. Addcell ("3");//Add Cells            }            returnTable3; }

Style:

    

2) Create a picture

Program:

 Private voidBtnpdfimage_click (Objectsender, EventArgs e) {            stringPdfname =string.            Empty; SaveFileDialog Dlg=NewSaveFileDialog (); Dlg. FileName="PDF Image"; Dlg. DefaultExt=". pdf"; Dlg. Filter="Text documents (. pdf) |*.pdf"; if(DLG. ShowDialog () = =DialogResult.OK) {pdfname=dlg.                FileName; FileStream FS=NewFileStream (Pdfname, FileMode.Create);//Create a file streamDocument document =NewDocument (PageSize.A5.Rotate ());//Create File PageSize.A5.Rotate () indicates A5 paper transverse outputPDFWriter PDFWriter = pdfwriter.getinstance (document, FS);//instantiation ofDocument. Open ();//Open FileDocument. ADD (AddImage (document));//Add a pictureDocument. ADD (AddImage2 (document));//Add a pictureDocument. Close ();//Close FileFS.            Close (); }        }         /// <summary>        ///PDF Add Picture/// </summary>        /// <returns></returns>        PrivateiTextSharp.text.Image AddImage (document document) {ITextSharp.text.Image Hglogo= ITextSharp.text.Image.GetInstance ("yijing.jpg");  Hglogo.scalepercent (4f); //Picture ProportionsHglogo.setabsoluteposition (40f, document. PAGESIZE.HEIGHT-100F);//iamge Location            returnHglogo; }        /// <summary>        ///PDF Add Image 2/// </summary>        /// <param name= "document" ></param>        /// <returns></returns>        PrivateiTextSharp.text.Image AddImage2 (document document) {ITextSharp.text.Image Hglogo= ITextSharp.text.Image.GetInstance ("yijing.jpg");  Hglogo.scalepercent (12f); //Picture ProportionsHglogo.setabsoluteposition (200f, document. PAGESIZE.HEIGHT-400F);//iamge Location            returnHglogo; }

Style:

Itextsharp exporting PDF tables and pictures (C #)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.