Open frankyxcs opened 7 years ago
Could you provide some sample code of what you would expect the API to look like? Thanks
Hi the other guy has included theses dependencies to make the encryption possible
dependencies { compile 'com.madgag.spongycastle:core:1.54.0.0' compile 'com.madgag.spongycastle:pkix:1.54.0.0' compile 'com.madgag.spongycastle:prov:1.54.0.0'
// Test dependencies
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.5'
}
Also the original pdfbox has an encryption part to invoke the encryption after saving the document. Take a look here how the other one made it
int keyLength = 128; // 128 bit is the highest currently supported
// Limit permissions of those without the password
AccessPermission ap = new AccessPermission();
ap.setCanPrint(false);
// Sets the owner password and user password
StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "hi", ap);
// Setups up the encryption parameters
spp.setEncryptionKeyLength(keyLength);
spp.setPermissions(ap);
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
PDFont font = PDType1Font.HELVETICA;
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
try
{
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Write Hello World in blue text
contentStream.beginText();
contentStream.setNonStrokingColor(15, 38, 192);
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello World");
contentStream.endText();
contentStream.close();
// Save the final pdf document to a file
document.protect(spp); // Apply the protections to the PDF
document.save(path);
document.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
I can't see any reason why you could not do that with pdfbox-layout also. What exactly do you want, a convenience layer like, say, Document.protect(String password)
or what is your intention?
Hi Ralf,
yes just a simple thing to protect the pdf for example with 128 Bit with admin and user password. I have tried to get this to work with pdfbox1-layout-1.0.0.jar but without any success.
I´m not very confirm with encryption stuff.
Okay, you were looking in the Android port of the pdfbox, that's something different. Here is a link to the PDFBox 1.8.x dependencies, and here are the dependencies for PDFBox 2.x. See the chapter on encryption for the correct bouncy castle jars you need to add to your project. Following is a code snippet of my pdfbox-layout hello world with password encryption, and it works (tested).
public static void main(String[] args) throws Exception {
Document document = new Document(40, 60, 40, 60);
Paragraph paragraph = new Paragraph();
paragraph.addText("Hello Document", 20,
PDType1Font.HELVETICA);
document.add(paragraph);
AccessPermission ap = new AccessPermission();
// Disable printing, everything else is allowed
ap.setCanPrint(false);
// Owner password (to open the file with all permissions) is "12345"
// User password (to open the file but with restricted permissions, is 4711)
StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "4711", ap);
spp.setEncryptionKeyLength(128);
document.getPDDocument().protect(spp);
final OutputStream outputStream = new FileOutputStream("hellodoc.pdf");
document.save(outputStream);
}
Since most of the code is needed to specify the security parameters, I do not think there is much need to provide simplified layer here. Hope this snippet helps. Regards Ralf
Hi Ralf, but the dependencies are only for using pure java and not android i would guess ?! Or did you test it on android ?
ok thanx for your help. Yes i forgot to mention that i´m using it in an android project .
It is not working on Android because AccessPersmission stuff is missing in your .jar file and StandardProtectionPolicy to protect the pdf under Android
I'm sorry, pdfbox-layout is not suited for Android, so there is not much I can do :-\
Hi would be nice if you could add encryption to the pdfbox to protect the pdf with password like here
https://github.com/TomRoush/PdfBox-Android
Greets
Frank