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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 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