PDF verification and seal through ITEXTSHARP implementation

Source: Internet
Author: User

The recent need to do a PDF automatic audit stamp work, in fact, is to read the PDF, and then the last page to add an audit chapter printed on the picture up. It looks very simple, but in the development process, there are some problems, recorded here.

The main problem is that the rotation of the page and the rotation of the contents of the separate, need to operate separately.

Preparation requires downloading ITextSharp.dll and then adding references

Using Itextsharp.text;

Using ITextSharp.text.pdf;

string path = @ "D:\28727_LOG001_FOLIOLE company Limited_stock_pi";
    
                Create a Pdfreader object Pdfreader reader = new Pdfreader (path + ". pdf"); get document pages int n = reader.
                Numberofpages; Get the size of the first page Rectangle psize = reader.
    
                GetPageSize (1); Float width = psize.
                Width; float height = psize.
    
                Height;
    
                Create a document variable documents document = New file (psize); Create the document generate a physical file PDFWriter writer = pdfwriter.getinstance (document, new FileStream (path + "_approve.pdf", F
    
                Ilemode.openorcreate)); Open document documents.
    
                Open (); Add content Pdfcontentbyte cb = writer.
    
                Directcontent;
                for (int i = 0; i < n;)
                    {i++;
                    Sets the pagsize of the specified page to contain rotation (page rotation degree)Document. SetPageSize (reader.
    
                    Getpagesizewithrotation (i)); Create a new page that takes note of the call to NewPage (), Pdfcontentbyte CB object will empty document by default.
    
                    NewPage (); Gets the rotation of the specified page int rotation = reader.
    
                    Getpagerotation (i); Gets the specified page content of the loaded PDF Pdfimportedpage Page1 = writer.
    
                    Getimportedpage (reader, i);
                            Add a content page to a new page and rotate the degree setting corresponding to the rotation switch (rotation) {Case 90: Cb. Addtemplate (Page1, 0,-1, 1, 0, 0, reader. Getpagesizewithrotation (i).
                            Height);
                        Break Case 180:CB. Addtemplate (Page1,-1, 0, 0,-1, reader. Getpagesizewithrotation (i). Width, reader. Getpagesizewithrotation (i).        

                   Height);
                        Break Case 270:CB. Addtemplate (Page1, 0, 1,-1, 0,Reader. Getpagesizewithrotation (i).
                            Width, 0);
                        Break DEFAULT:CB. Addtemplate (Page1, 1, 0, 0, 1, 0, 0);//equivalent to CB.
                    Addtemplate (Page1, 0,0) break;
                       if (i = = N)//If is the last page to add the specified picture {///rotate the page picture position of different rotation degrees the adjustment of left distance
               int imgleft = 350; if (rotation==90 | |                    
                        rotation==270) {imgleft = 550;}//Create a Picture object
    
                        ITextSharp.text.Image img = iTextSharp.text.Image.GetInstance (new Uri (@ "d:\Lock-icon.png")); Sets the specified size//img for the picture.
    
                        Scaletofit (140F, 320F); Scale//img proportionally.           
    
                        Scalepercent (50); Add the picture to the content page of the designated seat B width C height e bottom F left CB. AddimaGE (IMG, 0, 32F, 32F, 0, 50F, imgleft); Start to increase the text CB.
    
                        Begintext ();
                        Basefont bf = Basefont.createfont (Basefont.helvetica_oblique, basefont.cp1252, basefont.not_embedded); Set font size CB.
    
                        Setfontandsize (BF, 9); Specifies the absolute location of the added text CB.
                        Settextmatrix (Imgleft, 200); Increase the text CB.
    
                        Showtext ("GW INDUSTRIAL LTD"); End CB.
                           
                    Endtext ();
    
                }//Close document Document.
                    
            Close (); The catch (Exception de) {Console.Error.WriteLine (DE).
                message); Console.Error.WriteLine (DE.
            StackTrace); }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/csharp/

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.