We start with some of the most common functions involved in working with PDF files. This will also allow us to look at some of the basic concepts in working with the Adobe PDF Library.
- Opening a file
- Saving a file
- Opening a PDF file with a password
- Adding and editing annotations
Opening a file
To open a PDF file use the new Document constructor:
Document doc = new Document("samplesdatasample.pdf");
You can define the file name to open as a separate string value:
string filename = "samplesdatasample.pdf";
Document doc = new Document(filename);
In each case, however, the program defines the name of the file to open, and in this example, a partial path, in quotation marks. The sample PDF files used by the programs are stored in the \samples directory.
For Java, opening a file is similar:
String filename = "SamplesDatasample.pdf";
Document doc = new Document(filename);
System.out.println("Opened a document " + filename);
Saving a file
Likewise, many of our sample programs feature C# and Java instructions to save data to a new PDF file, including Watermark, ChangeLayerConfiguration, and ListInfo.
The Watermark program prompts the user to enter the name of a PDF file holding the watermark graphic needed. It applies this graphic to the original document, and saves a new PDF file with the result.
The Document.Save method saves the content to a new PDF file.
doc.Save(SaveFlags.Full, filename);
Opening a file with a password
This code shows how to prompt for a password to open a PDF file, and then validate the password when entered. It is drawn from the sample program DisplayPDFForm.cs, called by DisplayPDF.
Adding and editing annotations
A user can edit a PDF file with Adobe Acrobat or Adobe Reader. With these viewing tools a user can add comments, highlight, underline, or cross out text, and insert images. You can also add links to move within a PDF file, open a web page, or launch a video. These changes and additions are called annotations. The Adobe PDF Library Java and .NET interfaces provide several sample programs that show how to write code to automatically add annotations to PDF files.
Your firm generates PDF files as advertising sheets for several national retail chains, including grocery stores, bakeries, pharmacies, and hardware stores. You use PDF for the flyers because PDF makes it easy for you to use a template with a standard color palette, font, layout, and design for each of these clients. Also, many of your customers prefer to print the flyers so that they can deliver them door to door or provide them as newspaper inserts. But some of your retail clients post the PDF advertising circulars to their web pages or send them out as email attachments. For those shoppers who look at the flyers online, you want the PDF files to be interactive as well.
As your clients are national chains, the same small set of advertisements tend to apply across hundreds of individual retail locations across the country. But each store posts local content on its own web page, so you would generate a PDF advertising flyer that might apply to several dozen grocery stores in a region, and then make custom changes to these files, adding an annotation that will allow a user to click on a link within the PDF file when reading it online. The link would open the store’s individual web page.
The Action sample program embeds a hyperlink within a PDF file that links to a web address. It also creates a rectangle where the hyperlink appears, and provides the coordinates that defines the size and placement of that rectangle.
- Generate the monthly advertising flyer to be used for 65 grocery stores in Illinois, Wisconsin, Indiana, and Michigan.
- Run a script that makes 65 copies of this PDF file, and assigns each one a unique name, incrementally, including a code number in each file name that identifies the individual store where the PDF will be sent. The PDF files are sorted in the server directory by this code number.
- Run the process that adds the URL addresses for the store web page to the PDF files, one by one. The URL addresses for the web pages for each of these 65 stores are kept in a text file, and in the same order as the list of PDF files. The function selects the URL addresses from this text file and adds them, one by one, to the appropriate PDF files. The process also adds a rectangle to the PDF file and places the hyperlink in the middle of this rectangle. The PDF file will include the text “Click here to visit the store’s web page” under the rectangle.
The Action sample program includes the code to create an annotation with the web page address www.datalogics.comproductspdflpdflibrary.asp, and insert it into a rectangle on a PDF file. This is the C# program:
And the matching code in Java: