Hello,
I am attempting to create a Print-Only watermark as a layer in a PDF
using iText 5.3.0. The end goal is to have the watermark invisible when a
user views the PDF, but have the watermark displayed once they print the
document. I found some examples showing how to create a print-only layer,
and I was able to write the code below. The code works great in Chrome and
the watermark successfully displays only when the PDF is printed using
Chrome. When I open the PDF in Adobe Reader however, the print attribute
on the watermark layer is set to "Print When Visible", and my watermark
never displays, even when printed. I have played around with the
setPrint(), setOn(), and setView() methods of the PdfLayer class, but
haven't had any luck getting my watermark to display when printed from
Adobe Reader. Are there any known issues or differences between Adobe
Reader and Chrome that might be coming into play here? I'm fairly new to
iText, so if I'm missing something or should be using another class, please
let me know and I'd be happy to dig into it.
private void addWatermarkLayer(PdfStamper pdfStamper, PdfReader
pdfReader, String watermarkPDFFilePath, FileService fs, ExecutionContext
cxt, String xSubType) throws IOException, DocumentException,
ServiceException
{
SystemUtils.trace(traceStr, "Attempting to modify the PDF...");
PdfLayer wmLayer = new PdfLayer("watermark", pdfStamper.getWriter());
wmLayer.setOnPanel(true);
// set layer parameters
wmLayer.setOn(false);
wmLayer.setPrint("Watermark", true);
wmLayer.setView(false);
// Prepare transparency
PdfGState transparent = new PdfGState();
transparent.setStrokeOpacity(0.6f);
transparent.setFillOpacity(0.6f);
PdfContentByte cb;
int toPage = pdfStamper.getReader().getNumberOfPages();
for (int i = 1; i <= toPage; i++)
{
cb = pdfStamper.getOverContent(i);
Date d = new Date();
String todaysDate = LocaleResources.localizeDateOnly(d, cxt);
BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER,
"Cp1252", false);
cb.beginLayer(wmLayer);
cb.setGState(transparent); // set block trasparency properties
Rectangle mediaBox = pdfReader.getPageSize(i);
Rectangle cropBox = pdfReader.getCropBox(i);
String watermarkText = "";
if(xSubType != null && (xSubType.equals("SPE") ||
xSubType.equals("NOR")))
{
watermarkText = "Uncontrolled Copy";
}
else{
watermarkText = "Valid Until: " + todaysDate.toString();
}
float textAngle = 45f;
if (cropBox != null && (cropBox.getWidth() <
mediaBox.getWidth() || cropBox.getHeight() < cropBox.getHeight()))
{
mediaBox = cropBox;
}
cb.beginText();
cb.setFontAndSize(bf_courier, 28);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, watermarkText,
200, 300, textAngle);
cb.endText();
cb.endLayer();
}
fs.setFile(watermarkPDFFilePath);
fs.setSendFile(true);
fs.setConditionVar("SuppressCacheControlHeader", true);
}
Thanks for any help,
Paul
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php