📄 pdfwriter.java
字号:
final byte[] toPdf(PdfEncryption crypto) {
return bytes;
}
}
// static membervariables
/** A viewer preference */
public static final int PageLayoutSinglePage = 1;
/** A viewer preference */
public static final int PageLayoutOneColumn = 2;
/** A viewer preference */
public static final int PageLayoutTwoColumnLeft = 4;
/** A viewer preference */
public static final int PageLayoutTwoColumnRight = 8;
/** A viewer preference */
public static final int PageModeUseNone = 16;
/** A viewer preference */
public static final int PageModeUseOutlines = 32;
/** A viewer preference */
public static final int PageModeUseThumbs = 64;
/** A viewer preference */
public static final int PageModeFullScreen = 128;
/** A viewer preference */
public static final int HideToolbar = 256;
/** A viewer preference */
public static final int HideMenubar = 512;
/** A viewer preference */
public static final int HideWindowUI = 1024;
/** A viewer preference */
public static final int FitWindow = 2048;
/** A viewer preference */
public static final int CenterWindow = 4096;
/** A viewer preference */
public static final int NonFullScreenPageModeUseNone = 8192;
/** A viewer preference */
public static final int NonFullScreenPageModeUseOutlines = 16384;
/** A viewer preference */
public static final int NonFullScreenPageModeUseThumbs = 32768;
/** A viewer preference */
public static final int DirectionL2R = 65536;
/** A viewer preference */
public static final int DirectionR2L = 131072;
/** The mask to decide if a ViewerPreferences dictionary is needed */
static final int ViewerPreferencesMask = 0x3ff00;
/** The operation permitted when the document is opened with the user password */
public static final int AllowPrinting = 4 + 2048;
/** The operation permitted when the document is opened with the user password */
public static final int AllowModifyContents = 8;
/** The operation permitted when the document is opened with the user password */
public static final int AllowCopy = 16;
/** The operation permitted when the document is opened with the user password */
public static final int AllowModifyAnnotations = 32;
/** The operation permitted when the document is opened with the user password */
public static final int AllowFillIn = 256;
/** The operation permitted when the document is opened with the user password */
public static final int AllowScreenReaders = 512;
/** The operation permitted when the document is opened with the user password */
public static final int AllowAssembly = 1024;
/** The operation permitted when the document is opened with the user password */
public static final int AllowDegradedPrinting = 4;
/** Type of encryption */
public static final boolean STRENGTH40BITS = false;
/** Type of encryption */
public static final boolean STRENGTH128BITS = true;
/** this is the header of a PDF document */
private static byte[] HEADER = getISOBytes("%PDF-1.4\n%\u00e0\u00e1\u00e2\u00e3\n");
/** byte offset of the Body */
private static final int OFFSET = HEADER.length;
/** This is the object number of the root. */
private static final int ROOT = 1;
/** This is an indirect reference to the root. */
private static final PdfIndirectReference ROOTREFERENCE = new PdfIndirectReference(PdfObject.DICTIONARY, ROOT);
/** Indirect reference to the root of the document. */
protected PdfPages root = new PdfPages();
/** Dictionary, containing all the images of the PDF document */
protected PdfXObjectDictionary imageDictionary = new PdfXObjectDictionary();
/** The form XObjects in this document. */
protected HashMap formXObjects = new HashMap();
/** The name counter for the form XObjects name. */
protected int formXObjectsCounter = 1;
/** The font number counter for the fonts in the document. */
protected int fontNumber = 1;
/** The direct content in this document. */
protected PdfContentByte directContent;
/** The direct content under in this document. */
protected PdfContentByte directContentUnder;
/** The fonts of this document */
protected HashMap documentFonts = new HashMap();
// membervariables
/** body of the PDF document */
private PdfBody body = new PdfBody(OFFSET);
/** the pdfdocument object. */
private PdfDocument pdf;
/** The <CODE>PdfPageEvent</CODE> for this document. */
private PdfPageEvent pageEvent;
private PdfEncryption crypto;
// constructor
/**
* Constructs a <CODE>PdfWriter</CODE>.
* <P>
* Remark: a PdfWriter can only be constructed by calling the method
* <CODE>getInstance(Document document, OutputStream os)</CODE>.
*
* @param document The <CODE>PdfDocument</CODE> that has to be written
* @param os The <CODE>OutputStream</CODE> the writer has to write to.
*/
protected PdfWriter(PdfDocument document, OutputStream os) {
super(document, os);
pdf = document;
directContent = new PdfContentByte(this);
directContentUnder = new PdfContentByte(this);
}
// get an instance of the PdfWriter
/**
* Gets an instance of the <CODE>PdfWriter</CODE>.
*
* @param document The <CODE>Document</CODE> that has to be written
* @param os The <CODE>OutputStream</CODE> the writer has to write to.
* @return a new <CODE>PdfWriter</CODE>
*
* @throws DocumentException on error
*/
public static PdfWriter getInstance(Document document, OutputStream os)
throws DocumentException {
PdfDocument pdf = new PdfDocument();
document.addDocListener(pdf);
PdfWriter writer = new PdfWriter(pdf, os);
pdf.addWriter(writer);
return writer;
}
/** Gets an instance of the <CODE>PdfWriter</CODE>.
*
* @return a new <CODE>PdfWriter</CODE>
* @param document The <CODE>Document</CODE> that has to be written
* @param os The <CODE>OutputStream</CODE> the writer has to write to.
* @param listener A <CODE>DocListener</CODE> to pass to the PdfDocument.
* @throws DocumentException on error
*/
public static PdfWriter getInstance(Document document, OutputStream os, DocListener listener)
throws DocumentException {
PdfDocument pdf = new PdfDocument();
pdf.addDocListener(listener);
document.addDocListener(pdf);
PdfWriter writer = new PdfWriter(pdf, os);
pdf.addWriter(writer);
return writer;
}
// methods to write objects to the outputstream
/**
* Adds some <CODE>PdfContents</CODE> to this Writer.
* <P>
* The document has to be open before you can begin to add content
* to the body of the document.
*
* @return a <CODE>PdfIndirectReference</CODE>
* @param page the <CODE>PdfPage</CODE> to add
* @param contents the <CODE>PdfContents</CODE> of the page
* @throws PdfException on error
*/
public PdfIndirectReference add(PdfPage page, PdfContents contents) throws PdfException {
if (!open) {
throw new PdfException("The document isn't open.");
}
PdfIndirectObject object = body.add(contents);
try {
object.writeTo(os);
os.flush();
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
page.add(object.getIndirectReference());
page.setParent(ROOTREFERENCE);
PdfIndirectObject pageObject = body.add(page);
try {
pageObject.writeTo(os);
os.flush();
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
root.add(pageObject.getIndirectReference());
return pageObject.getIndirectReference();
}
/**
* Writes a <CODE>PdfImage</CODE> to the outputstream.
*
* @param pdfImage the image to be added
* @return a <CODE>PdfIndirectReference</CODE> to the encapsulated image
* @throws PdfException when a document isn't open yet, or has been closed
*/
public PdfIndirectReference add(PdfImage pdfImage) throws PdfException {
if (! imageDictionary.contains(pdfImage)) {
PdfIndirectObject object = body.add(pdfImage);
try {
object.writeTo(os);
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
imageDictionary.put(pdfImage.name(), object.getIndirectReference());
return object.getIndirectReference();
}
return (PdfIndirectReference) imageDictionary.get(pdfImage.name());
}
/**
* return the <CODE>PdfIndirectReference</CODE> to the image with a given name.
*
* @param name the name of the image
* @return a <CODE>PdfIndirectReference</CODE>
*/
public PdfIndirectReference getImageReference(PdfName name) {
return (PdfIndirectReference) imageDictionary.get(name);
}
/**
* Writes a <CODE>PdfOutline</CODE> to the outputstream.
*
* @return a <CODE>PdfIndirectReference</CODE> to the encapsulated outline
* @param outline the outline to be written
* @throws PdfException when a document isn't open yet, or has been closed
*/
public PdfIndirectReference add(PdfOutline outline) throws PdfException {
PdfIndirectObject object = body.add(outline);
try {
object.writeTo(os);
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
return object.getIndirectReference();
}
// methods to open and close the writer
/**
* Signals that the <CODE>Document</CODE> has been opened and that
* <CODE>Elements</CODE> can be added.
* <P>
* When this method is called, the PDF-document header is
* written to the outputstream.
*/
public void open() {
try {
os.write(HEADER);
}
catch(IOException ioe) {
}
}
/**
* Signals that the <CODE>Document</CODE> was closed and that no other
* <CODE>Elements</CODE> will be added.
* <P>
* The pages-tree is built and written to the outputstream.
* A Catalog is constructed, as well as an Info-object,
* the referencetable is composed and everything is written
* to the outputstream embedded in a Trailer.
*/
public synchronized void close() {
if (open) {
pdf.close();
try {
// add the fonts
for (Iterator it = documentFonts.values().iterator(); it.hasNext();) {
FontDetails details = (FontDetails)it.next();
details.writeFont(this);
}
// add the form XObjects
for (Iterator it = formXObjects.keySet().iterator(); it.hasNext();) {
PdfTemplate template = (PdfTemplate)it.next();
PdfIndirectObject obj = body.add(template.getFormXObject(), template.getIndirectReference());
obj.writeTo(os);
}
// add the root to the body
PdfIndirectObject rootObject = body.add(root);
rootObject.writeTo(os);
// make the catalog-object and add it to the body
PdfIndirectObject indirectCatalog = body.add(((PdfDocument)document).getCatalog(rootObject.getIndirectReference()));
indirectCatalog.writeTo(os);
// add the info-object to the body
PdfIndirectObject info = body.add(((PdfDocument)document).getInfo());
info.writeTo(os);
PdfIndirectReference encryption = null;
PdfLiteral fileID = null;
if (crypto != null) {
PdfIndirectObject encryptionObject = body.add(crypto.getEncryptionDictionary());
encryptionObject.writeTo(os);
encryption = encryptionObject.getIndirectReference();
fileID = crypto.getFileID();
}
// write the cross-reference table of the body
os.write(body.getCrossReferenceTable());
// make the trailer
PdfTrailer trailer = new PdfTrailer(body.size(),
body.offset(),
indirectCatalog.getIndirectReference(),
info.getIndirectReference(),
encryption,
fileID);
os.write(trailer.toPdf(crypto));
super.close();
}
catch(IOException ioe) {
}
}
}
// methods
/**
* Returns the number of the next object that can be added to the body.
*
* @return the size of the body-object
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -