New Itextsharp PDF Code

Source: Internet
Author: User

usingSystem;usingItextsharp.text;usingiTextSharp.text.pdf;usingSystem.IO;namespaceitextsharp.demo{/**//// <summary>///Making a document with a header containing ' page X of y ' and with a watermark on every page./// </summary>     Public classpagenumberswatermark:ipdfpageevent{/**//** An Image of the goes in the header.*/ PublicImage headerimage;/**//** the headertable.*/ Publicpdfptable table;/**//** The Graphic state*/ Publicpdfgstate gstate;/**//** A template that would hold the total number of pages.*/ Publicpdftemplate TPL;/**//** The font that would be used.*/ PublicBasefont Helv; PublicPagenumberswatermark () {Try{//Step 1:creating the documentDocument doc =NewDocument (PAGESIZE.A4, -, -, -, the);//Step 2:creating the writerPDFWriter writer= Pdfwriter.getinstance (Doc,NewFileStream ("pagenumberswatermark.pdf", FileMode.Create));//Step 3:initialisations + Opening the documentWriter. Pageevent =NewPagenumberswatermark ();d OC. Open ();//Step 4:adding Content                stringText ="some padding text"; for(intK =0; K <Ten; ++k) Text+=text; Paragraph P=NewParagraph (text);p. Alignment=(element.align_justified);d oc. ADD (p);//Step 5:closing the documentDoc. Close ();}Catch(Exception e) {System.Diagnostics.Debug.WriteLine (e.stacktrace);}} Ipdfpageevent Members#regionIpdfpageevent Members Public voidOnOpenDocument (pdfwriter writer, document document) {Try{//initialization of the Header tableHeaderimage = Image.getinstance ("logo.gif"); Table=NewPdfptable (2); Phrase P=NewPhrase (); Chunk ck=NewChunk ("lowagie.com\n",NewFont (Font.times_roman, -, Font.bolditalic, Color.Blue)); P.add (CK); CK=NewChunk ("Ghent\nbelgium",NewFont (Font.helvetica, A, Font.normal, Color.dark_gray)); P.add (CK); table. Defaultcell.backgroundcolor=(color.yellow); table. Defaultcell.borderwidth=(0); table. Addcell (P); table. Defaultcell.horizontalalignment=(element.align_right); table. Addcell (NewPhrase (NewChunk (Headerimage,0,0)));//initialization of the Graphic stateGstate =Newpdfgstate (); gstate. Fillopacity=(0.3f); gstate. Strokeopacity=(0.3f);//initialization of the templateTPL = writer. Directcontent.createtemplate ( -, -); TPL. BoundingBox=(NewRectangle (- -, - -, -, -));//initialization of the fontHelv = Basefont.createfont ("Helvetica", Basefont.winansi,false);}Catch(Exception e) {Throwe;}} Public voidOnCloseDocument (pdfwriter writer, document document) {TPL. Begintext (); TPL. Setfontandsize (Helv, A); TPL. Settextmatrix (0,0); TPL. Showtext (""+ (writer. PageNumber-1) ); TPL. Endtext ();} Public voidOnparagraph (pdfwriter writer, document document,floatparagraphposition) {//Todo:add pagenumberswatermark.onparagraph Implementation        } Public voidOnEndPage (pdfwriter writer, document document) {Pdfcontentbyte CB=writer. DIRECTCONTENT;CB. SaveState ();//Write the headertableTable. Totalwidth= (document. Right-document. left), table. Writeselectedrows (0, -1, document. Left, document. Pagesize.height- -, CB);//Compose the footerString Text ="Page"+ writer. PageNumber +" of";floatTextSize = Helv. Getwidthpoint (Text, A);floatTextbase = document. Bottom- -; CB. Begintext (); CB. Setfontandsize (Helv, A);//for Odd pagenumbers, show the footer at the            if(writer. PageNumber &1) ==1) {cb. Settextmatrix (document. Left, textbase); CB. Showtext (text); CB. Endtext (); CB. Addtemplate (TPL, document. Left+textSize, textbase);}//for even numbers, show the footer on the right            Else{floatadjust = Helv. Getwidthpoint ("0", A); CB. Settextmatrix (document. Right-TextSize-adjust, textbase); CB. Showtext (text); CB. Endtext (); CB. Addtemplate (TPL, document. Right-adjust, textbase);} Cb. SaveState ();//draw a Rectangle around the pageCB. Setcolorstroke (Color.orange); CB. Setlinewidth (2); CB. Rectangle ( -, -, document. Pagesize.width- +, document. Pagesize.height- +); CB. Stroke (); CB. Restorestate ();//starting on page 3, a watermark with an Image of is made transparent            if(writer. PageNumber >=3) {cb. Setgstate (gstate); CB. Setcolorfill (color.red); CB. Begintext (); CB. Setfontandsize (Helv, -); CB. Showtextaligned (Element.align_center,"Watermark Opacity"+ writer. PageNumber, document. Pagesize.width/2, document. Pagesize.height/2, $); CB. Endtext ();Try{cb. AddImage (Headerimage, Headerimage.width,0,0, Headerimage.height, the, the);}Catch(Exception e) {Throwe;} Cb. Restorestate ();}} Public voidOnsection (pdfwriter writer, document document,floatParagraphposition,intdepth, Paragraph title) {//Todo:add pagenumberswatermark.onsection Implementation        } Public voidOnsectionend (pdfwriter writer, document document,floatparagraphposition) {//Todo:add pagenumberswatermark.onsectionend Implementation        } Public voidOnparagraphend (pdfwriter writer, document document,floatparagraphposition) {//Todo:add pagenumberswatermark.onparagraphend Implementation        } Public voidOngenerictag (pdfwriter writer, document document, Rectangle rect,stringtext) {//Todo:add Pagenumberswatermark.ongenerictag Implementation        } Public voidOnchapterend (pdfwriter writer, document document,floatparagraphposition) {//Todo:add pagenumberswatermark.onchapterend Implementation        } Public voidOnchapter (pdfwriter writer, document document,floatParagraphposition, Paragraph title) {//Todo:add Pagenumberswatermark.onchapter Implementation        } Public voidOnStartPage (pdfwriter writer, document document) {if(writer. PageNumber <3) {Pdfcontentbyte CB=writer. DIRECTCONTENTUNDER;CB. SaveState (); CB. Setcolorfill (Color.pink); CB. Begintext (); CB. Setfontandsize (Helv, -); CB. Showtextaligned (Element.align_center,"My Watermark under"+ writer. PageNumber, document. Pagesize.width/2, document. Pagesize.height/2, $); CB. Endtext (); CB. Restorestate ();}}#endregion}}

New Itextsharp PDF Code

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.