Use a PdfStamper. If you want to use PdfWriter don't use Watermark, use page events instead.
Best Regards,
Paulo Soares
-----Original Message-----
From: [EMAIL PROTECTED] [SMTP:[EMAIL PROTECTED] On Behalf Of Kalpana Sundaram
Sent: Monday, January 12, 2004 18:57
To: [EMAIL PROTECTED]
Subject: [iText-questions] Question about watermark
Hello,
�
I am a Java/J2EE developer in Parsippany, NJ. I am trying to make a watermark be displayed on every page of a PDF file instead of just on the first page. The following are the lines of code that I am using to achieve this:
String templateFolder = (String) htHeader.get("TEMPLATE_FOLDER");
��� String templateName = (String) htHeader.get("TEMPLATE");
��� PdfReader reader = new PdfReader(stringUtils.getFolder(templateFolder)+
�������������������������������������� templateName);
��� int n = reader.getNumberOfPages();
��� if (n==0){
����� throw new Exception("This document has zero(0) pages");
��� }
��� int lastDetailPageNum = getLastDetailPage(alDetail);
��� if (lastDetailPageNum > 0 && n < lastDetailPageNum) {
����� appendExtraPagesToTemplate(templateName, templateFolder, reader, htHeader, alDetail, lastDetailPageNum);
����� reader = new PdfReader(stringUtils.getFolder(templateFolder)+
�������������������������������������� templateName);
��� }
��� n = reader.getNumberOfPages();
��� pdfConsts.setTotalNumOfPages(n);
��� int i = 0;
��� PdfImportedPage page;
��� int rotation;
��� Rectangle rect = reader.getPageSizeWithRotation(1);
��� Color bgcolor = getColor(htHeader, "BGCOLOR_BASE", "BGCOLOR_VAL1", "BGCOLOR_VAL2",
��� "BGCOLOR_VAL3", "BGCOLOR_VAL4");
��� rect.setBackgroundColor(bgcolor);
��� FileOutputStream fos = new FileOutputStream(((String) htHeader.get("SAVE_FOLDER"))+
����������������������������������������������� ((String) htHeader.get("SAVE_AS")));
��� PdfWriter writer = PdfWriter.getInstance(document, fos);
��� document.setPageSize(rect);
��� Image wmImg = pdfImage.loadImage("H:\\dms-html\\cendant\\images\\sponge.jpg");
��� float tLy = document.getPageSize().height() - 5f - wmImg.scaledHeight();
��� Watermark watermark = new Watermark(wmImg, 5f, tLy);
��� document.add(watermark);
��� document.open();
��� PdfContentByte cb = writer.getDirectContent();
��� while (i < n) {
����� i++;
����� //System.out.println("Current template pagenum: " + i);
����� rect = reader.getPageSizeWithRotation(i);
����� rect.setBackgroundColor(bgcolor);
����� document.setPageSize(rect);
����� document.newPage();
����� watermark = new Watermark(wmImg, 5f, tLy);
����� document.add(watermark);
����� page = writer.getImportedPage(reader, i);
����� rotation = reader.getPageRotation(i);
����� if (rotation == 90 || rotation == 270) {
������� cb.addTemplate(page, 0, -1f, 1f, 0, 0, rect.height());
����� }
����� else {
������� cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
����� }
����� for (int j = 0; j < alDetail.size(); j++) {
������� Hashtable htDetail = (Hashtable) alDetail.get(j);
������� int pageNum = 1;
������� if (htDetail.containsKey("PAGENUM") && !stringUtils.isNull((String) htDetail.get("PAGENUM")))
��������� pageNum = Integer.parseInt((String) htDetail.get("PAGENUM"));
��������� if (pageNum == 0)
����������� pageNum = 1;
//������� System.out.println("Current detail pagenum: " + pageNum);
������� if (pageNum != i)
��������� continue;
������� String sActionType = (String) htDetail.get("ACTION_TYPE");
//������� System.out.println("Current Action Type: " + sActionType);
������� if ( sActionType.equals("T") )
��������� pdfText.addText(htDetail, htHeader, cb, document);
������� else if ( sActionType.equals("I") )
��������� pdfImage.addImage(htDetail, cb, document);
������� else if ( sActionType.equals("P") )
��������� pdfPDF.addPDF(htDetail, writer, document, cb, 1);
������� else if ( sActionType.equals("C") )
��������� pdfColumnText.addColumnText(document, htDetail, htHeader, cb);
������� else if ( sActionType.equals("L") )
��������� pdfGraphics.drawLine(document, htDetail, cb);
������� else if ( sActionType.equals("R") )
��������� pdfGraphics.drawRectangle(document, htDetail, cb);
������� else if ( sActionType.equals("K") )
��������� pdfGraphics.drawCircle(document, htDetail, cb);
����� }
��� }
�
-----------------------------------------------------------------------------------
�
The problem is that when I invoke the method containing this source code, the watermark�is displayed on�only the first page of the output PDF file instead of�on all the pages. Is there something I am doing wrong here? Please tell me the correct way of adding watermark to all the pages of a PDF file and also send me example code, if any.
�
Thanks,
�
Kalpana
_____
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes <http://pa.yahoo.com/*http://us.rd.yahoo.com/hotjobs/mail_footer_email/evt=21482/*http://hotjobs.sweepstakes.yahoo.com/signingbonus>
