import java.awt.Color;
import java.io.FileOutputStream;
//import java.util.HashMap;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
//import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState
;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
/**
* Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
*/
public class pdfWatermark {
/** The Graphic state */
public static PdfGState gstate;
public String inFile;
public String outFile;
public String imgFile;
/**
* Reads the pages of an existing PDF file, adds pagenumbers and a watermark.
* @param args no arguments needed
*/
public pdfWatermark(){
inFile = "";
outFile = "";
imgFile = "";
}
public pdfWatermark (String iFile, String oFile, String imageFile) {
inFile = iFile;
outFile = oFile;
imgFile = imageFile;
}
public void makeWatermark () {
// System.out.println("Add watermarks and pagenumbers");
try {
// we create a reader for a certain document
PdfReader reader = new PdfReader(inFile);
int n = reader.getNumberOfPages();
// we create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outFile),'\0');
// adding some metadata
// HashMap moreInfo = new HashMap();
//moreInfo.put("Author", "Bruno Lowagie");
// stamp.setMoreInfo(moreInfo);
// adding content to each page
int i = 0;
PdfContentByte under;
PdfContentByte over;
Image img = Image.getInstance(imgFile);
img.setRotationDegrees(45);
//img.setTransparency(new int[] {255, 255});
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
gstate = new PdfGState();
gstate.setFillOpacity(0.3f);
gstate.setStrokeOpacity
(0.3f);
img.setAbsolutePosition(160, 300);
while (i < n) {
i++;
// watermark OVER the existing page
under = stamp.getUnderContent(i);
//under.addImage(img);
// text over the existing page
over = stamp.getOverContent(i);
over.addImage(img);
over.beginText();
over.setGState
(gstate);
over.setColorFill(Color.red);
//under.setFontAndSize(bf, 18);
//under.setTextMatrix(30, 30);
//under.showText("page " + i);
over.setFontAndSize(bf, 80);
over.showTextAligned(Element.ALIGN_CENTER, " PAID", reader.getPageSize(i).width() /2,reader.getPageSize(i).height() /2, 45);
over.endText();
}
// adding an extra page
/*
stamp.insertPage(1, PageSize.A4);
over = stamp.getOverContent(1);
over.beginText();
over.setFontAndSize(bf, 18);
over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE OF AN EXISTING PDF DOCUMENT", 30, 600, 0);
over.endText();
*/
// adding a page from another document
/*
PdfReader reader2 = new PdfReader("SimpleAnnotations1.pdf");
under = stamp.getUnderContent(1);
under.addTemplate(stamp.getImportedPage(reader2, 3), 1, 0, 0, 1, 0, 0);
*/
// closing PdfStamper will generate the new PDF file
stamp.close();
}
catch (Exception de) {
de.printStackTrace();
}
}
public static void main (String args[]){
pdfWatermark p = new pdfWatermark(args[0], args[1],args[2]);
p.makeWatermark();
}
}
------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://itext.ugent.be/itext-in-action/
