Hello All
I am wondering why following code doesn't work when I use
RandomAccessFileOrArray as an input and works when I create PdfReader
passing a filename (String) directly.
Difference is that the PDF created using RandomAccessFileOrArray does not
contain watermarks.
Any ideas?
Best regards
Karol
import java.io.File;
import java.io.FileOutputStream;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
public class Test {
private static void addWatermark(PdfReader reader, PdfContentByte
content,
Image watermark, int m_pageCurrent) throws Exception {
float pageWidth =
reader.getPageSizeWithRotation(m_pageCurrent).width();
float pageHeight =
reader.getPageSizeWithRotation(m_pageCurrent).height();
float watermarkWidth = watermark.width();
float watermarkHeight = watermark.height();
float x = (pageWidth - watermarkWidth) / 2;
float y = (pageHeight - watermarkHeight) / 2;
content.addImage(watermark, watermarkWidth, 0, 0,
watermarkHeight, x, y);
}
public static void main(String[] args) throws Exception {
// PdfReader tmpReader = new PdfReader("c:\\file.pdf");
RandomAccessFileOrArray randomFile = new
RandomAccessFileOrArray("c:\\file.pdf");
PdfReader tmpReader = new PdfReader(randomFile,null);
File fileResult = new File("c:\\out.pdf");
fileResult.createNewFile();
PdfStamper stamper = new PdfStamper(tmpReader, new
FileOutputStream(fileResult));
Image imageWatermark = null;
File fileWatermark = new File("c:\\watermark.jpg");
PdfGState gs = new PdfGState();
gs.setFillOpacity(0.5f);
imageWatermark = Image.getInstance(fileWatermark.toString());
for (int m_pageCurrent = 1; m_pageCurrent <=
tmpReader.getNumberOfPages(); m_pageCurrent++) {
PdfContentByte overContent =
stamper.getOverContent(m_pageCurrent);
overContent.saveState();
overContent.setGState(gs);
addWatermark(tmpReader, overContent, imageWatermark,
m_pageCurrent);
overContent.restoreState();
}
stamper.close();
}
}
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
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/