PDF Add Watermark Logo This demand scenario is really very few, sometimes some sales documents to generate a PDF add a watermark logo, do a simple anti-counterfeiting effect, although actually not much effect, but the product manager said to, Balabala ... Omit 10,000 words.
The following will be the source of sharing to the ape friends, useful on the Looklook, no use to shift the line of sight.
First, the effect shows
Watermark-Less PDF:
Add a watermark after the PDF:
Here the effect may not be very obvious, the need for ape friends can directly download the following source code compression package, which has added a watermark after the PDF file.
Second, the source code download
http://download.csdn.net/detail/u013142781/9421432
Inside the Pdf_project.zip is the source code, the source environment Eclipse+maven
Iii. Description of the case
Engineering needs to add Itextpdf package dependencies, maven dependencies are:
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.3</version></dependency>
In addition to dependency, a class is Pdfaddwatermark.java, and its code is as follows:
PackageCom.luo.pdf;ImportCom.itextpdf.text.Image;ImportCom.itextpdf.text.pdf.PdfContentByte;ImportCom.itextpdf.text.pdf.PdfGState;ImportCom.itextpdf.text.pdf.PdfReader;ImportCom.itextpdf.text.pdf.PdfStamper;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.util.Random; Public class pdfaddwatermark { Static Final floatIamge_height = theF//Limit the height of the watermark image Static Final floatIamge_width = theF//Limit the width of the watermark picture /** * Add watermark to PDF file * @param file to watermark the original PDF file * @throws Exception */ Public Static void Addpdfmark(File file)throwsException {//If it is a Web project, you should get the logo from the Web project //String Markimagepath = //Request.getsession (). Getservletcontext (). Getrealpath ("/") + //"resources/images/logo.jpg";String Markimagepath ="C:/users/luoguohui/desktop/logo.jpg";//Watermark Picture PathPdfreader reader =NewPdfreader (File.getpath (),"PDF". GetBytes ());//If it is a Web project, the direct download should be placed in the response stream //Pdfstamper stamp = new Pdfstamper (reader, //Response.getoutputstream ()); //pdf file after adding watermarksPdfstamper stamp =NewPdfstamper (Reader,NewFileOutputStream ("C:/users/luoguohui/desktop/afteraddpdfmark.pdf"));intPageSize = Reader.getnumberofpages ();floatPageHeight = Reader.getpagesize (1). GetHeight ();floatPageWidth = Reader.getpagesize (1). GetWidth ();Try{//Every two lines show two left one, one on the right intLineNum = (int) (Pageheight/iamge_height);//number of rows intMiddley = (int) PageWidth/2; for(inti =1; I <= pageSize; i++) { for(intj =0, k =0; J < LineNum; j = j +2, k++) {Random random =NewRandom (); Image img = image.getinstance (markimagepath);//Insert WatermarkImg.scaleabsolute (Iamge_width, Iamge_height *184/455); Img.setalignment (image.underlying);//Under the word intTruey; while(true) {Truey = Random.nextint (Middley);if(Truey > Iamge_width/2&& Truey < (middley-iamge_width)) { Break; }} img.setabsoluteposition (Truey, J * iamge_height + (float) Random.nextint (int) iamge_height)-(k%2) *Ten);//Location of WatermarkImg.setrotationdegrees (Random.nextint ( the));//Rotation anglePdfcontentbyte under = Stamp.getundercontent (i); Pdfgstate GS =NewPdfgstate (); Gs.setfillopacity (0.3f);//Set Transparency to 0.3Under.setgstate (GS); Under.addimage (IMG); while(true) {Truey = Random.nextint (Middley) + Middley;if(Truey > Middley + iamge_width/2&& Truey < (2* middley-iamge_width)) { Break; }} img.setabsoluteposition (Truey, J * iamge_height + (float) Random.nextint (int) iamge_height)-(k%2) *Ten);//Location of WatermarkImg.setrotationdegrees (Random.nextint ( the));//Rotation angleUnder.addimage (IMG); } } }Catch(Exception e) {ThrowE }finally{Stamp.close ();//OffReader.close (); } } Public Static void Main(string[] args)throwsException {//pdf file before adding a watermarkFile File =NewFile ("C:/users/luoguohui/desktop/beforeaddpdfmark.pdf"); Addpdfmark (file); }}
Here it is necessary to explain the above code is: In fact, it is in the PDF to add the image logo, but these image logo will not obscure the text, and there is a certain degree of transparency. In addition the logo randomly rotates a certain angle. In addition, its appearance, according to the height of the current page to calculate how many lines of a page logo, each line to the left one to the right, and then its position up and down around a certain range of random. Code implementation So, if there is nothing to understand can leave a message.
Add security Watermark logo to PDF file (download with project source)