Java to add a clear watermark image to the PDF, need a third package jar package: Itextpdf-5.1.3.jar

Source: Internet
Author: User

Import Java.io.File;
Import Java.io.FileOutputStream;
Import Com.itextpdf.text.Image;
Import Com.itextpdf.text.pdf.PdfArray;
Import Com.itextpdf.text.pdf.PdfContentByte;
Import Com.itextpdf.text.pdf.PdfDictionary;
Import Com.itextpdf.text.pdf.PdfName;
Import Com.itextpdf.text.pdf.PdfObject;
Import Com.itextpdf.text.pdf.PdfReader;
Import Com.itextpdf.text.pdf.PdfStamper;


public class testimage{

Public Testimage () {
}

public static void Addmark (String inpdffile,string outpdffile) {

Pdfreader reader = new Pdfreader (inpdffile, "PDF". GetBytes ());
Pdfstamper stamp = new Pdfstamper (reader, New FileOutputStream (Outpdffile));

Start Signature Insert Watermark
Image img = image.getinstance ("C:/users/tangwei/desktop/sunkai.png");//Watermark Picture Path
Img.setabsoluteposition (50, 50);//Set the location of the watermark picture

Watermark image size is inconsistent with the size of the PDF, you can directly set the image size to insert the watermark,

Do not scale the watermark image in advance, it is clearer after inserting

Imgluqi.scaleabsolute (200, 100);

Pdfcontentbyte under = stamp.getundercontent (1);//Get PDF Current Page

Under.addimage (IMG);

Stamp.close ();//signature complete, close stream output

File Tempfile = new file (inpdffile);
if (tempfile.exists ()) {
Tempfile.delete ();
}

}

Public static void Main (string[] args) {

Addmark ("D:/123.pdf", "d:/123_signatrue");

}

}

This article is from the "13160828" blog, please be sure to keep this source http://13170828.blog.51cto.com/13160828/1951771

Java to add a clear watermark image to the PDF, need a third package jar package: Itextpdf-5.1.3.jar

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.