Thursday, January 24, 2008

The Procedure

First, set your PDF's logical page numbering [Hack #62] to match your document's page numbering. Then, use pdftk to dump this information into a text file, like so:

pdftk mydoc.pdf dump_data output mydoc.data.txt

Next, convert your PDF to plain text with pdftotext:

pdftotext mydoc.pdf mydoc.txt

Create a keyword list from mydoc.txt using kw_catcher, like so:

kw_catcher 12 keywords_only mydoc.txt > mydoc.kw.txt

Edit mydoc.kw.txt to remove duds and add missing keywords. At present, only one keyword is allowed per line. If two or more keywords are adjacent in mydoc.txt, our page_refs program will assemble them into phrases.

Now pull all these together to create a text index using page_refs:

page_refs mydoc.txt mydoc.kw.txt mydoc.data.txt > mydoc.index.txt

Finally, create a PDF from mydoc.index.txt using enscript and ps2pdf:

enscript --columns 2 --font 'Times-Roman@10' \

--header '|INDEX' --header-font 'Times-Bold@14' \

--margins 54:54:36:54 --word-wrap --output - mydoc.index.txt \

| ps2pdf - mydoc.index.pdf

Preparation

Creating a good document Index section is a difficult job performed by professionals. However, an automatically generated index still can be very helpful. Use automatic keywords or select your own keywords. This section will locate their pages, build a reference, and then create PDF pages that you can append to your document. It even uses your PDF's page labels (also known as logical page numbering) to ensure trouble-free lookup.
Download and install pdftotext, kw_index, and pdftk . You must also have enscript (Windows users visit http://gnuwin32.sf.net/packages/enscript.htm) and ps2pdf. ps2pdf comes with Ghostscript. The kw_index package includes the kw_catcher and page_refs programs (and source code) that we use in the following posts.

Copy Tables into a New Document

In Microsoft Word, use the macro to copy a document's tables into a new document. In Word, create the macro like so.

Open the Macros dialog box (Tools >Macro >Macros . . . ). Type CopyTablesIntoNewDocument into the "Macro name:" field, set "Macros in:" to Normal.dot, and click Create.

A window will open where you can enter the macro's code. It already will have two lines of code: Sub CopyTablesIntoNewDocument() and End Sub. You don't need to duplicate these lines.

You can download the code from http://www.pdfhacks.com/copytables/:

Run this macro from Word by selecting Tools >Macro>Macro . . . , selecting Copy Tables Into New Document, and clicking Run. A new document will open that contains all the tables from your current document. It will also include the paragraphs immediately before and after each table. This feature was added to help readers find the table they want. Modify the macro code to suit your requirements.

Saturday, January 19, 2008

Attachments and Encryption

When you encrypt a PDF, you also encrypt its attachments. The permissions you apply can affect whether users can unpack these attachments.
Once the PDF is open in Acrobat/Reader (which might require a password), any files attached to PDF pages can be unpacked, regardless of the PDF's permissions. This enables you to disable copy/paste features, yet still make select data available to your readers.

Document attachments are more restricted than page attachments. You must grant the ModifyAnnotations permission if you want your readers to be able to unpack and view document attachments.

Attach Files to PDFs with pdftk

pdftk can attach files to PDF documents and pages.

When attaching files to an existing PDF, call pdftk like so:

pdftk attach_file \

[to_page ] output

The output filename must be different from the input filename. For example, attach the file data.xls to the first page of the PDF report.pdf like so:

pdftk report.pdf attach_file data.xls to_page 1 output report.page_attachment.pdf

Attach data.xls to report.pdf as a document attachment instead of a page attachment by simply omitting the to_page parameter:

pdftk report.pdf attach_file data.xls output report.doc_attachment.pdf

You can include additional output parameters, too, such as PDF encryption options.

PDF Attachment

PDF provides a convenient package for your document. A typical PDF contains fonts, images, page streams, annotations, and metadata. It turns out that you can pack anything into a PDF file, even the source document used to create the PDF! These attachments enjoy the benefits of PDF features such as compression, encryption, and digital signatures. Attachments also enable you to provide your readers with document data, such as tables, in a native file format that they can easily use. People often ask]. Attach your document data as HTML or Excel files and give your readers exactly what they need.

Page Attachments Versus Document Attachments
You can attach a file to a particular PDF page, where it is visible as an icon. Or, you can attach a file to the PDF document so that it keeps a lower profile. After encrypting your PDF, document attachments can't be unpacked without the ModifyAnnotations permission. Page attachments, on the other hand, can be unpacked at any time, regardless of the security permissions you imposed. Of course, the PDF must be opened first, which could require a user password.

Attach Files to a PDF with Acrobat
Attach your file to a PDF page using the Attach File commenting tool. In Acrobat 6, access this tool using the Advanced Commenting toolbar or from the Tools>Advanced> Commenting Attach menu. In Acrobat 5, access this tool using the Commenting toolbar. The Attach File tool button hides under the Note tool button; click the little down arrow to discover it.

Activate the Attach File tool and the cursor becomes a push pin. Click the page where you want the attachment's icon to appear and a file selector dialog opens. Select the file to attach. A properties dialog will open, where you can customize the appearance of your attachment's icon.

As we noted, document attachments are different from page attachments. In Acrobat 6, access document attachments by selecting Document>File Attachments . . . . Select Document>File Attachments and click Import . . . to add an attachment. In Acrobat 5, select File Document Properties>Embedded Data Objects . . . . Click Import . . . to add an attachment.

Wednesday, January 16, 2008

How to Add the Decrypt PDF Context Menu Item?

Make sure you have downloaded pdftk.
Follow all the steps in the previous post, except name the action Decrypt and replace the cmd.exe arguments in step 4 with:


C:\windows\system32\cmd.exe

/C C:\windows\system32\pdftk.exe "%1" input_pw PROMPT

output "%1.decrypted.pdf"