📄 pdfwriter.java
字号:
entry = (PdfCrossReference) i.next(); stream.write(entry.toPdf(null)); } } catch (IOException ioe) { throw new ExceptionConverter(ioe); } return stream.toByteArray(); } } /** * <CODE>PdfTrailer</CODE> is the PDF Trailer object. * <P> * This object is described in the 'Portable Document Format Reference Manual version 1.3' * section 5.16 (page 59-60). */ class PdfTrailer { // membervariables /** content of the trailer */ private byte[] bytes; // constructors /** * Constructs a PDF-Trailer. * * @param size the number of entries in the <CODE>PdfCrossReferenceTable</CODE> * @param offset offset of the <CODE>PdfCrossReferenceTable</CODE> * @param root an indirect reference to the root of the PDF document * @param info an indirect reference to the info object of the PDF document */ PdfTrailer(int size, int offset, PdfIndirectReference root, PdfIndirectReference info, PdfIndirectReference encryption, PdfObject fileID) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { stream.write(getISOBytes("trailer\n")); PdfDictionary dictionary = new PdfDictionary(); dictionary.put(PdfName.SIZE, new PdfNumber(size)); dictionary.put(PdfName.ROOT, root); if (info != null) { dictionary.put(PdfName.INFO, info); } if (encryption != null) dictionary.put(PdfName.ENCRYPT, encryption); if (fileID != null) dictionary.put(PdfName.ID, fileID); stream.write(dictionary.toPdf(null)); stream.write(getISOBytes("\nstartxref\n")); stream.write(getISOBytes(String.valueOf(offset))); stream.write(getISOBytes("\n%%EOF\n")); } catch (IOException ioe) { throw new ExceptionConverter(ioe); } bytes = stream.toByteArray(); } /** * Returns the PDF representation of this <CODE>PdfObject</CODE>. * * @return an array of <CODE>byte</CODE>s */ byte[] toPdf(PdfWriter writer) { 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; public static final PdfName DOCUMENT_CLOSE = PdfName.DC; public static final PdfName WILL_SAVE = PdfName.WS; public static final PdfName DID_SAVE = PdfName.DS; public static final PdfName WILL_PRINT = PdfName.WP; public static final PdfName DID_PRINT = PdfName.DP; public static final PdfName PAGE_OPEN = PdfName.O; public static final PdfName PAGE_CLOSE = PdfName.C; public static final int SIGNATURE_EXISTS = 1; public static final int SIGNATURE_APPEND_ONLY = 2; public static final char VERSION_1_2 = '2'; public static final char VERSION_1_3 = '3'; public static final char VERSION_1_4 = '4'; private static final int VPOINT = 7; /** this is the header of a PDF document */ protected byte[] HEADER = getISOBytes("%PDF-1.4\n%\u00e0\u00e1\u00e2\u00e3\n"); /** byte offset of the Body */ private 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. */ protected 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. The key is the xref and the value is Object[]{PdfName, template}.*/ 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 color number counter for the colors in the document. */ protected int colorNumber = 1; /** The patten number counter for the colors in the document. */ protected int patternNumber = 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(); /** The colors of this document */ protected HashMap documentColors = new HashMap(); /** The patterns of this document */ protected HashMap documentPatterns = new HashMap(); protected HashMap documentShadings = new HashMap(); protected HashMap documentShadingPatterns = new HashMap(); protected ColorDetails patternColorspaceRGB; protected ColorDetails patternColorspaceGRAY; protected ColorDetails patternColorspaceCMYK; protected HashMap documentSpotPatterns = new HashMap(); // membervariables /** body of the PDF document */ protected PdfBody body = new PdfBody(OFFSET, this); /** the pdfdocument object. */ protected PdfDocument pdf; /** The <CODE>PdfPageEvent</CODE> for this document. */ private PdfPageEvent pageEvent; protected PdfEncryption crypto; private HashMap importedPages = new HashMap(); protected PdfReaderInstance currentPdfReaderInstance; /** The PdfIndirectReference to the pages. */ protected ArrayList pageReferences = new ArrayList(); protected int currentPageNumber = 1; /** The defaukt space-char ratio. */ public static final float SPACE_CHAR_RATIO_DEFAULT = 2.5f; /** Disable the inter-character spacing. */ public static final float NO_SPACE_CHAR_RATIO = 10000000f; /** Use the default run direction. */ public static final int RUN_DIRECTION_DEFAULT = 0; /** Do not use bidirectional reordering. */ public static final int RUN_DIRECTION_NO_BIDI = 1; /** Use bidirectional reordering with left-to-right * preferential run direction. */ public static final int RUN_DIRECTION_LTR = 2; /** Use bidirectional reordering with right-to-left * preferential run direction. */ public static final int RUN_DIRECTION_RTL = 3; protected int runDirection = RUN_DIRECTION_NO_BIDI; /** * The ratio between the extra word spacing and the extra character spacing. * Extra word spacing will grow <CODE>ratio</CODE> times more than extra character spacing. */ private float spaceCharRatio = SPACE_CHAR_RATIO_DEFAULT; // 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -