PDFBox如何将PDF文档安全打印设置为低分辨率

PDFBox How to set PDF document security printing to low resolution

TL;DR; 如何使用 PDFBox 在“文档安全”>“详细信息”>“打印”下生成具有“低分辨率”的 PDF?

我正在尝试使用 Java 创建 PDF,并且我正在尝试将文档安全性下的打印值 属性 设置为低分辨率。

我尝试这样做是因为我正在处理的应用程序偶然发现了这个 chrome 错误 https://bugs.chromium.org/p/chromium/issues/detail?id=1307219 并且我试图了解它的哪一部分正在设置这样的值(当然,管理 PDF 的部分是一个旧的意大利面条代码集 classes)

据我所知 PDFBox docs 我的目标应该可以通过使用 AccessPermission class 和 setCanPrintDegraded()

来实现

我创建了一个使用 PDFBox 生成 PDF 的简单 class,但是,无论我尝试什么,我创建的每个 PDF 都具有“高分辨率”。我使用的是正确的标志吗? 谢谢!

public static void main(String[] args) throws IOException {

    try (PDDocument doc = new PDDocument()) {

        PDPage myPage = new PDPage();
        doc.addPage(myPage);


        try (PDPageContentStream cont = new PDPageContentStream(doc, myPage)) {

            cont.beginText();

            cont.setFont(PDType1Font.TIMES_ROMAN, 12);
            cont.setLeading(14.5f);

            cont.newLineAtOffset(25, 700);
            String line1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
                    "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " ;
            cont.showText(line1);
            cont.newLine();
            String line2 = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " +
                    "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " ;
            cont.showText(line2);
            cont.newLine();
            String line3 = "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." ;
            cont.showText(line3);
            cont.newLine();

            cont.endText();
        }

        AccessPermission permission = new AccessPermission();
        permission.setCanPrintDegraded(true);
        permission.setReadOnly();
        StandardProtectionPolicy policy = new StandardProtectionPolicy("", "", permission);
        doc.protect(policy);
        doc.save("src/main/resources/setCanPrintDegradedTrueANDreadOnly.pdf");

    }
}

感谢@TilmanHausherr 找到了解决方案(请参阅问题的评论)。

为了在文档安全>详细信息>打印下获得'low quality'

我必须对原始代码应用以下更改:

  1. 我设置了以下权限:

    AccessPermission 权限 = new AccessPermission(); permission.setCanPrint(真); permission.setCanPrint已降级(假);

  2. 我已经加密了 pdf

    Int keyLeght = 128 StandardProtectionPolicy policy = new StandardProtectionPolicy("test", "", permission); policy.setEncryptionKeyLength(keyLength);

这是更新后的代码:

import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class JavaPdfBoxWriteText {

    public static void main(String[] args) throws IOException {

        try (PDDocument doc = new PDDocument()) {
            int keyLength = 128;
            PDPage myPage = new PDPage();
            doc.addPage(myPage);


            try (PDPageContentStream cont = new PDPageContentStream(doc, myPage)) {

                cont.beginText();

                cont.setFont(PDType1Font.TIMES_ROMAN, 12);
                cont.setLeading(14.5f);

                cont.newLineAtOffset(25, 700);
                String line1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
                        "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " ;
                cont.showText(line1);
                cont.newLine();
                String line2 = "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " +
                        "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " ;
                cont.showText(line2);
                cont.newLine();
                String line3 = "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." ;
                cont.showText(line3);
                cont.newLine();

                cont.endText();
            }
            AccessPermission permission = new AccessPermission();
            permission.setCanPrint(true);
            permission.setCanPrintDegraded(false);

            StandardProtectionPolicy policy = new StandardProtectionPolicy("test", "", permission);
            policy.setEncryptionKeyLength(keyLength);
            doc.protect(policy);
            doc.save("src/main/resources/testPDF.pdf");

        }
    }
}