📄 pdfcopy.java
字号:
PdfIndirectReference myRef; IndirectReferences iRef = (IndirectReferences)indirects.get(key); if (iRef != null) { acroForm = myRef = iRef.getRef(); } else { acroForm = myRef = body.getPdfIndirectReference(); iRef = new IndirectReferences(myRef); indirects.put(key, iRef); } if (! iRef.getCopied()) { iRef.setCopied(); PdfDictionary theForm = copyDictionary((PdfDictionary)PdfReader.getPdfObject(hisRef)); addToBody(theForm, myRef); } } /* * the getCatalog method is part of PdfWriter. * we wrap this so that we can extend it */ protected PdfDictionary getCatalog(PdfIndirectReference rootObj) { try { PdfDictionary theCat = pdf.getCatalog(rootObj); if (fieldArray == null) { if (acroForm != null) theCat.put(PdfName.ACROFORM, acroForm); } else addFieldResources(theCat); writeOutlines(theCat, false); return theCat; } catch (IOException e) { throw new ExceptionConverter(e); } } private void addFieldResources(PdfDictionary catalog) throws IOException { if (fieldArray == null) return; PdfDictionary acroForm = new PdfDictionary(); catalog.put(PdfName.ACROFORM, acroForm); acroForm.put(PdfName.FIELDS, fieldArray); acroForm.put(PdfName.DA, new PdfString("/Helv 0 Tf 0 g ")); if (fieldTemplates.isEmpty()) return; PdfDictionary dr = new PdfDictionary(); acroForm.put(PdfName.DR, dr); for (Iterator it = fieldTemplates.keySet().iterator(); it.hasNext();) { PdfTemplate template = (PdfTemplate)it.next(); PdfFormField.mergeResources(dr, (PdfDictionary)template.getResources()); } if (dr.get(PdfName.ENCODING) == null) dr.put(PdfName.ENCODING, PdfName.WIN_ANSI_ENCODING); PdfDictionary fonts = (PdfDictionary)PdfReader.getPdfObject(dr.get(PdfName.FONT)); if (fonts == null) { fonts = new PdfDictionary(); dr.put(PdfName.FONT, fonts); } if (!fonts.contains(PdfName.HELV)) { PdfDictionary dic = new PdfDictionary(PdfName.FONT); dic.put(PdfName.BASEFONT, PdfName.HELVETICA); dic.put(PdfName.ENCODING, PdfName.WIN_ANSI_ENCODING); dic.put(PdfName.NAME, PdfName.HELV); dic.put(PdfName.SUBTYPE, PdfName.TYPE1); fonts.put(PdfName.HELV, addToBody(dic).getIndirectReference()); } if (!fonts.contains(PdfName.ZADB)) { PdfDictionary dic = new PdfDictionary(PdfName.FONT); dic.put(PdfName.BASEFONT, PdfName.ZAPFDINGBATS); dic.put(PdfName.NAME, PdfName.ZADB); dic.put(PdfName.SUBTYPE, PdfName.TYPE1); fonts.put(PdfName.ZADB, addToBody(dic).getIndirectReference()); } } /** * 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 reference table is composed and everything is written * to the outputstream embedded in a Trailer. */ public void close() { if (open) { PdfReaderInstance ri = currentPdfReaderInstance; pdf.close(); super.close(); if (ri != null) { try { ri.getReader().close(); ri.getReaderFile().close(); } catch (IOException ioe) { // empty on purpose } } } } public PdfIndirectReference add(PdfOutline outline) { return null; } public void addAnnotation(PdfAnnotation annot) { } PdfIndirectReference add(PdfPage page, PdfContents contents) throws PdfException { return null; } public void freeReader(PdfReader reader) throws IOException { indirectMap.remove(reader); if (currentPdfReaderInstance != null) { if (currentPdfReaderInstance.getReader() == reader) { try { currentPdfReaderInstance.getReader().close(); currentPdfReaderInstance.getReaderFile().close(); } catch (IOException ioe) { // empty on purpose } currentPdfReaderInstance = null; } } } /** * Create a page stamp. New content and annotations, including new fields, are allowed. * The fields added cannot have parents in another pages. This method modifies the PdfReader instance.<p> * The general usage to stamp something in a page is: * <p> * <pre> * PdfImportedPage page = copy.getImportedPage(reader, 1); * PdfCopy.PageStamp ps = copy.createPageStamp(page); * ps.addAnnotation(PdfAnnotation.createText(copy, new Rectangle(50, 180, 70, 200), "Hello", "No Thanks", true, "Comment")); * PdfContentByte under = ps.getUnderContent(); * under.addImage(img); * PdfContentByte over = ps.getOverContent(); * over.beginText(); * over.setFontAndSize(bf, 18); * over.setTextMatrix(30, 30); * over.showText("total page " + totalPage); * over.endText(); * ps.alterContents(); * copy.addPage(page); * </pre> * @param iPage an imported page * @return the <CODE>PageStamp</CODE> */ public PageStamp createPageStamp(PdfImportedPage iPage) { int pageNum = iPage.getPageNumber(); PdfReader reader = iPage.getPdfReaderInstance().getReader(); PdfDictionary pageN = reader.getPageN(pageNum); return new PageStamp(reader, pageN, this); } public static class PageStamp { PdfDictionary pageN; PdfCopy.StampContent under; PdfCopy.StampContent over; PageResources pageResources; PdfReader reader; PdfCopy cstp; PageStamp(PdfReader reader, PdfDictionary pageN, PdfCopy cstp) { this.pageN = pageN; this.reader = reader; this.cstp = cstp; } public PdfContentByte getUnderContent(){ if (under == null) { if (pageResources == null) { pageResources = new PageResources(); PdfDictionary resources = (PdfDictionary)PdfReader.getPdfObject(pageN.get(PdfName.RESOURCES)); pageResources.setOriginalResources(resources, cstp.namePtr); } under = new PdfCopy.StampContent(cstp, pageResources); } return under; } public PdfContentByte getOverContent(){ if (over == null) { if (pageResources == null) { pageResources = new PageResources(); PdfDictionary resources = (PdfDictionary)PdfReader.getPdfObject(pageN.get(PdfName.RESOURCES)); pageResources.setOriginalResources(resources, cstp.namePtr); } over = new PdfCopy.StampContent(cstp, pageResources); } return over; } public void alterContents() throws IOException { if (over == null && under == null) return; PdfArray ar = null; PdfObject content = PdfReader.getPdfObject(pageN.get(PdfName.CONTENTS), pageN); if (content == null) { ar = new PdfArray(); pageN.put(PdfName.CONTENTS, ar); } else if (content.isArray()) { ar = (PdfArray)content; } else if (content.isStream()) { ar = new PdfArray(); ar.add(pageN.get(PdfName.CONTENTS)); pageN.put(PdfName.CONTENTS, ar); } else { ar = new PdfArray(); pageN.put(PdfName.CONTENTS, ar); } ByteBuffer out = new ByteBuffer(); if (under != null) { out.append(PdfContents.SAVESTATE); applyRotation(pageN, out); out.append(under.getInternalBuffer()); out.append(PdfContents.RESTORESTATE); } if (over != null) out.append(PdfContents.SAVESTATE); PdfStream stream = new PdfStream(out.toByteArray()); stream.flateCompress(cstp.getCompressionLevel()); PdfIndirectReference ref1 = cstp.addToBody(stream).getIndirectReference(); ar.addFirst(ref1); out.reset(); if (over != null) { out.append(' '); out.append(PdfContents.RESTORESTATE); out.append(PdfContents.SAVESTATE); applyRotation(pageN, out); out.append(over.getInternalBuffer()); out.append(PdfContents.RESTORESTATE); stream = new PdfStream(out.toByteArray()); stream.flateCompress(cstp.getCompressionLevel()); ar.add(cstp.addToBody(stream).getIndirectReference()); } pageN.put(PdfName.RESOURCES, pageResources.getResources()); } void applyRotation(PdfDictionary pageN, ByteBuffer out) { if (!cstp.rotateContents) return; Rectangle page = reader.getPageSizeWithRotation(pageN); int rotation = page.getRotation(); switch (rotation) { case 90: out.append(PdfContents.ROTATE90); out.append(page.getTop()); out.append(' ').append('0').append(PdfContents.ROTATEFINAL); break; case 180: out.append(PdfContents.ROTATE180); out.append(page.getRight()); out.append(' '); out.append(page.getTop()); out.append(PdfContents.ROTATEFINAL); break; case 270: out.append(PdfContents.ROTATE270); out.append('0').append(' '); out.append(page.getRight()); out.append(PdfContents.ROTATEFINAL); break; } } private void addDocumentField(PdfIndirectReference ref) { if (cstp.fieldArray == null) cstp.fieldArray = new PdfArray(); cstp.fieldArray.add(ref); } private void expandFields(PdfFormField field, ArrayList allAnnots) { allAnnots.add(field); ArrayList kids = field.getKids(); if (kids != null) { for (int k = 0; k < kids.size(); ++k) expandFields((PdfFormField)kids.get(k), allAnnots); } } public void addAnnotation(PdfAnnotation annot) { try { ArrayList allAnnots = new ArrayList(); if (annot.isForm()) { PdfFormField field = (PdfFormField)annot; if (field.getParent() != null) return; expandFields(field, allAnnots); if (cstp.fieldTemplates == null) cstp.fieldTemplates = new HashMap(); } else allAnnots.add(annot); for (int k = 0; k < allAnnots.size(); ++k) { annot = (PdfAnnotation)allAnnots.get(k); if (annot.isForm()) { if (!annot.isUsed()) { HashMap templates = annot.getTemplates(); if (templates != null) cstp.fieldTemplates.putAll(templates); } PdfFormField field = (PdfFormField)annot; if (field.getParent() == null) addDocumentField(field.getIndirectReference()); } if (annot.isAnnotation()) { PdfObject pdfobj = PdfReader.getPdfObject(pageN.get(PdfName.ANNOTS), pageN); PdfArray annots = null; if (pdfobj == null || !pdfobj.isArray()) { annots = new PdfArray(); pageN.put(PdfName.ANNOTS, annots); } else annots = (PdfArray)pdfobj; annots.add(annot.getIndirectReference()); if (!annot.isUsed()) { PdfRectangle rect = (PdfRectangle)annot.get(PdfName.RECT); if (rect != null && (rect.left() != 0 || rect.right() != 0 || rect.top() != 0 || rect.bottom() != 0)) { int rotation = reader.getPageRotation(pageN); Rectangle pageSize = reader.getPageSizeWithRotation(pageN); switch (rotation) { case 90: annot.put(PdfName.RECT, new PdfRectangle( pageSize.getTop() - rect.bottom(), rect.left(), pageSize.getTop() - rect.top(), rect.right())); break; case 180: annot.put(PdfName.RECT, new PdfRectangle( pageSize.getRight() - rect.left(), pageSize.getTop() - rect.bottom(), pageSize.getRight() - rect.right(), pageSize.getTop() - rect.top())); break; case 270: annot.put(PdfName.RECT, new PdfRectangle( rect.bottom(), pageSize.getRight() - rect.left(), rect.top(), pageSize.getRight() - rect.right())); break; } } } } if (!annot.isUsed()) { annot.setUsed(); cstp.addToBody(annot, annot.getIndirectReference()); } } } catch (IOException e) { throw new ExceptionConverter(e); } } } public static class StampContent extends PdfContentByte { PageResources pageResources; /** Creates a new instance of StampContent */ StampContent(PdfWriter writer, PageResources pageResources) { super(writer); this.pageResources = pageResources; } /** * Gets a duplicate of this <CODE>PdfContentByte</CODE>. All * the members are copied by reference but the buffer stays different. * * @return a copy of this <CODE>PdfContentByte</CODE> */ public PdfContentByte getDuplicate() { return new PdfCopy.StampContent(writer, pageResources); } PageResources getPageResources() { return pageResources; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -