multivalent.std.adaptor.pdf
public class PDFWriter extends COSSource
Either start with an existing PDF or start from stratch with the appropriate construction. Then add or modify objects, as informed by the Adobe's PDF Reference.
When done, simply writePDF. Any objects not set in this class will be faulted in from the backing PDFReader, if any. Some useful PDF manipulations, such as replacing CCITT FAX images with JBIG2, just need to modify a few objects; for this the convenience method writePDF will invoke the caller before every object, at which time it can be modified before writing.
To encrypt, just set up a PDF encryption dictionary according to the PDF Reference (don't forget point to it in the trailer). See Encrypt for an example. If there is a backing PDF then by default the PDF to be written inherits it encryption settings, if any. Encryption must be set up before writing (writePDF or writeHeader).
To write in Compact format, just set up dictionary according to the Compact PDF Specification.
For greater control, as for PDF concatenation and excerpting and other applications, write parts separately: writeHeader, PDFWriter for each object, PDFWriter
PDFWriter pdfw = new PDFWriter(...); pdfw.writeHeader(); pdfw.writeObject(); // ... pdfw.close();
Version: $Revision: 1.63 $ $Date: 2004/01/02 04:02:37 $
Field Summary | |
---|---|
static NumberFormat | NF Floating point formatter that matches PDF limits. |
static int | PDFOBJ_OVERHEAD |
static int | PDFOBJREF_OVERHEAD |
Constructor Summary | |
---|---|
PDFWriter(File file, PDFReader base)
Modify based on an existing PDF.
| |
PDFWriter(File file, PDFReader base, boolean incremental)
Writes a PDF to a java.io.File, which can be the same as the source PDF file. | |
PDFWriter(RandomAccess w, PDFReader base, boolean incremental) | |
PDFWriter(File file) Creates a PDF from scratch, written to file. | |
PDFWriter(RandomAccess w)
Creates a PDF from scratch.
|
Method Summary | |
---|---|
void | addFilter(Dict stream, String filter, Object parms) Prepends filter to stream. |
IRef | addObject(Object obj)
Adds object by reusing number of deleted object if possible, or else appending to end. |
void | close()
Closes PDFWriter and associated java.io.File or RandomAccess.
|
void | convertType1(String subformat)
Convert embedded Type 1 fonts, if any, to a different format.
|
byte[] | deflateStream(Dict stream, int objnum)
If data would be smaller with Flate compression applied, apply it, set /Filter /FlateDecode and /Length, and return compressed data.
|
Object | getCache(int objnum) Low-level retrieval from table of instantiated objects: if object is not present, null is returned. |
Dict | getCatalog() Returns document /Catalog . |
File | getFile() |
Dict | getInfo() |
int | getMajorVersion() |
int | getMinorVersion() |
int | getObjCnt() Returns number of objects. |
Object | getObject(int objnum, boolean fcache)
Objects are read on demand from the backing PDFReader, if any.
|
Object | getObject(Object ref) |
Object[] | getObjects()
Return array of all objects currently read in base PDFReader or explicitly set by client code.
|
int | getObjGen(int objnum) |
byte | getObjType(int objnum) |
RandomAccess | getRAF() |
byte[] | getStreamData(Object obj) |
Dict | getTrailer() Document trailer. |
void | liftPageTree()
Removes unnecessarily duplicated inherited attributes in page tree.
|
boolean | makeObjectStreams(int start, int end)
Collect non-stream objects into compressed object streams (introduced in PDF 1.5),
in groups of 200 or so.
|
static byte[] | maybeDeflateData(byte[] data)
Deflates data, if compressed size is smaller than original. |
boolean | objEquals(Object o1, Object o2)
Deep equality testing, recursing through arrays and dictionarys and one level of indirect references. |
void | readAllObjects()
Read all remaining objects from backing PDFReader that have not already been read or set by PDFWriter.
|
int[] | refcnt()
Reference count PDF objects to see how many times (and if) an object is used. |
int | refcntRemove()
Reference count and remove unused objects. |
void | removeFilter(Dict stream, String filter) Removes filter and associated DecodeParms from stream. |
void | renumber(int[] newnum)
Renumbers IRef's according to newnum[]
by descending through object tree (rooted at |
int[] | renumberRemove(int[] newnum)
Descends through object tree (rooted at Trailer), renumbering IRef's according to newnum[]
and removing unused objects.
|
void | resetPageTree(List<IRef> leaves)
Rebalances page tree so that each internal node tries to have 20 children and none has no more than 20 children.
|
void | setCompress(boolean b)
Compress objects, or not (for debugging or pedagogical purposes).
|
void | setExact(boolean b)
If false (the default), unpacks objects from objects streams and report object streams themselves as OBJECT_NULL.
|
void | setMinVersion(int major, int minor)
If using a PDF feature introduced since PDF 1.0, use this method to ensure that the document header is correct.
|
void | setMonitor(boolean b) Shows status information. |
void | setObjCnt(int newcnt)
Truncate object list or allocate space for more (addObject automatically allocate space as needed too). |
void | setObject(int num, Object obj)
Set an object to null to take from base PDFReader.
|
void | setObjGen(int objnum, int newgen) |
void | setPassword(String password) Provide either owner or user password for encryption, if any. |
byte[] | writeCommandArray(Cmd[] cmds, boolean prettyprint)
Writes command array back into a byte stream, skipping commands marked invalid.
|
void | writeHeader()
Writes document header: "%PDF-m.n\n%byte/byte/byte/byte\n".
|
StringBuffer | writeInlineImage(Dict params, byte[] data, StringBuffer sb)
Writes inline image with image data into content stream sb. |
long | writeObject(Object obj, int objnum, int objgen)
Writes a top-level object: n g obj contents endobj ,
with applicable encryption, respecting CryptFilter , if any. |
long | writeObject(Object obj, int objnum, int objgen, boolean fplain, Encrypt encrypt)
Low-level write of a top-level object: n g obj contents endobj , encrypting according to encrypt, which can be null for no encryption.
|
StringBuffer | writeObject(Object o, StringBuffer sb, boolean fcrunch)
Writes contents of passed PDF object to StringBuffer that represents a content stream. |
long | writePDF(Observer observer)
Writes data in memory or base PDF to new PDF file, complete with header, xref table, and trailer.
|
long | writePDF() Convience method for writePDF(null) . |
void | writeXref(Dict trailer, int size, long prev, long[] offset, int start, int length)
Writes cross reference table and trailer.
|
void | writeXref(Dict trailer, int size, long prev, long[] offset, int[] start, int[] length) Writes cross reference of multiple sections and trailer. |
Returns: object number assigned
closed
as well.
After closing, the PDFWriter is invalid and should not be used or queried.Returns: null if dictionary is not a stream
null
is returned./Catalog
.Parameters: fcache true if should object be cached
Other algorithms that make object streams represent this adding the object streams as dictionaries, storing their component objects in the STREAM_DATA dictionary key, and replacing the old copies of component objects with a number of class CLASS_OBJSTMC that holds the object number of the object stream and setting its generation to the index of the object in the object stream.
Returns: array such that array[objnum] = reference count.
Returns: number of unused objects
Parameters: newnum is mutated so that object id's point to positions in collapsed array
Returns: offsets array updated to match new object numbers so, in addition to objs_, callers can update parallel arrays
for (int i=0; i
See Also: PDFWriter
Parameters: leaves holds all the pages, in sequence, with all of their attributes explicit (not relying on inheritance from a parent). A convenient way to accumulate this list is to read IRefs from PDFReader and make attributes explicit with PDFReader.
See Also: PDFReader
setMajorVersion
/setMinorVersion
methods to set an arbitrary value since this might be less than the version necessary to reflect the features of PDF in use.)
If you don't care about compatibility with older PDF readers, you can simply set this to the current level of PDF (1.5 as of this writing) at the start.
The minimum version settable is PDF 1.2 (Acrobat 3.0).
This number only goes up, so if you want something else, write your own header.
See Also: readInlineImage
n g obj
contents endobj
,
with applicable encryption, respecting CryptFilter
, if any.n g obj
contents endobj
, encrypting according to encrypt, which can be null for no encryption.
in that follows encrypt setting, ignoring any CryptFilter
.
Content streams should pass their data streams as a byte[]
under the STREAM_DATA key.
If object is a stream and no filter has been applied, applies Flate compression if that results in a smaller object.Returns: file offset of start of object, or 0 if the object has been deleted or object number is 0 (which is a special number reserved by PDF).
Returns: same StringBuffer passed in
null
observer is passed in, it is invoked after fully reading but before writing each PDF object,
with observer being invoked with a two-element array consisting of the PDF object and the object number as an Integer.
If asked to write onto existing file, first writes to temporary file, then deletes and renames.
If an object has not been instantiated, it is instantiated, written, and then cleared in order to free up memory
and thus allow PDFs of any size to be processed in a limited amount of memory.
Completely control writing by writing own version of this method with these steps:
If PDF version is 1.5 or later, writes trailer and xref as a stream, adding an object for it with the highest object number. If object streams are desired, they must be created beforehand, as by invoking PDFWriter.
If Compact PDF writing mode is set writes entire PDF in format that puts almost every object in a single BZip2 or Flate stream. It is more 30 to 60% more compact on large classes of PDF, but does not conform to the PDF 1.5 specification. It is readable with the PDFReader class, the Multivalent Browser, and the Multivalent tools; and it is valid PDF so you can do incremental writes and so on. See Compact PDF format.
After writing, objects may have been mutated or deleted, and therefore should not be accessed.
Returns: length of new PDF file
writePDF(null)
.Parameters: size total number of objects in the file (not just in current xref segment); usually same as getObjCnt
Returns: file position of start of Xref table.