PDF document Stamp

Source: Internet
Author: User

Overview

On the last page of the PDF document, in the right place, add a stamp image.

Maven Dependency
<Dependency>    <groupId>Com.itextpdf</groupId>    <Artifactid>Itextpdf</Artifactid>    <version>5.5.7</version></Dependency>
Implementing a class Diagram

byte float float float float): com.itextpdf.text.Image

Creates an Itextpdf image object with the specified content, size, and location, which will be used for the AddImage method.

byte []): Com.itextpdf.text.Image

Creates an image object of Itextpdf with the specified contents, the default size and location (referenced constants defined in the class diagram), which is used for the AddImage method.

void

Adds a picture to the last page of the specified PDF document and writes the result to the specified output stream. The parameter is provides the contents of the original PDF, and the parameter OS provides a stream to write the results.

void

Test: Adds the default picture to the last page of the specified PDF document and writes the result to the specified output stream.

Code Istamperservice
 PackageCn.ljl.javaweb.demo.ckeditor.service;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportOrg.apache.commons.logging.Log;Importorg.apache.commons.logging.LogFactory;ImportCom.itextpdf.text.Image;/*** Set picture, add picture. *@authorLijinlong **/ Public InterfaceIstamperservice { Public Static FinalLog logger = Logfactory.getlog (istamperservice.class); /**Default Width*/    floatDefault_width =100f; /**Default Height*/    floatDefault_height =100f; /**default x-coordinate*/    floatDefault_left =440f; /**default y-coordinate*/    floatDefault_bottom =50f; /*** According to the specified picture data, encapsulated into {@linkImage} object instance. *      * @paramimagecontent * Picture content *@paramWidth * Picture widths *@paramHeight * Picture Heights *@paramLeft * picture x Coordinate *@paramtop * Picture y coordinate*/     PublicImage CreateImage (byte[] imagecontent,floatWidthfloatheight,floatLeftfloatbottom); /*** Use default data to create {@linkImage} object instance. * @paramimagecontent image content. * @return     */     PublicImage CreateImage (byte[] imagecontent); /*** Add Picture .<br/> * Requires first call {@link#setImage (byte[], float, float, float, float)} set Picture;<br/> * Read the PDF content from the specified input stream, add the picture and write to the specified output stream .<br/> * @paramimage specified as the stamp {@linkImage} instance. * @paramis *@paramOS*/     Public voidAddImage (image Image, InputStream is, OutputStream OS); /*** Add default image to Pdf.<br/> * This method is only used for functional testing. * @paramis *@paramOS*/     Public voidTestadddefaultimage (InputStream is, OutputStream OS);}
Stamperserviceimpl

 PackageCn.ljl.javaweb.demo.ckeditor.service;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.MalformedURLException;Importcom.itextpdf.text.BadElementException;Importcom.itextpdf.text.DocumentException;ImportCom.itextpdf.text.Image;ImportCom.itextpdf.text.pdf.PdfContentByte;ImportCom.itextpdf.text.pdf.PdfReader;ImportCom.itextpdf.text.pdf.PdfStamper; Public classStamperserviceimplImplementsIstamperservice {Stamperserviceimpl () {Super(); } @Override PublicImage CreateImage (byte[] imagecontent,floatWidthfloatHeightfloatLeft ,floatbottom) {Image Image=NULL; Try{image=image.getinstance (imagecontent); } Catch(badelementexception e) {e.printstacktrace (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace ();        } image.scaleabsolute (width, height);                Image.setabsoluteposition (left, bottom); returnimage; } @Override PublicImage CreateImage (byte[] imagecontent) {Image Image=createimage (imagecontent, Default_width, Default_height, Default_left, Default_bottom); returnimage; } @Override Public voidAddImage (image Image, InputStream is, OutputStream os) {Pdfreader reader=NULL; Pdfstamper Stamper=NULL; Try{Reader=NewPdfreader (IS); intNOP =reader.getnumberofpages (); Stamper=Newpdfstamper (reader, OS); Pdfcontentbyte content=stamper.getovercontent (NOP);        Content.addimage (image); } Catch(IOException e) {e.printstacktrace (); } Catch(documentexception e) {e.printstacktrace (); } finally {            Try {                if(Stamper! =NULL) Stamper.close (); } Catch(documentexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }            if(Reader! =NULL) Reader.close (); }    }        /**Test Picture*/    Private Static FinalString path_image = "Ckeditor/stamper/signature.gif"; @Override Public voidTestadddefaultimage (InputStream is, OutputStream os) {InputStream Imageis= Stamperserviceimpl.class. getClassLoader (). getResourceAsStream (Path_image); byte[] Imagecontent =NULL; Try {            intLength =imageis.available (); Imagecontent=New byte[length];        Imageis.read (imagecontent); } Catch(IOException e) {e.printstacktrace (); } finally {            Try{imageis.close (); } Catch(IOException e) {e.printstacktrace (); }} Image img=createimage (imagecontent);    AddImage (IMG, is, OS); }}
Seal pictures

The stamped image needs to be handled in a special way (should be handled in terms of transparency), otherwise it will overwrite the content below. Here is a picture for testing:

PDF document Stamp

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.