/static/fonts/SIMYOU.TTF 'font

/ **
* Pdf hydrolysis marked
*
* Attoreturn
* /
Public Byte [] PdfAddWaterMark (Byte [] Byes) {

. String fileName = UUID.randomUUID () toString () + ".pdf";
String courseFile = "";
the try {
// second: get the project path D: \ git \ daotie \ daotie
// generate temporary files, read take complete delete
File directory = new File ( "" ); // parameter is empty
courseFile directory.getCanonicalPath = () + "/";
} the catch (IOException E) {
e.printStackTrace ();
}
byte [] = null returnBytes ;
// file to be watermarked
PdfReader Reader = null;
PdfStamper Stamper = null;
// ByteArrayOutputStream BAOS = null;
a FileOutputStream OS = null;
the try {
Reader = new new PdfReader (-byes);
// complete addition watermarked file
// BAOS ByteArrayOutputStream new new = ();
// new new Stamper = PdfStamper (Reader, BAOS);
// add watermark complete file
os = new FileOutputStream(courseFile + fileName);
stamper = new PdfStamper(reader, os);

Total reader.getNumberOfPages = int () +. 1;
PdfContentByte Content;
// BaseFont font BaseFont.createFont = ();
BaseFont the basefont = BaseFont.createFont ( "/ static / Fonts / SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED );
// here more crucial font settings, this setting is supported by Chinese wording
/ * BaseFont Base = BaseFont.createFont ( "STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // use the system fonts * /

/ * // set the transparency
PdfGState new new PdfGState GS = ();
gs.setFillOpacity (1F);
gs.setStrokeOpacity (1F); * /

PdfContentByte under;
com.itextpdf.text.Rectangle pageRect = null;

// per cycle of inserting a watermark
for (int I =. 1; I <Total; I ++) {
pageRect = stamper.getReader () getPageSizeWithRotation (I);.
// watermark calculating X, Y coordinates
float x = (float) ( pageRect.getWidth () / 1.98);
a float Y = (a float) (pageRect.getHeight () / 2.8);
// get PDF topmost
an under stamper.getOverContent = (I);
under.saveState ();
// SET Transparency
GS = new new PdfGState PdfGState ();
// set transparency 0.2
gs.setFillOpacity (1.f);
under.setGState (GS);
under.restoreState ();
under.beginText ();
under.setFontAndSize (the basefont, pageRect. getHeight () /. 17);
under.setColorFill (BaseColor.RED);

Watermark Text // inclined at an angle of 45 degrees
System.out.println ( "width" + pageRect.getWidth ());
System.out.println ( "height" + pageRect.getHeight ());
System.out.println ( " X "+ X);
System.out.println (" Y "+ Y);
under.showTextAligned (Element.ALIGN_CENTER," preview picture only, not for commercial use ", X, Y, 45);
// add watermark text
under.endText ();
under.setLineWidth (1F);
under.stroke ();
}
// returnBytes baos.toByteArray = ();
} the catch (IOException E) {
e.printStackTrace ();
} the catch (E DocumentException ) {
e.printStackTrace ();
} {the finally
the try {
stamper.close ();
IF (OS = null) {!
os.close ();
}
IF (Reader!= null) {
reader.close();
}
} catch (DocumentException e) {
e.printStackTrace(http://www.amjmh.com/v/BIBRGZ_558768/);
} catch (IOException e) {
e.printStackTrace();
}
}
————————————————

Guess you like

Origin www.cnblogs.com/ly570/p/11426884.html