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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

752 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