📄 pdfwriter.java
字号:
public PdfOutline getRootOutline() { return directContent.getRootOutline(); } /** * Adds a <CODE>BaseFont</CODE> to the document and to the page resources. * @param bf the <CODE>BaseFont</CODE> to add * @return the name of the font in the document */ FontDetails add(BaseFont bf) { FontDetails ret = (FontDetails)addSimple(bf); pdf.addFont(ret.getFontName(), ret.getIndirectReference()); return ret; } /** * Adds a <CODE>BaseFont</CODE> to the document but not to the page resources. * It is used for templates. * @param bf the <CODE>BaseFont</CODE> to add * @return an <CODE>Object[]</CODE> where position 0 is a <CODE>PdfName</CODE> * and position 1 is an <CODE>PdfIndirectReference</CODE> */ FontDetails addSimple(BaseFont bf) { FontDetails ret = (FontDetails)documentFonts.get(bf); if (ret == null) { ret = new FontDetails(new PdfName("F" + (fontNumber++)), body.getPdfIndirectReference(), bf); documentFonts.put(bf, ret); } return ret; } void eliminateFontSubset(PdfDictionary fonts) { for (Iterator it = documentFonts.values().iterator(); it.hasNext();) { FontDetails ft = (FontDetails)it.next(); if (fonts.get(ft.getFontName()) != null) ft.setSubset(false); } } /** * Adds a <CODE>SpotColor</CODE> to the document and to the page resources. * @param spc the <CODE>PdfSpotColor</CODE> to add * @return the name of the spotcolor in the document */ ColorDetails add(PdfSpotColor spc) { ColorDetails ret = (ColorDetails)addSimple(spc); pdf.addColor(ret.getColorName(), ret.getIndirectReference()); return ret; } /** * Adds a <CODE>SpotColor</CODE> to the document but not to the page resources. * @param spc the <CODE>SpotColor</CODE> to add * @return an <CODE>Object[]</CODE> where position 0 is a <CODE>PdfName</CODE> * and position 1 is an <CODE>PdfIndirectReference</CODE> */ ColorDetails addSimple(PdfSpotColor spc) { ColorDetails ret = (ColorDetails)documentColors.get(spc); if (ret == null) { ret = new ColorDetails(new PdfName("CS" + (colorNumber++)), body.getPdfIndirectReference(), spc); documentColors.put(spc, ret); } return ret; } ColorDetails addSimplePatternColorspace(Color color) { int type = ExtendedColor.getType(color); if (type == ExtendedColor.TYPE_PATTERN || type == ExtendedColor.TYPE_SHADING) throw new RuntimeException("An uncolored tile pattern can not have another pattern or shding as color."); try { switch (type) { case ExtendedColor.TYPE_RGB: if (patternColorspaceRGB == null) { patternColorspaceRGB = new ColorDetails(new PdfName("CS" + (colorNumber++)), body.getPdfIndirectReference(), null); PdfArray array = new PdfArray(PdfName.PATTERN); array.add(PdfName.DEVICERGB); PdfIndirectObject cobj = body.add(array, patternColorspaceRGB.getIndirectReference()); cobj.writeTo(os); } return patternColorspaceRGB; case ExtendedColor.TYPE_CMYK: if (patternColorspaceCMYK == null) { patternColorspaceCMYK = new ColorDetails(new PdfName("CS" + (colorNumber++)), body.getPdfIndirectReference(), null); PdfArray array = new PdfArray(PdfName.PATTERN); array.add(PdfName.DEVICECMYK); PdfIndirectObject cobj = body.add(array, patternColorspaceCMYK.getIndirectReference()); cobj.writeTo(os); } return patternColorspaceCMYK; case ExtendedColor.TYPE_GRAY: if (patternColorspaceGRAY == null) { patternColorspaceGRAY = new ColorDetails(new PdfName("CS" + (colorNumber++)), body.getPdfIndirectReference(), null); PdfArray array = new PdfArray(PdfName.PATTERN); array.add(PdfName.DEVICEGRAY); PdfIndirectObject cobj = body.add(array, patternColorspaceGRAY.getIndirectReference()); cobj.writeTo(os); } return patternColorspaceGRAY; case ExtendedColor.TYPE_SEPARATION: { ColorDetails details = addSimple(((SpotColor)color).getPdfSpotColor()); ColorDetails patternDetails = (ColorDetails)documentSpotPatterns.get(details); if (patternDetails == null) { patternDetails = new ColorDetails(new PdfName("CS" + (colorNumber++)), body.getPdfIndirectReference(), null); PdfArray array = new PdfArray(PdfName.PATTERN); array.add(details.getIndirectReference()); PdfIndirectObject cobj = body.add(array, patternDetails.getIndirectReference()); cobj.writeTo(os); documentSpotPatterns.put(details, patternDetails); } return patternDetails; } default: throw new RuntimeException("Invalid color type in PdfWriter.addSimplePatternColorspace()."); } } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } void addSimpleShadingPattern(PdfShadingPattern shading) { if (!documentShadingPatterns.containsKey(shading)) { shading.setName(patternNumber); ++patternNumber; documentShadingPatterns.put(shading, null); addSimpleShading(shading.getShading()); } } void addSimpleShading(PdfShading shading) { if (!documentShadings.containsKey(shading)) { documentShadings.put(shading, null); shading.setName(documentShadings.size()); } } /** * Gets the <CODE>PdfDocument</CODE> associated with this writer. * @return the <CODE>PdfDocument</CODE> */ PdfDocument getPdfDocument() { return pdf; } /** * Gets a <CODE>PdfIndirectReference</CODE> for an object that * will be created in the future. * @return the <CODE>PdfIndirectReference</CODE> */ PdfIndirectReference getPdfIndirectReference() { return body.getPdfIndirectReference(); } int getIndirectReferenceNumber() { return body.getIndirectReferenceNumber(); } PdfName addSimplePattern(PdfPatternPainter painter) { PdfName name = (PdfName)documentPatterns.get(painter); try { if ( name == null ) { name = new PdfName("P" + patternNumber); ++patternNumber; documentPatterns.put(painter, name); } } catch (Exception e) { throw new ExceptionConverter(e); } return name; } /** * Adds a template to the document but not to the page resources. * @param template the template to add * @return the <CODE>PdfName</CODE> for this template */ PdfName addDirectTemplateSimple(PdfTemplate template) { PdfIndirectReference ref = template.getIndirectReference(); Object obj[] = (Object[])formXObjects.get(ref); PdfName name = null; try { if (obj == null) { name = new PdfName("Xf" + formXObjectsCounter); ++formXObjectsCounter; if (template.getType() == PdfTemplate.TYPE_IMPORTED) template = null; formXObjects.put(ref, new Object[]{name, template}); } else name = (PdfName)obj[0]; } catch (Exception e) { throw new ExceptionConverter(e); } return name; } /** * Sets the <CODE>PdfPageEvent</CODE> for this document. * @param pageEvent the <CODE>PdfPageEvent</CODE> for this document */ public void setPageEvent(PdfPageEvent pageEvent) { this.pageEvent = pageEvent; } /** * Gets the <CODE>PdfPageEvent</CODE> for this document or <CODE>null</CODE> * if none is set. * @return the <CODE>PdfPageEvent</CODE> for this document or <CODE>null</CODE> * if none is set */ public PdfPageEvent getPageEvent() { return pageEvent; } /** * Adds the local destinations to the body of the document. * @param dest the <CODE>HashMap</CODE> containing the destinations * @throws IOException on error */ void addLocalDestinations(TreeMap dest) throws IOException { for (Iterator i = dest.keySet().iterator(); i.hasNext();) { String name = (String)i.next(); Object obj[] = (Object[])dest.get(name); PdfDestination destination = (PdfDestination)obj[2]; if (destination == null) throw new RuntimeException("The name '" + name + "' has no local destination."); if (obj[1] == null) obj[1] = getPdfIndirectReference(); PdfIndirectObject iob = body.add(destination, (PdfIndirectReference)obj[1]); iob.writeTo(os); } } /** * Gets the current pagenumber of this document. * * @return a page number */ public int getPageNumber() { return pdf.getPageNumber(); } /** * Sets the viewer preferences by ORing some of these constants:<br> * <ul> * <li>The page layout to be used when the document is opened (choose one). * <ul> * <li><b>PageLayoutSinglePage</b> - Display one page at a time. * <li><b>PageLayoutOneColumn</b> - Display the pages in one column. * <li><b>PageLayoutTwoColumnLeft</b> - Display the pages in two columns, with * oddnumbered pages on the left. * <li><b>PageLayoutTwoColumnRight</b> - Display the pages in two columns, with * oddnumbered pages on the right. * </ul> * <li>The page mode how the document should be displayed * when opened (choose one). * <ul> * <li><b>PageModeUseNone</b> - Neither document outline nor thumbnail images visible. * <li><b>PageModeUseOutlines</b> - Document outline visible. * <li><b>PageModeUseThumbs</b> - Thumbnail images visible. * <li><b>PageModeFullScreen</b> - Full-screen mode, with no menu bar, window * controls, or any other window visible. * </ul> * <li><b>HideToolbar</b> - A flag specifying whether to hide the viewer application's tool * bars when the document is active. * <li><b>HideMenubar</b> - A flag specifying whether to hide the viewer application's * menu bar when the document is active. * <li><b>HideWindowUI</b> - A flag specifying whether to hide user interface elements in * the document's window (such as scroll bars and navigation controls), * leaving only the document's contents displayed. * <li><b>FitWindow</b> - A flag specifying whether to resize the document's window to * fit the size of the first displayed page. * <li><b>CenterWindow</b> - A flag specifying whether to position the document's window * in the center of the screen. * <li>The document's page mode, specifying how to display the * document on exiting full-screen mode. It is meaningful only * if the page mode is <b>PageModeFullScreen</b> (choose one). * <ul> * <li><b>NonFullScreenPageModeUseNone</b> - Neither document outline nor thumbnail images * visible * <li><b>NonFullScreenPageModeUseOutlines</b> - Document outline visible * <li><b>NonFullScreenPageModeUseThumbs</b> - Thumbnail images visible * </ul> * </ul> * @param preferences the viewer preferences */ public void setViewerPreferences(int preferences) { pdf.setViewerPreferences(preferences); } /** Sets the encryption options for this document. The userPassword and the * ownerPassword can be null or have zero length. In this case the ownerPassword * is replaced by a random string. The open permissions for the document can be * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations, * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting. * The permissions can be combined by ORing them. * @param userPassword the user password. Can be null or empty * @param ownerPassword the owner password. Can be null or empty * @param permissions the user permissions * @param strength128Bits true for 128 bit key length. false for 40 bit key length * @throws DocumentException if the document is already open */ public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException { if (pdf.isOpen()) throw new DocumentException("Encryption can only be added before opening the document."); crypto = new PdfEncryption(); crypto.setupAllKeys(userPassword, ownerPassword, permissions, strength128Bits); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -