Itext PDF Generate watermark Picture Text

Source: Internet
Author: User

Because the project uses, so on the internet to find a lot, but more or less some problems, I sorted out the update.

Itext Rack Package Download Address:

http://download.csdn.net/detail/qm4050/5531349



The PDF generates the following code:


Import java.io.*;
Import com.itextpdf.text.*;

Import com.itextpdf.text.pdf.*; /** * PDF Generation * * Public class Pdfbuild {public static void Buidpdf (String pdffile, String imagefile, String Watermar kname, int permission) {try {File file = File.createtempfile ("Tempfile", ". pdf");//create temporary files//Generate PDF if (CRE Atepdffile (file)) {watermark (File.getpath (), ImageFile, Pdffile, watermarkname, permission);//Add watermark}} C
		Atch (Exception e) {e.printstacktrace (); /** * Create PDF file * * @param file * Temporary files * @return success/failure/public static Boolean Createpdffil
		E (file file) {Rectangle rect = new Rectangle (PAGESIZE.A4);
		Document doc = new document (Rect, 50.0F, 50.0F, 50.0F, 50.0F);
			try {pdfwriter.getinstance (doc, new FileOutputStream (file));

			Doc.open (); Basefont bf = Basefont.createfont ("C:/windows/fonts/simsun".
			ttc,1 "," Identity-h ", true);/Use system fonts Font font = new Font (BF, 14.0F, 0); Font.setstyle (37); Set style
			Font.setfamily ("Song Body");
			Set font Paragraph p = new Paragraph ("payment notice \ r \ n", font);
			P.setalignment (1);
			Doc.add (P);
			Doc.close ();
		return true;
		catch (Exception e) {e.printstacktrace ();
	return false; public static void Watermark (String inputfile, String imagefile, String outputfile, string watermarkname, int permi
			Ssion) {try {pdfreader reader = new Pdfreader (inputfile);

			Pdfstamper stamper = new Pdfstamper (reader, New FileOutputStream (outputfile)); Basefont base = Basefont.createfont ("C:/windows/fonts/simsun.")
			ttc,1 "," Identity-h ", true);/use system font int total = reader.getnumberofpages () + 1;

			Image image = Image.getinstance (ImageFile);
			Picture Position Image.setabsoluteposition (400, 480);
			Pdfcontentbyte under;
			Int J = Watermarkname.length ();
			char c = 0;
			int rise = 0;
				for (int i = 1; i < total; i++) {rise = 400;
				Under = Stamper.getundercontent (i);
				Under.begintext (); Under.setfontandsize (Base, 30);
					if (j >=) {Under.settextmatrix (200, 120);
						for (int k = 0; k < J; k++) {under.settextrise (rise);
						c = Watermarkname.charat (k);
					Under.showtext (c + "");
					} else {Under.settextmatrix (240, 100);
						for (int k = 0; k < J; k++) {under.settextrise (rise);
						c = Watermarkname.charat (k);
						Under.showtext (c + "");

					Rise-= 18;

				}///Add watermark Text Under.endtext ();

				Add watermark Picture under.addimage (image);
				Draw a circle Under.ellipse (250, 450, 350, 550);
				Under.setlinewidth (1f);
			Under.stroke ();
		} stamper.close ();
		catch (Exception e) {e.printstacktrace ();  } public static void Main (string[] args) {string imagefilepath = "d:/itext2.png";//Watermark picture path String Pdffilepath = "D:/itext.pdf";
	File generation path Buidpdf (Pdffilepath, Imagefilepath, "genuine authorization", 16); }
}


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.