【发布时间】:2015-10-19 10:25:34
【问题描述】:
我遇到过许多使用 PDFBox 图层实用程序的 appendFormAsLayer 方法的示例,如下所示:
/**
* Places the given form over the existing content of the indicated page (like an overlay).
* The form is enveloped in a marked content section to indicate that it's part of an
* optional content group (OCG), here used as a layer. This optional group is returned and
* can be enabled and disabled through methods on {@link PDOptionalContentProperties}.
* @param targetPage the target page
* @param form the form to place
* @param transform the transformation matrix that controls the placement
* @param layerName the name for the layer/OCG to produce
* @return the optional content group that was generated for the form usage
* @throws IOException if an I/O error occurs
*/
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage,
PDXObjectForm form, AffineTransform transform,
String layerName) throws IOException
{
PDDocumentCatalog catalog = targetDoc.getDocumentCatalog();
PDOptionalContentProperties ocprops = catalog.getOCProperties();
if (ocprops == null)
{
ocprops = new PDOptionalContentProperties();
catalog.setOCProperties(ocprops);
}
if (ocprops.hasGroup(layerName))
{
throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName);
}
PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
ocprops.addGroup(layer);
PDResources resources = targetPage.findResources();
PDPropertyList props = resources.getProperties();
if (props == null)
{
props = new PDPropertyList();
resources.setProperties(props);
}
//Find first free resource name with the pattern "MC<index>"
int index = 0;
PDOptionalContentGroup ocg;
COSName resourceName;
do
{
resourceName = COSName.getPDFName("MC" + index);
ocg = props.getOptionalContentGroup(resourceName);
index++;
} while (ocg != null);
//Put mapping for our new layer/OCG
props.putMapping(resourceName, layer);
PDPageContentStream contentStream = new PDPageContentStream(
targetDoc, targetPage, true, !DEBUG);
contentStream.beginMarkedContentSequence(COSName.OC, resourceName);
contentStream.drawXObject(form, transform);
contentStream.endMarkedContentSequence();
contentStream.close();
return layer;
}
前面代码中getPDFName调用中的“MC”是什么意思?
我编写了以下代码来在现有 pdf 的每一页上插入水印并启用每组可选内容。
LayerUtility layerUtility = new LayerUtility(document);
PDXObjectForm form = layerUtility.importPageAsForm(overlayDoc, 0);
for (int i = 0; i < document.getDocumentCatalog().getAllPages().size(); i++) {
PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(i);
PDOptionalContentGroup ocGroup = layerUtility.appendFormAsLayer(page, form, new AffineTransform(), "watermark" + i);
}
PDOptionalContentProperties ocprops = document.getDocumentCatalog().getOCProperties();
for (String groupName : ocprops.getGroupNames()) {
if (groupName.startsWith("watermark")) {
ocprops.setGroupEnabled(groupName, true);
}
}
将组设置为启用或禁用“setGroupEnabled(groupName, true)”会使其同时显示以供显示和打印。根据我在此主题上研究的其他信息,可以在可选内容可见时进行更精细的调整,这表明存在 onScreen 和 onPrint 布尔属性,可以设置这些属性来确定内容的可见性。见https://acrobatusers.com/tutorials/watermarking-a-pdf-with-javascript
有没有办法使用 PDFBox 使水印在打印时可见但在显示时不可见?如果没有,任何替代解决方案的建议将不胜感激。
这里是从字符串 (createOverlay) 和调用 LayerUtility 传递水印文档的函数 (addWatermark) 创建水印 pdf 的附加代码。所需要做的就是从任何现有的 pdf 文件创建一个 PDDocument 并将其与水印字符串一起传递。
public PDDocument addWatermark(PDDocument document, String text) throws IOException {
PDDocument overlayDoc = createOverlay(text);
// Create the watermark in an optional content group
LayerUtility layerUtility = new LayerUtility(document);
PDXObjectForm form = layerUtility.importPageAsForm(overlayDoc, 0);
for (int i = 0; i < document.getDocumentCatalog().getAllPages().size(); i++) {
PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(i);
layerUtility.appendFormAsLayer(page, form, new AffineTransform(), "watermark" + i);
}
return document;
}
private PDDocument createOverlay(String text) throws IOException {
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDExtendedGraphicsState extendedGraphicsState = new PDExtendedGraphicsState();
// Set the transparency/opacity
extendedGraphicsState.setNonStrokingAlphaConstant(0.4f);
if (page.findResources() == null) {
page.setResources(new PDResources());
}
PDResources resources = page.findResources();// Get the page resources.
// Get the defined graphic states.
if (resources.getGraphicsStates() == null)
{
resources.setGraphicsStates(new HashMap<String, PDExtendedGraphicsState>());
}
Map<String, PDExtendedGraphicsState> graphicsStateDictionary = resources.getGraphicsStates();
if (graphicsStateDictionary != null){
graphicsStateDictionary.put("TransparentState", extendedGraphicsState);
resources.setGraphicsStates(graphicsStateDictionary);
}
// the x/y coords
Float xVal = 0f; //Float.parseFloat(config.getProperty("ss.xVal"));
Float yVal = 0f; //Float.parseFloat(config.getProperty("ss.yVal"));
// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);
contentStream.appendRawCommands("/TransparentState gs\n");
// Create the text and position it
contentStream.beginText();
contentStream.setFont(font, fontSize);
contentStream.setTextRotation(Math.PI/4,page.getMediaBox().getWidth()/4,page.getMediaBox().getHeight()/4);
contentStream.setNonStrokingColor(210,210,210); //light grey
contentStream.moveTextPositionByAmount(xVal, yVal);
contentStream.drawString(text);
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
//return the string doc
return document;
}
【问题讨论】:
-
PDF 规范有你想要的,请参阅 PDF 规范中的“可选内容使用字典中的条目”。但是我看不到 PDFBox 是如何支持它的,我在代码中找到了这条评论:“//TODO 添加对“Intent”和“Usage”的支持”。但是仍然可以“艰难地”添加字典,即通过使用 getCOSObject() 然后使用你得到的 COSDictionary。关于您的另一个问题:MC 只是这些资源的名称,它们被称为 /MC0、/MC1 等。我可以提供更多帮助,但需要一些示例文件来确定。
标签: java pdf layer pdfbox watermark