This is an old revision of the document!
Changing existing PDFs
To change an existing PDF, the file has to be read with:
aFile := PDF.File read: <aFilename>
The file can then be converted to a Document, an object which can write itself as a PDF file by:
aDocument := aFile asDocument
After changing things in aDocument
, the PDF file is written out with:
<aDocument> write: <aFilename>
Details
The class PDF.File is for reading PDFs from files. It does so incrementally by just reading objects from disk when they are needed. On can see that in the PDFExplorer:
The inital object read from a PDF is the /Trailer
. Apart from some internal bookkeeping attributes, a trailer contains the /Root
with a reference to the Catalog (the contents of the PDF), the /Info and the /ID. This trailer is held by a File in the #trailer
instance variable.
The cloning of the PDF is done in the File»asDocument
method:
asDocument "<Document> a new document with the same contents as the receiver for writing out the PDF later" | newDocument info | newDocument := Document new. newDocument root: self trailer Root. info := self trailer Info. info at: #ModDate put: Timestamp now. info at: #Producer put: PDF producerText. newDocument info: info. newDocument previousId: self trailer ID. ^newDocument
For the new document , we just take the /Root
, /Info
and /ID
attributes from the file just read. The /Info
is modified by setting the modification time and overwriting /Producer
with the name and version of the library. This is a good place to make your own modifications to the original /Info.
The /ID
needs special treatment. It is an array with two hash values, where the first identifies the original PDF (there both hashes were the same), while the second changes with every change of the document. Some workflows identify different versions of a document by their first ID value. Therefore, it should be preserved by the new document, which is why we store the old /ID
as #previousId
in the new document.