Solved

put password to a pdf file in C++

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

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

Thanks
0
Comment
Question by:justinY
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:zvytas
Comment Utility
The following is in C#, but converting should not be a problem:

http://stackoverflow.com/questions/370571/password-protected-pdf-using-c
0
 

Author Comment

by:justinY
Comment Utility
Thanks zvytas. But I dont know C#. Can you give me C++ version?
0
 
LVL 7

Expert Comment

by:tampnic
Comment Utility
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.

Cheers,
 Chris
0
 
LVL 7

Accepted Solution

by:
tampnic earned 125 total points
Comment Utility
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 http://www.pdflabs.com/docs/build-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!

Cheers,
  Chris
0
 

Author Closing Comment

by:justinY
Comment Utility
good stuff.
Thank you so much
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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. …
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…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

17 Experts available now in Live!

Get 1:1 Help Now