Itextsharp PDF generation problem set

Source: Internet
Author: User

 

Recently, I want to create a PDF Project generated by Asp.net. I found that the DLL generated by itextsharp on the Internet is still good,

I encountered a lot of problems during development and found a lot of information on the Internet. I summarized a knowledge point here and made some comments on the key points to help my friends in the park.

 

Using system; using system. web; using system. io; using itextsharp. text; using itextsharp.text.pdf; public class PDF: ihttphandler {public void processrequest (httpcontext context) {rectangle rect = new rectangle (8.5f * 72, 11*72 ); // set the paper size by yourself. Document document = new document (rect, 20, 20, 80, 20); // document = new document (pagesize. a4); // you can specify the paper size as string pdffile = context. server. mappath ("sampleath"); extends writer. getinstance (document, new filestream (pdffile, filemode. create); document. open (); // set the document background image string imgpath = context. server. mappath (".. /images/2012/bg2.png "); image = image. getinstance (imgpath); image. setabsoluteposition (0, 0); image. alignment = image. align_undefined; image. scaleabsolute (image. width, image. height); document. add (image); // set the document attributes in PDF. addauthor ("junny"); document. addcreationdate (); document. addcreator ("SBS. INC "); document. addheader ("email", "junny_sa@163.com"); document. addkeywords ("pdf"); document. addproducer (); document. addsubject ("this is subject"); document. addtitle ("this is Title"); // set the Chinese font basefont = createchinesefont (); // set the font type itextsharp. text. font titlefont = new itextsharp. text. font (basefont, 18, Font. bold); itextsharp. text. font normalfont = new itextsharp. text. font (basefont, 12), normalboldfont = new itextsharp. text. font (basefont, 10, Font. bold); itextsharp. text. font normalredfont = new itextsharp. text. font (basefont, 10, Font. normal | font. bold, basecolor. red); // set a blank line feed float normallineheight = 25f; paragraph pblank = new paragraph ("", normalfont); pblank. leading = normallineheight; document. add (pblank); float [] widths = new float [] {12f, 140f, 35f}; float padding = 3f; basecolor bgcolor = new basecolor (153,204,255 ); // set the background color pdfptable table = new pdfptable (3); // set it to a three-column table. setwidths (widths );
Table. addcell (createcellheader ("Create PDF", titlefont, 3, padding, padding, bgcolor); string [] id_arr = {"1", "2", "3 "}; string [] name_arr = {"junny", "kendy", "Sally"}; string [] phone_arr = {"178-894-7893 ", "378-822-7289", "221-842-9874"}; table. addcell (createcell ("ID", normalfont, false, 1, 0, padding, padding); table. addcell (createcell ("name", normalfont, false, 0, 0, padding, padding); table. addcell (createcell ("phone", normalfont, false, 0, 0, padding, padding); For (INT I = 0; I <3; I ++) {table. addcell (createcell (id_arr [I], normalfont, true, 0, 0, padding, padding); table. addcell (createcell (name_arr [I], normalfont, false, 0, 0, padding, padding); table. addcell (createcell (phone_arr [I], normalfont, false, 0, 0, padding, padding);} document. add (table); document. close ();}}

 

Createcell Method
/// <Summary> /// create a table row /// </Summary> /// <Param name = "TXT"> text </param> /// <Param name = "txtfont"> font </param> // <Param name = "image"> Add image </param> /// <Param name = "align"> alignment </param> /// <Param name = "colspan"> Cross-row count </param> /// <Param name = "padtop"> top padding </param> /// <Param name = "padbottom"> bottom padding </param> /// <returns> table row </returns> Public static partition pcell createcell (string txt, font txtfont, bool image, int align, int colspan, float padtop, float padbottom) {export pcell cell = new export pcell (); phrase pH = new phrase (txt, txtfont ); // here is an example of adding an image. By the way, if (image) {image IMG = image. getinstance (httpcontext. current. server. mappath (".. /images/view_add.png "); chunk ck = new chunk (IMG, 4,-4); // you can set the offset value for the image. add (CK);} cell. addelement (ph); If (padtop> 0) {Cell. paddingtop = padtop;} If (padbottom> 0) {Cell. paddingbottom = padbottom;} If (colspan> 0) {Cell. colspan = colspan;} // cell. border = 0; set table line cell. horizontalalignment = align; return cell ;}

 

 

Createcellheader Method
/// <Summary> /// create a table row /// </Summary> /// <Param name = "TXT"> text </param> /// <Param name = "txtfont"> font </param> /// <Param name = "colspan"> Cross-row count </param> /// <Param name = "padtop"> top padding </param> /// <Param name = "padbottom"> bottom padding </param> /// <Param name = "bgcolor"> background color </param> // /<returns> table row </returns> Public static partition pcell createcellheader (string txt, font txtfont, int colspan, float padtop, float padbottom, basecolor bgcolor) {symbol pcell cell = new symbol pcell (new phrase (txt, txtfont); If (padtop> 0) {Cell. paddingtop = padtop;} If (padbottom> 0) {Cell. paddingbottom = padbottom;} If (colspan> 0) {Cell. colspan = colspan;} // cell. border = 0; cell. horizontalalignment = element. align_center; // 0 = left, 1 = centre, 2 = right cell. verticalalignment = element. align_middle; cell. backgroundcolor = bgcolor; return cell ;}

 

 

// Set the Chinese font

Public static basefont createchinesefont () {* // set the boldface type here, return basefont. createfont (@ "C: \ WINDOWS \ fonts \ simhei. TTF ", basefont. identity_h, basefont. not_embedded );}

 

Example:

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.