About Me:

Customizing PDF Portfolio Layouts
Part 7: Adding Resources and Files

Adding Resources and Files

Introduction:

The focus of this article is to show you how to add additional files and resources to the navigator of your PDF Portfolio.

If you remember from part two of this series, your navigator can use resources that are stored in the .nav file. These files must be stored in a folder called "navigator" in the root of the .nav file and you can reference these files from your .swf application through URLs as in the example code below.

<mx:Image source="navigator/ActionScriptHeader.png"/>

But what do you do when you want to allow your users to add their own images, logos for example, and modify other aspects of the navigator and save those into the PDF file for use later? Well, the Acrobat ActionScript API provides the tools to do just that.

Working with Resources:
The ICollection object has three methods related to resources; addResource, deleteResource, and writeResource. You can only add resources when the document permissions allow it to be modified. The Acrobat ActionScript handles this pretty nicely. The permissions property of the collection will help you do that. By using the permissions property, you don't need to differentiate between a writable PDF Portfolio that is being viewed in the Adobe Reader vs. a PDF Portfolio that's been secured. The code below shows how to check for the ability to modify a PDF Portfolio.

if (_host.collection.permissions.check("Document", "Modify") == true) 
{ 
//you can save changes 
} 
else 
{ 
//You can’t 
} 

addResource:
The addResource method will bring up a file browsing dialog prompting the user to select the needed file. File types that are not blacklisted by Acrobat will be added to the resource dictionary of the PDF Portfolio. The resource can later be accessed by any call that can read data from a URL. You can set the path where the resource will be added. It must be a relative path, "/" separated, but with no leading "/" and must start with "navigator/". If the URL does not have a file extension (that is, it does not contain a '.'), then the file extension of the added file (if any) will be appended. If the URL ends with "/", then the entire file name of the selected file will be appended. This makes it pretty easy to find things later. Because there is no way to enumerate the resources, you'll want to devise a way to keep track of what was added or deleted.

deleteResource:
The same thing as above... but in reverse. Basically, you just pass in the path to the resource to be deleted. It must match a path used previously on a successful call to addResource or writeResource. Again, because there is no way to enumerate the resources, you'll want to devise a way to keep track of what was added or deleted.

writeResource:
This one is a little different from addResource in that no dialog is presented. You need to start out with an object that you want to write then convert that to a ByteArray. The method then replaces the contents of the navigator resource if one exists for the given path or creates a new one. The naming and permission rules that I outlined above for also apply.

See it in Action:
If you have Acrobat 9, download this ZIP file to try out a PDF Portfolio that uses this functionality. The zip file contains a PDF file and an image file that can be used to add a header graphic to the PDF Portfolio.You can also modify the title area of the PDF Portfolio and that title will be stored. The adding the image uses addResource. The title text is stored in an XML file called "navigator/config.xml" and the TextArea object uses writeResource method to update that file when a change to the title is made. You can also download the source at the link below.

Files:
The Acrobat 9 ActionScript SDK
Adding Resources and Files project