0

I guess my question is rather simple. All I found in my research were threads with very short answers sounding like "DAT SO EZ LUK HEER NAP: link".. I tried those links and they were all 404.. So I'm exposing myself to another public execution and will try this thread a millionth time.

I'm working with PDFBox 2.0.17 and I am trying to sign a PDF-File with an already existing pfx-certificate. Thats pretty much everything. I got some pretty disgusting solutions myself with printing the file via pdf-Creator and stuff, but there must be a smoother, nicer solution.

I'd be pretty thankful for every non-404-Link and will accept any kind of public humiliation.

Best regards, YXCD

P.S.: Of course I found solutions like PDFone and other providers. But I'm trying to do this without getting myself bankrupt ..

YXCD
  • 23
  • 3
  • 2
    Have you had a look at the original PDFBox examples `CreateSignature`, `CreateVisibleSignature`, and `CreateVisibleSignature` [here](https://github.com/apache/pdfbox/tree/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature)? They rely on PKCS12 files, so you may have to transform your pfx to pkcs12 using one of the ways explained in answer to [this question](https://stackoverflow.com/q/6819079/1729265). – mkl Nov 26 '19 at 16:00
  • Yeah. That's actually my most recent try which I'm still at. Im working with the CreateVisibleSignature2 I found here in another thread. Doesn't work though.. I seem to have some trouble with my bouncy castle, because CreateSignatureBase is calling an unknown method: "fromBytArray".. But that's my last hope to make this work. I did not find any other solution for my problem. EDIT: Okay, the wrong method was just because of the version, so I upgraded the version and now he doesn't find the class anymore.. I'm still having fun. – YXCD Nov 26 '19 at 16:09
  • 1
    Consider cloning the whole PDFBox examples project and use maven (or a maven IDE integration) to play around with those examples. That way you'll get the correct dependency versions. – mkl Nov 26 '19 at 16:20
  • I guess I'll have to do that. Just upgraded to 2.0.17 and now my everything failed. It seems the "CreateSignature" class doesn't exist anymore.. But I guess I'm done for today .. Motivation is gone. I hope some1 has some brilliant idea .. :/ – YXCD Nov 26 '19 at 16:38
  • 1
    I always use maven when working with libraries developed with maven. I.e. most of the time. And usually that allows to quickly get going – mkl Nov 26 '19 at 20:46
  • 1
    The best would be that you mention what error you got. The CreateSignature class does exist, but this is in the examples subproject. Try downloading the source code, unpack, open it with your IDE and look there. In theory, the CreateSignature code should work directly, you need to add the command line arguments. – Tilman Hausherr Nov 27 '19 at 06:10
  • 1
    As command line arguments, you can use (on windows) args = new String[]{"XXXXXXX\\PDFBox reactor\\examples\\src\\test\\resources\\org\\apache\\pdfbox\\examples\\signature\\keystore.p12", "123456", "YOURFILE.pdf"}; – Tilman Hausherr Nov 27 '19 at 06:14
  • 1
    Re "public execution" or "public humiliation", this is not the intention here, we want to help you! Also, the management at SO is aware that SO may sometimes be unwelcoming for newbies. But the worst that can happen is downvotes or your question being closed, I haven't seen nasty comments in the PDFBox topic (maybe elsewhere). Anyway, your question can be improved by adding specifics of what didn't work and we'll try our best to get you on the right track. – Tilman Hausherr Nov 27 '19 at 07:59
  • I tried installing maven, but my system is a mess. I can't make the %java_home% working and already made me angry this morning again. after a few trys I gave up and tried it manually. I looked for the most recent pdfbox version and upgraded all the dependencies to the most recent version. A little bit of tuning there, a little bit of turning wheels there and I hit the run button expecting some random error again. Long story short... My files were just corrupted and I used the CreateSignature class wrong. Everything's fine now. – YXCD Nov 27 '19 at 09:56

1 Answers1

0

Okay, to sum this up..

My experience here is that PDFBox has some very precise dependencies which will throw Exceptions the moment they are slightly out of version. I fixed every problem by first of all reloading the entire PDFBox-Files and then downloading every dependencies the exact version as listed in the PDFBox Version. Using newer versions will throw exceptions.

At the end I took the CreateSignature-Example and rewrote it for my needs. Then it all worked perfectly and smoothly.

Thanks to @mkl and @TilmanHausherr for your comments and giving me the right guidelines.

YXCD
  • 23
  • 3
  • 1
    *some very precise dependencies which will throw Exceptions the moment they are slightly out of version* - if you have bouncycastle on your mind, then yes, you should use the same version as your pdfbox version had been compiled for. But this is less a pdfbox issue but more a bouncycastle one, sometimes they hide major api changes behind minor version changes. – mkl Dec 02 '19 at 22:07