Skip to content
Advertisement

PDFBox How to set PDF document security printing to low resolution

TL;DR; How can PDFBox be used to generate a PDF with “low resolution” under Document Seurity > Details > Printing?

I’m trying to create a PDF using Java and I’m trying to set the value of the printing property, under document security, to low resolution.

I’m trying to do this because the application I’m working on, stumbled across this chrome bug https://bugs.chromium.org/p/chromium/issues/detail?id=1307219 and I’m trying to understand which part of it is setting such value (and, of course, the part that manages PDFs is an old spaghetti code set of classes)

As far as I understood from PDFBox docs my goal should be achievable by playing with the AccessPermission class and setCanPrintDegraded()

I’ve created a simple class that generates PDFs using PDFBox, however, no matter what I try, every single PDF I create comes out with “high resolution”. Am I using the correct flag? Thanks!

enter image description here

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");

    }
}

Advertisement

Answer

Found the solution thanks to @TilmanHausherr (see the comments to the question).

In order to get ‘low quality’ under Document Security > Details > Printing

enter image description here

I had to apply the following changes to the original code:

  1. I’ve set the following permissions:

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

  2. I’ve encrypted the pdf

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

Here’s the updated code:

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");

        }
    }
}
Advertisement