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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

635 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