Itext 5.3.0 to add (text and picture) watermarks to PDF files

Source: Internet
Author: User
Tags log log

In the newer version of Itext, there is still a problem with the support of Chinese, the information obtained on the network and the various attempts, the font file XX. TTF into the project, and then loaded into the basefont, feasible. As follows:
basefont font = basefont.createfont (path + msyh). TTF ", Basefont.identity_h, basefont.embedded);

Jar Package Required:

The jar package under the Itext-5.3.0.zip downloaded from the website

Itextpdf-5.3.0.jar

Itext-xtra-5.3.0.jar

Extension jar packages that are not required for Chinese support are implemented from the loading font file mentioned above to Basefont;

The specific watermark code is as follows:

Package com.sslinm.tools;

Import Java.io.FileOutputStream; /** * "Function Description: To add a watermark to the PDF, (text watermark and image watermark)" * "Features Detailed description: Detailed description of the function" * * @author "lfssay" * @see "Related class/method" * @version "class version number, 20 13-8-20 11:22:21 "* @since" product/Module Version "*/public class Pdfaddwatermark {static log log = Logfactory.getlog (Pdfaddwa

    Termark.class); public static void Main (string[] args) throws Documentexception, IOException {new Pdfaddwatermark (). Addwatermark (
    "E:/tt/1.pdf", "E:/tt/129.pdf", "National Library Possession", "e:/tt/1.jpg", 400, 800, 200, 800);            /** * * "feature description: Add picture and Text watermark" "Feature detailed Description: Feature description" * * @see "class, Class # method, Class # member" * @param srcfile * Watermark File * @param destfile * Add watermark to store address * @param text * Watermark content *            Param imgfile * Watermark picture file * @param textWidth * Text Horizontal axis * @param textHeight *            Text ordinate * @param imgwidth * Picture Horizontal Axis * @param imgheight *Image ordinate * @throws IOException * @throws documentexception/public void Addwatermark (String srcfile, Str ing destfile, string text, string imgfile, int textWidth, int textHeight, int imgwidth, int imgheight) throws
        IOException, Documentexception {//Pending watermark file Pdfreader reader = new Pdfreader (srcfile);

        Add the watermark to the file Pdfstamper Stamper = new Pdfstamper (reader, New FileOutputStream (DestFile));
        int total = Reader.getnumberofpages () + 1;
        Pdfcontentbyte content;
        String path = This.getclass (). GetResource ("/"). GetPath ();
        Set font//Basefont base = Basefont.createfont ("Stsong-light", "unigb-ucs2-h",//basefont.embedded); Load the library to complete the creation of the font basefont font = basefont.createfont (path + msyh).
        TTF ", Basefont.identity_h, basefont.embedded);
        Basefont base2 = Basefont.createfont (Basefont.helvetica,//Basefont.winansi, basefont.embedded); Watermark Text String watertext = text;
        Image image = null; if (!
            Stringutils.isblank (Imgfile)) {image = Image.getinstance (imgfile);
            Image.setabsoluteposition (ImgWidth, imgheight);
        Set the display size of the picture Image.scaletofit (100, 125); Int J = Watertext.length ();
        Literal length char c = 0;  int high = 0;//Height//loop inserts a watermark for (int i = 1; i < total; i++) {//watermark start higher
            = 50; Watermark above Previous text             content = stamper.getovercontent (i)
            ;
            if (image!= null) {content.addimage (image); } if (!
                Stringutils.isblank (text)) {//Start content.begintext ();
                Set the color default to Blue Content.setcolorfill (Basecolor.blue);
                Content.setcolorfill (Color.gray); setting font and font size Content.setfontandSize (font, 38);
                Set starting position//Content.settextmatrix (400, 880);
                Content.settextmatrix (TextWidth, textHeight);
                Start writing watermark content.showtextaligned (element.align_left, text, TextWidth, TextHeight, 45);
                for (int k = 0; k < J; k++) {//Content.settextrise (14);
                c = Watertext.charat (k);
                Convert char to string//Content.showtext (C + "");
                High = 5;
            } content.endtext ();
        } stamper.close ();
    Log.info ("= = =" + srcfile + "= = = Add watermark to = =" + destfile + "= = Success =====");
 }
}


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.