put password to a pdf file in C++

Posted on 2011-09-06
Last Modified: 2012-05-12
Hi Experts,

I need to put a password to a pdf file. How can I do that?

Question by:justinY
  • 2
  • 2

Expert Comment

ID: 36489680
The following is in C#, but converting should not be a problem:

Author Comment

ID: 36490758
Thanks zvytas. But I dont know C#. Can you give me C++ version?

Expert Comment

ID: 36510206
PDF is a difficult format to manipulate programmatically!

I recently had an application which needed to print data from a database onto a PDF formatted certificate. I changed the PDF certificate into a PDF form. I used the FDF toolkit from adobe to create an FDF file with the field data. I then needed to merge the FDF data into the PDF form fields. (There are commercial C++ libraries available to do this, most quite expensive and most designed to work as a server process) Then I modified the "pdftk" source code (open source) to create a DLL to merge the FDF data into the PDF form and create a PDF file, which was not a trivial undertaking!

My recommendation to you would be to create a PDF form as a template with a field to hold your password string (requires a full copy of Acrobat, the reader can't do this). Then your code can write an FDF file using the Adobe FDF Toolkit headers and library. Adobe Reader can merge the form data into the PDF form if both files are available to the user.

The other option would be to use libharu to assemble the pdf file directly.


Accepted Solution

tampnic earned 125 total points
ID: 36513785
Reading the question again I think I misunderstood. Are you trying to write some text into a PDF file or protect the PDF file with a password? I explained how to write data into a PDF in my answer above.

If protecting an existing PDF file with a password, libharu can only assemble PDF files and not read existing PDFs so its no good here. iTextSharp is the only open source library I know of which will encrypt an existing PDF file. There are commercial options for native C++ code which can cost $1000 plus. Unfortunately iTextSharp is written in Java, a C# port exists which you were pointed to.

Pdftk is an open source C++ program which uses iTextSharp compiled by the GNU Java compiler into native machine code. Look at the source for pdftk to find out how they call the iTextSharp libraries. Building pdftk is not trivial if you are targetting Windows -  you need to set up a MinGW32 environment to do it. You can't use the current MinGW environment as they dropped support for the GNU Java compiler a while back. You need to hunt the MinGW32 sourceforge archive for the files you need. Once you have a working MinGW environment (took me about three days to set up, trying different combinations of GCJ, GCC, MinGW base versions until I stumbled across a set which would work together) you can then strip the pdftk source code down to the functionailty you require and encapsulate it in a dll.

If you can live with it, the easier option would be to call the pdftk executable with the correct parameters using a system() call if restricted to ANSI C++ or ShellExecute() if on Windows. The drawback doing it this way in a GUI environment is that you get a console window popping up when pdftk is called.

Sorry the answer is not simple - I wish Adobe provided a nice simple API to manipulate PDF but they don't!


Author Closing Comment

ID: 36522513
good stuff.
Thank you so much

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now