Recently, the project needs to investigate how to add the logo in pdf to differentiate versions. Finally, it is determined to use itext for implementation.
The Code is as follows:
Java code
/**
* Authory kingviker
* Time: 2012-12-12
*/
Import java. io. FileOutputStream;
Import java. io. IOException; www.2cto.com
Import com. itextpdf. text. DocumentException;
Import com. itextpdf. text. Font;
Import com. itextpdf. text. Image;
Import com.itext;.text=. BaseFont;
Import com.itext;.text=. PdfArray;
Import com.itext;.text=. inclucontentbyte;
Import com.itext;.text}. PdfDictionary;
Import com.itext;.text}. CommonName;
Import com.itext;.text=. PdfObject;
Import com.itext;.text=. PdfReader;
Import com.itext;.text=. datagstamper;
Public class AddContentToPDF {
Public static void main (String [] args) throws IOException, incluentexception {
// Create a pdf read stream
PdfReader reader = new PdfReader ("C:/helpader ");
// Create a pdfStamper based on a pdfreader to generate a new pdf file.
PdfStamper stamper = new PdfStamper (reader,
New FileOutputStream ("C:/help2.pdf "));
// This font is self-contained in the itext-asian.jar, so do not consider the operating system environment issues.
BaseFont bf = BaseFont. createFont ("STSong-Light ",
"UniGB-UCS2-H", BaseFont. NOT_EMBEDDED); // set font
// BaseFont does not support font style setting. However, font fonts require the operating system to support this character.
Font font = new Font (bf, 10 );
Font. setStyle (Font. BOLD );
Font. getBaseFont ();
// The page number starts from 1.
For (int I = 1; I <= reader. getNumberOfPages (); I ++ ){
// Obtain the content printed on the upper layer of the current page. That is to say, the content will overwrite the original pdf content.
Response contentbyte over = stamper. getOverContent (I );
// Use pdfreader to obtain the dictionary object of the current page. It contains some data of this page, such as the coordinate axis information of this page.
PdfDictionary p = reader. getPageN (I );
// Get the pdf size information of the page in mediaBox.
PdfObject po = p. get (new queue name ("MediaBox "));
System. out. println (po. isArray ());
// Po is an array object that contains the coordinate axis range of the pdf file on this page.
PdfArray pa = (PdfArray) po;
System. out. println (pa. size ());
// Check the maximum value of the Y axis.
System. out. println (pa. getAsNumber (pa. size ()-1 ));
// Start writing text
Over. beginText ();
// Set the font and size
Over. setFontAndSize (font. getBaseFont (), 10 );
// Set the font output position
Over. setTextMatrix (107,540 );
// Text to be output
Over. showText ("I want to add the words [Final Draft]" + I );
Over. endText ();
// Create an image object.
Image image = Image. getInstance ("c:/1.jpg ");
// Set the output position of the image object pa. getAsNumber (pa. size ()-1). floatValue () is the maximum value of the Y axis of the pdf axis on this page.
Image. setAbsolutePosition (0, pa. getAsNumber (pa. size ()-1). floatValue ()-100); // 0, 0,841.92, 595.32
Over. addImage (image );
// Draw a circle.
Over. setRGBColorStroke (0xFF, 0x00, 0x00 );
Over. setLineWidth (5f );
Over. ellipse (250,450,350,550 );
Over. stroke ();
}
Stamper. close ();
}
}