Java 保护PDF';s

Java 保护PDF';s,java,pdf,apache-fop,Java,Pdf,Apache Fop,我目前正在使用ApacheFOP库来生成PDF。我想保护这些PDF不被复制粘贴,因此人们必须使用实际的OCR库(或手动键入)来获取PDF上的信息 FOP显然提供了一些安全性,然后在PDF上添加为元数据,以防止打印或复制之类的事情,但这似乎无法正常工作(启用打印时无法禁用复制粘贴等) 在我看来,一种直接的可能性基本上是以某种方式将PDF上的所有文本转换为图像,但我找不到任何关于这一问题的信息 显然,我不在乎PDF是否可搜索。我只是想防止人们在仍然可以打印的时候复制粘贴 我当前的FOP代码: pri

我目前正在使用ApacheFOP库来生成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);
}
以下是myfopconfig.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>

假的
假的
./
./
72
72

我不确定它是如何与ApacheFop一起工作的,但是使用iText lib非常简单


这是我不久前写的一篇关于这个的教程

您使用的是什么版本?您是否有任何fop.conf文件,在实例化FopFactory时传递设置文件时覆盖任何设置?添加了额外的信息,Anida。对图像内容的转换是否必须在fop转换期间进行,或者是一个后处理步骤,例如使用apache pdfbox也可以?您正在添加fopconfig.xml@Aninda可能在说你在哪个地方设置了加密。mkl,在转换后进行加密对我来说是一种可能,但我更喜欢一步到位。我相信,我不会添加任何fop.xconf文件。