Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

Chapter 16. PDF streams > Embedding files into a PDF - Pg. 533

Embedding files into a PDF PdfIndirectObject objref = stamper.getWriter().addToBody(stream); font.put(PdfName.FONTFILE2, objref.getIndirectReference()); } } stamper.close(); 533 Adds stream to writer Adds reference to stream In this listing, you're adding the complete font file. You add the reference to the stream using the FONTFILE2 key because you know in advance that the font has True- Type outlines. That's not the only assumption you make. You also assume that the met- rics of the font that is used in the PDF correspond to the metrics of the new font you're embedding. When we talked about parsing PDF s, I explained that we could only make a fair attempt, but that the functionality could fail for PDF s using exotic encodings. Several warnings that were mentioned in section 15.3.1 also apply here. In real-world exam- ples, replacing one font with another can be very difficult. Now that you know what a PDF looks like on the inside, these examples comple- ment your knowledge about images (discussed in chapter 10) and fonts (chapter 11). In the sections that follow, we'll take a close look at annotations (chapter 7) that are associated with a PDF stream. 16.2 Embedding files into a PDF You've already created a document with file attachment annotations in section 7.3.3. You can embed different files of any type--images, Word documents, XML files, other PDF files--into a PDF document as an annotation, but there's also an alternative way to do this. In this section, we'll briefly return to file attachment annotations, and you'll learn about document-level attachments and create actions to open these annotations. We'll also discuss the concept of portable collections. 16.2.1 File attachment annotations Figure 16.2 shows a list of Kubrick movies available in video stores. There's a pushpin next to every movie title, and if you click the pushpin, the movie poster is shown. All the file attachments are also listed in the file attachments panel at the bottom. The next listing demonstrates how you can extract the attached files by looping over all the pages of the document, inspecting the /Annots array.