我在将水印添加到 PDF 文件时遇到问题。我的目标是在我的 pdf 文件中没有可选择的、可见的水印。
我正在使用 TextStamp 向我的 pdf 文件添加水印,但它不可见。有趣的是,我可以选择此文本,将其复制到记事本中,就可以了。我知道要解决这个问题,我必须使用 setBacground(false),然后我会看到我的水印,但我仍然可以选择它并复制。
我不想有能力为用户选择水印。然后我使用 setDraw(true) 和 true - 我不能再选择这个水印,但它看起来很丑。有什么解决办法吗?我只想在背景中有一个不可选择的水印。
我已经尝试过使用 TextStamp 和 WatermarkArtifact,但仍然没有 - 老实说,我不知道建议的解决方案是什么,我都尝试过。这是我的代码的片段:
private byte[] addWatermarkToPdf(byte[] data) {
com.aspose.pdf.Document pdfDocument = null;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
pdfDocument = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
TextStamp textStamp = new TextStamp("Watermark text");
textStamp.setBackground(true);
textStamp.setWidth(558.35);
textStamp.setHeight(93.05);
textStamp.setRotateAngle(-315);
textStamp.setOpacity(0.5);
textStamp.getTextState().setFont(new FontRepository().openFont("C:\\myFont.ttf"));
textStamp.getTextState().setFontSize(14.0F);
textStamp.getTextState().setForegroundColor(Color.fromArgb(222, 222, 222));
textStamp.setHorizontalAlignment(HorizontalAlignment.Center);
textStamp.setVerticalAlignment(VerticalAlignment.Center);
for (int Page_counter = 1; Page_counter <= pdfDocument.getPages().size(); Page_counter++) {
pdfDocument.getPages().get_Item(Page_counter).addStamp(textStamp);
}
} catch (Exception e) {
log.error("Error while adding watermark to pdf", e);
return output.toByteArray();
}
pdfDocument.save(output, SaveFormat.Pdf);
return output.toByteArray();
}
我正在使用 Aspose.Total for Java v 17.10
编辑:
我也尝试了这些方法,但它们都不能按我的意愿工作:
方法二
public byte[] addWaterMarkAnnotation3(byte[] data) {
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
FormattedText formattedText = new FormattedText("Watermark", java.awt.Color.BLUE, FontStyle.Courier, EncodingType.Identity_h, true, 72.0F);
WatermarkArtifact artifact = new WatermarkArtifact();
artifact.setText(formattedText);
artifact.setArtifactHorizontalAlignment(HorizontalAlignment.Center);
artifact.setArtifactVerticalAlignment(VerticalAlignment.Center);
artifact.setRotation(45);
artifact.setOpacity(0.5);
artifact.setBackground(true);
doc.getPages().get_Item(1).getArtifacts().add(artifact);
ByteArrayOutputStream out = new ByteArrayOutputStream();
doc.save(out);
return out.toByteArray();
}
方法二
public byte[] addWaterMarkAnnotation4(byte[] data) {
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
TextStamp textStamp = new TextStamp("Watermark");
textStamp.setXIndent(25);
textStamp.setYIndent(400);
textStamp.getTextState().setFont(FontRepository.findFont("Arial"));
textStamp.getTextState().setFontSize(72.0F);
textStamp.getTextState().setFontStyle(FontStyles.Italic);
textStamp.getTextState().setForegroundColor(Color.fromArgb(222, 222, 222));
textStamp.setOpacity(50);
textStamp.setStampId(123456);
textStamp.setBackground(true);
doc.getPages().get_Item(1).addStamp(textStamp);
ByteArrayOutputStream out = new ByteArrayOutputStream();
doc.save(out);
return out.toByteArray();
}
方法 3
public byte[] addWaterMarkAnnotation5(byte[] data) {
PdfFileStamp fileStamp = new PdfFileStamp();
fileStamp.bindPdf(new ByteArrayInputStream(data));
FormattedText formattedText = new FormattedText("Watermark", java.awt.Color.BLUE, FontStyle.Courier, EncodingType.Identity_h, true, 72.0F);
fileStamp.setStartingNumber(1);
final Stamp stamp = new Stamp();
stamp.setRotation(100f);
stamp.setBackground(true);
stamp.setOpacity(0.5f);
stamp.bindLogo(formattedText);
fileStamp.addStamp(stamp);
ByteArrayOutputStream out = new ByteArrayOutputStream();
fileStamp.save(out);
fileStamp.close();
return out.toByteArray();
}
方法 4
public byte[] addWaterMarkAnnotation7(byte[] data) {
PdfFileStamp fileStamp = new PdfFileStamp();
com.aspose.pdf.Document doc = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
fileStamp.bindPdf(doc);
FormattedText formattedText = new FormattedText("Watermark", java.awt.Color.BLUE, FontStyle.Courier, EncodingType.Identity_h, true, 72.0F);
final Stamp stamp = new Stamp();
stamp.bindLogo(formattedText);
stamp.setOrigin(200,200);
stamp.setRotation(100f);
stamp.setBackground(true);
stamp.setOpacity(0.5f);
fileStamp.addStamp(stamp);
ByteArrayOutputStream out = new ByteArrayOutputStream();
fileStamp.save(out);
fileStamp.close();
return out.toByteArray();
}
我找到的唯一可行的解决方案,但我无法旋转它并以我想要的方式使其大小是这样的:
public byte[] addWaterMarkAnnotation(byte[] data) {
// it works in background!!!!!!
com.aspose.pdf.Document document = new com.aspose.pdf.Document(new ByteArrayInputStream(data));
Page page = document.getPages().get_Item(1);
final Rectangle rect = new Rectangle(558.35, 93.05, 400, 600);
rect.rotate(3);
WatermarkAnnotation wa = new WatermarkAnnotation(page, rect);
page.getAnnotations().add(wa);
TextState ts = new TextState();
ts.setFont(new FontRepository().openFont("W:\\myFont.ttf"));
ts.setFontSize(14.0F);
ts.setForegroundColor(Color.fromArgb(222, 222, 222));
ts.setHorizontalAlignment(HorizontalAlignment.Center);
wa.setOpacity(0.8);
wa.setTextAndState(new String[] {"Watermark"}, ts);
ByteArrayOutputStream out = new ByteArrayOutputStream();
document.save(out);
System.out.println(wa.getCharacteristics().getRotate()); // shows me value 0
return out.toByteArray();
}
也许有人可以帮助我或弄清楚如何以其他方式使其工作。