gpt4 book ai didi

java - 保护 PDF

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:57:39 27 4
gpt4 key购买 nike

我目前正在使用 Apache FOP 库生成 PDF。我希望这些 PDF 免受复制粘贴,因此人们必须使用实际的 OCR 库(或手动输入)来获取 PDF 上的信息。

FOP 显然提供了一些安全性,然后将其作为 元数据 添加到 PDF 上,以防止诸如打印或复制之类的事情发生,但这似乎无法正常工作(不能启用打印时禁用复制粘贴等)。

一种对我来说似乎很直接的可能性基本上是以某种方式将 PDF 上的所有文本转换为图像,但我找不到关于此事的任何信息。

显然,我不关心 PDF 是否可搜索。我只是想防止人们复制粘贴,而他们仍然应该能够打印它。

我当前的 FOP 代码:

private static FopFactory fopFactory;

private static FopFactory initializeFactory() throws IOException,
SAXException {
if (fopFactory == null) {
File f = new File(SettingUtil.getSetting(LetterGeneratorSettings.FOP_CONFIG_LOCATION));
fopFactory = FopFactory.newInstance(f);
}
return fopFactory;
}

public static File generatePDFFromXML(File fopTemplate, File xmlSource,
File resultFileLocation) throws IOException {
try {
initializeFactory();
URL url = fopTemplate.toURI().toURL();
// creation of transform source
StreamSource transformSource = new StreamSource(url.openStream());
// create an instance of fop factory

// a user agent is needed for transformation
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
foUserAgent.getRendererOptions().put("encryption-params",
getEncryptionParams());
// to store output
ByteArrayOutputStream pdfoutStream = new ByteArrayOutputStream();
StreamSource source = new StreamSource(new ByteArrayInputStream(IOUtils.toByteArray(new FileInputStream(xmlSource))));
Transformer xslfoTransformer;
try {
TransformerFactory transfact = TransformerFactory.newInstance();

xslfoTransformer = transfact.newTransformer(transformSource);
// Construct fop with desired output format
Fop fop;
try {
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, pdfoutStream);
// Resulting SAX events (the generated FO)
// must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());

// Start XSLT transformation and FOP processing
try {
// everything will happen here..
xslfoTransformer.transform(source, res);

// if you want to save PDF file use the following code
OutputStream out = new java.io.FileOutputStream(resultFileLocation);
out = new java.io.BufferedOutputStream(out);
FileOutputStream str = new FileOutputStream(resultFileLocation);
str.write(pdfoutStream.toByteArray());
str.close();
out.close();

} catch (TransformerException e) {
e.printStackTrace();
}
} catch (FOPException e) {
e.printStackTrace();
}
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
}
return resultFileLocation;
} catch (Exception ex) {
throw new IOException(ex);
}
}

private static PDFEncryptionParams getEncryptionParams() {
return new PDFEncryptionParams(null,
SettingUtil.getSetting(LetterGeneratorSettings.PDF_PASSWORD),
true, false, false, false, false);
}

以下是我的fopconfig.xml

的内容
    <fop version="1.0">

<!-- Strict user configuration -->
<strict-configuration>false</strict-configuration>

<!-- Strict FO validation -->
<strict-validation>false</strict-validation>

<!-- Base URL for resolving relative URLs -->
<base>./</base>

<!-- Font Base URL for resolving relative font URLs -->
<font-base>./</font-base>

<!-- Source resolution in dpi (dots/pixels per inch) for determining the size of pixels in SVG and bitmap images, default: 72dpi -->
<source-resolution>72</source-resolution>
<!-- Target resolution in dpi (dots/pixels per inch) for specifying the target resolution for generated bitmaps, default: 72dpi -->
<target-resolution>72</target-resolution>

<!-- default page-height and page-width, in case
value is specified as auto -->
<default-page-settings height="11in" width="8.26in"/>

<!-- etc. etc..... -->
</fop>

最佳答案

我不确定它是如何与 Apache FOP 一起工作的,但它与 iText 库一起使用非常容易。

这是我前一段时间写的关于这个的教程 http://tutors4all.net/index.php/2015/05/06/encrypt-pdf-file/

关于java - 保护 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34065617/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com