如何解决向 PDF 文件添加水印/图章 - 不可见但可选择Aspose
我在将水印添加到 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));
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",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",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));
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();
}
也许有人可以帮助我或想出如何让它以其他方式工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。