|
Multivalent API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmultivalent.std.adaptor.pdf.COSSource
multivalent.std.adaptor.pdf.PDFWriter
Write new PDF file from low-level data structures.
new PDF based on existing
, modify existing PDF incrementally
,
new PDF from scratch
,
close()
getVersion()
, getTrailer()
, getCatalog()
,
getObjCnt()
, setObjCnt(int)
,
setObject(int, Object)
, addObject(Object)
,
getObject(int, boolean)
, getObject(Object)
, getCache(int)
, readAllObjects()
,
getObjects()
,
getObjGen(int)
, setObjGen(int,int)
, getObjType(int)
,
addFilter(Dict, String, Object)
, removeFilter(Dict, String)
,
deflateStream(Dict, int)
, maybeDeflateData(byte[])
objEquals(Object,Object)
, COSSource.connected(Object)
,
refcnt()
, refcntRemove()
, renumber(int[])
, renumberRemove(int[])
,
resetPageTree(List)
, liftPageTree()
,
#convertType1(int)
setCompress(boolean)
,
makeObjectStreams(int, int)
,
writeHeader()
, writeXref(Dict,int,long, long[],int,int)
/ writeXref(Dict,int,long, long[],int[],int[])
,
writeObject(Object, int, int)
and writeObject(Object, int, int, boolean, Encrypt)
for top-level objects,
writeObject(Object, StringBuffer, boolean)
to content stream,
writeCommandArray(Cmd[], boolean)
, writeInlineImage(Dict, byte[], StringBuffer)
,
writePDF(Observer)
, writePDF()
writeFDF()
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(Observer)
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 tool.pdf.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()
, writeObject(Object, int, int)
for each object, writeXref(Dict,int,long, long[], int, int)
PDFWriter pdfw = new PDFWriter(...); pdfw.writeHeader(); pdfw.writeObject(); // ... pdfw.close();
Notes
Field Summary | |
---|---|
static java.text.NumberFormat |
NF
Floating point formatter that matches PDF limits. |
static int |
PDFOBJ_OVERHEAD
|
static int |
PDFOBJREF_OVERHEAD
|
Constructor Summary | |
---|---|
PDFWriter(java.io.File file,
PDFReader base,
boolean incremental)
|
|
PDFWriter(com.pt.io.OutputUni ou)
Creates a PDF from scratch. |
|
PDFWriter(com.pt.io.OutputUni ou,
PDFReader base)
Convenience for new PDFWriter(out, base, false). |
|
PDFWriter(com.pt.io.OutputUni ou,
PDFReader base,
boolean incremental)
Creates a new PDF based on an existing PDF. |
Method Summary | |
---|---|
void |
addFilter(Dict stream,
java.lang.String filter,
java.lang.Object parms)
Prepends filter to stream. |
IRef |
addObject(java.lang.Object obj)
Adds object by reusing number of deleted object if possible, or else appending to end. |
void |
close()
Closes PDFWriter and associated File or OutputStreamTee . |
void |
convertType1(java.lang.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. |
java.lang.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 . |
Dict |
getInfo()
|
int |
getObjCnt()
Returns number of objects. |
java.lang.Object |
getObject(int objnum,
boolean fcache)
Objects are read on demand from the backing PDFReader, if any. |
java.lang.Object |
getObject(java.lang.Object ref)
|
java.lang.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)
|
com.pt.io.OutputStreamTee |
getOutputStream()
For expert use in special cases. |
PDFReader |
getReader()
|
byte[] |
getStreamData(java.lang.Object obj)
|
Dict |
getTrailer()
Document trailer. |
java.net.URI |
getURI()
|
phelps.util.Version |
getVersion()
Returns the major version of PDF used; for example, for PDF 1.4. |
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(java.lang.Object o1,
java.lang.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 setObject(int, Object) . |
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,
java.lang.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(java.util.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 COS.OBJECT_NULL . |
void |
setMonitor(boolean b)
Shows status information. |
void |
setObjCnt(int newcnt)
Truncate object list or allocate space for more ( addObject(Object) automatically allocate space as needed too). |
void |
setObject(int num,
java.lang.Object obj)
Set an object to null to take from base PDFReader. |
void |
setObjGen(int objnum,
int newgen)
|
void |
setPassword(java.lang.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 |
writeFDF()
Writes contents in Forms Data Format (FDF). |
void |
writeHeader()
Writes document header: "%PDF-m.n\n%byte/byte/byte/byte\n". |
java.lang.StringBuffer |
writeInlineImage(Dict params,
byte[] data,
java.lang.StringBuffer sb)
Writes inline image with image data into content stream sb. |
long |
writeObject(java.lang.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(java.lang.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. |
java.lang.StringBuffer |
writeObject(java.lang.Object o,
java.lang.StringBuffer sb,
boolean fcrunch)
Writes contents of passed PDF object to StringBuffer that represents a content stream. |
java.lang.Object |
writePDF()
Convience method for writePDF(null) . |
java.lang.Object |
writePDF(java.util.Observer observer)
Writes data in memory or base PDF to new PDF file, complete with header, xref 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. |
void |
writeXref(Dict trailer,
int size,
long prev,
long[] offset,
int start,
int length)
Writes cross reference table and trailer. |
Methods inherited from class multivalent.std.adaptor.pdf.COSSource |
---|
connected, getDecodeParms, getObjInt |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int PDFOBJ_OVERHEAD
public static final int PDFOBJREF_OVERHEAD
public static final java.text.NumberFormat NF
Constructor Detail |
---|
public PDFWriter(com.pt.io.OutputUni ou, PDFReader base, boolean incremental) throws java.io.IOException
true
or else to a new file.
To write to a pipe (that is, a PDFReader) pass null
for ou.
The output is allowed to be the same as the input base if writing to a File
.
java.io.IOException
public PDFWriter(com.pt.io.OutputUni ou, PDFReader base) throws java.io.IOException
new PDFWriter(out, base, false).
- Throws:
java.io.IOException
public PDFWriter(java.io.File file, PDFReader base, boolean incremental) throws java.io.IOException
java.io.IOException
public PDFWriter(com.pt.io.OutputUni ou) throws java.io.IOException
java.io.IOException
Method Detail |
---|
public void setExact(boolean b)
COS.OBJECT_NULL
.
If true, reports objects as seen from backing PDFReader
, with objects inside object streams given as a Long
giving their index within the stream.
PDFReader.setExact(boolean)
public void setPassword(java.lang.String password)
public PDFReader getReader()
public java.net.URI getURI()
public com.pt.io.OutputStreamTee getOutputStream()
public phelps.util.Version getVersion()
COSSource
getVersion
in class COSSource
public Dict getTrailer()
getTrailer
in class COSSource
public Dict getCatalog() throws java.io.IOException
/Catalog
.
getCatalog
in class COSSource
java.io.IOException
public Dict getInfo() throws java.io.IOException
java.io.IOException
public int getObjCnt()
getObjCnt
in class COSSource
public void setObjCnt(int newcnt)
addObject(Object)
automatically allocate space as needed too).
public IRef addObject(java.lang.Object obj)
public void setObject(int num, java.lang.Object obj)
IRef
s as top-level objects.
Do not set an object beyond getObjCnt()
, exclusive; first extend the set of objects with setObjCnt(int)
.
To delete an object, set it to COS.OBJECT_DELETED
.
public java.lang.Object getObject(int objnum, boolean fcache) throws java.io.IOException
COS.STREAM_DATA
.
Objects typically requires about 10 times as many bytes in memory as on disk.
Valid object numbers are 0 .. getObjCnt()
-1.
fcache
- true if should object be cached
java.io.IOException
public java.lang.Object getCache(int objnum)
null
is returned.
public java.lang.Object getObject(java.lang.Object ref) throws java.io.IOException
getObject
in class COSSource
java.io.IOException
public byte[] getStreamData(java.lang.Object obj) throws java.io.IOException
java.io.IOException
public void readAllObjects() throws java.io.IOException
setObject(int, Object)
.
Same effect as invoking getObject(int, boolean)
on all objects in backing PDF.
java.io.IOException
public java.lang.Object[] getObjects()
readAllObjects()
.
Invoking this method takes control of the objects from PDFWriter, and results in losing any deleted object chain.
public void setObjGen(int objnum, int newgen)
public int getObjGen(int objnum)
public byte getObjType(int objnum) throws java.io.IOException
java.io.IOException
public void addFilter(Dict stream, java.lang.String filter, java.lang.Object parms) throws java.io.IOException
java.io.IOException
public void removeFilter(Dict stream, java.lang.String filter) throws java.io.IOException
java.io.IOException
public byte[] deflateStream(Dict stream, int objnum) throws java.io.IOException
java.io.IOException
public static byte[] maybeDeflateData(byte[] data) throws java.io.IOException
java.io.IOException
public boolean objEquals(java.lang.Object o1, java.lang.Object o2)
public int[] refcnt()
public int refcntRemove()
public void renumber(int[] newnum)
public int[] renumberRemove(int[] newnum)
newnum
- is mutated so that object id's point to positions in collapsed array
renumber(int[])
public void resetPageTree(java.util.List<IRef> leaves) throws java.io.IOException
liftPageTree()
.
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 IRef
s from PDFReader.getPageRef(int)
and make attributes explicit with PDFReader.getPage(int)
.
java.io.IOException
public void liftPageTree() throws java.io.IOException
java.io.IOException
public void convertType1(java.lang.String subformat) throws java.io.IOException
NFontType1.SUBFORMAT_DECRYPTED
) for low-level inspection.
This does not affect other font formats (Type 1C, TrueType, ...).
java.io.IOException
public void setCompress(boolean b)
public void setMonitor(boolean b)
public boolean makeObjectStreams(int start, int end) throws java.io.IOException
Other algorithms that make object streams represent this
adding the object streams as dictionaries, storing their component objects in the COS.STREAM_DATA
dictionary key,
and replacing the old copies of component objects with a number of class COS.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.
java.io.IOException
public void writeHeader() throws java.io.IOException
java.io.IOException
public void writeXref(Dict trailer, int size, long prev, long[] offset, int start, int length) throws java.io.IOException
makeObjectStreams(int,int)
.
size
- total number of objects in the file (not just in current xref segment); usually same as getObjCnt()
java.io.IOException
public void writeXref(Dict trailer, int size, long prev, long[] offset, int[] start, int[] length) throws java.io.IOException
java.io.IOException
public long writeObject(java.lang.Object obj, int objnum, int objgen) throws java.io.IOException
n g obj
contents endobj
,
with applicable encryption, respecting CryptFilter
, if any.
java.io.IOException
public long writeObject(java.lang.Object obj, int objnum, int objgen, boolean fplain, Encrypt encrypt) throws java.io.IOException
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 COS.STREAM_DATA
key.
If object is a stream and no filter has been applied, applies Flate compression if that results in a smaller object.
java.io.IOException
public java.lang.StringBuffer writeObject(java.lang.Object o, java.lang.StringBuffer sb, boolean fcrunch)
public byte[] writeCommandArray(Cmd[] cmds, boolean prettyprint)
public java.lang.StringBuffer writeInlineImage(Dict params, byte[] data, java.lang.StringBuffer sb)
PDFReader.readInlineImage(InputStreamComposite)
public java.lang.Object writePDF(java.util.Observer observer) throws java.io.IOException
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:
writeHeader()
writeObject(Object, int, int)
s, keeping track of file offset for xref table
writeXref(Dict,int,long, long[], int, int)
table
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 makeObjectStreams(int,int)
.
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.
Long
) or other object for special cases
java.io.IOException
public java.lang.Object writePDF() throws java.io.IOException
writePDF(null)
.
java.io.IOException
public void writeFDF() throws java.io.IOException
java.io.IOException
public void close() throws java.io.IOException
File
or OutputStreamTee
.
If there was a backing PDFReader, any mutated objects are mutated in PDFReader as well,
and therefore in most cases that PDFReader instance should be closed
as well.
After closing, the PDFWriter is invalid and should not be used or queried.
java.io.IOException
|
Multivalent API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |