📄 pdfcopy.java
字号:
PdfName key = (PdfName) it.next();
PdfObject value = in.get(key);
out.put(key, copyObject(value));
}
return out;
}
/**
* Translate a PRArray to a PdfArray. Also translate all of the objects contained
* in it
*/
protected PdfArray copyArray(PdfArray in) throws IOException, BadPdfFormatException {
PdfArray out = new PdfArray();
for (Iterator i = in.getArrayList().iterator(); i.hasNext();) {
PdfObject value = (PdfObject)i.next();
out.add(copyObject(value));
}
return out;
}
/**
* Translate a PR-object to a Pdf-object
*/
protected PdfObject copyObject(PdfObject in) throws IOException,BadPdfFormatException {
if (in == null)
return PdfNull.PDFNULL;
switch (in.type) {
case PdfObject.DICTIONARY:
// System.out.println("Dictionary: " + in.toString());
return copyDictionary((PdfDictionary)in);
case PdfObject.INDIRECT:
return copyIndirect((PRIndirectReference)in);
case PdfObject.ARRAY:
return copyArray((PdfArray)in);
case PdfObject.NUMBER:
case PdfObject.NAME:
case PdfObject.STRING:
case PdfObject.NULL:
case PdfObject.BOOLEAN:
return in;
case PdfObject.STREAM:
return copyStream((PRStream)in);
// return in;
default:
if (in.type < 0) {
String lit = ((PdfLiteral)in).toString();
if (lit.equals("true") || lit.equals("false")) {
return new PdfBoolean(lit);
}
return new PdfLiteral(lit);
}
System.out.println("CANNOT COPY type " + in.type);
return null;
}
}
/**
* convenience method. Given an importedpage, set our "globals"
*/
protected int setFromIPage(PdfImportedPage iPage) {
int pageNum = iPage.getPageNumber();
PdfReaderInstance inst = currentPdfReaderInstance = iPage.getPdfReaderInstance();
reader = inst.getReader();
setFromReader(reader);
return pageNum;
}
/**
* convenience method. Given a reader, set our "globals"
*/
protected void setFromReader(PdfReader reader) {
this.reader = reader;
indirects = (HashMap)indirectMap.get(reader);
if (indirects == null) {
indirects = new HashMap();
indirectMap.put(reader,indirects);
PdfDictionary catalog = reader.getCatalog();
PRIndirectReference ref = null;
PdfObject o = catalog.get(PdfName.ACROFORM);
if (o == null || o.type() != PdfObject.INDIRECT)
return;
ref = (PRIndirectReference)o;
if (acroForm == null) acroForm = body.getPdfIndirectReference();
indirects.put(new RefKey(ref), new IndirectReferences(acroForm));
}
}
/**
* Add an imported page to our output
* @param iPage an imported page
* @throws IOException, BadPdfFormatException
*/
public void addPage(PdfImportedPage iPage) throws IOException, BadPdfFormatException {
int pageNum = setFromIPage(iPage);
PdfDictionary thePage = reader.getPageN(pageNum);
PRIndirectReference origRef = reader.getPageOrigRef(pageNum);
reader.releasePage(pageNum);
RefKey key = new RefKey(origRef);
PdfIndirectReference pageRef;
IndirectReferences iRef = (IndirectReferences)indirects.get(key);
if (iRef != null && !iRef.getCopied()) {
pageReferences.add(iRef.getRef());
iRef.setCopied();
}
pageRef = getCurrentPage();
if (iRef == null) {
iRef = new IndirectReferences(pageRef);
indirects.put(key, iRef);
}
iRef.setCopied();
PdfDictionary newPage = copyDictionary(thePage);
root.addPage(newPage);
++currentPageNumber;
}
/**
* Copy the acroform for an input document. Note that you can only have one,
* we make no effort to merge them.
* @param reader The reader of the input file that is being copied
* @throws IOException, BadPdfFormatException
*/
public void copyAcroForm(PdfReader reader) throws IOException, BadPdfFormatException {
setFromReader(reader);
PdfDictionary catalog = reader.getCatalog();
PRIndirectReference hisRef = null;
PdfObject o = catalog.get(PdfName.ACROFORM);
if (o != null && o.type() == PdfObject.INDIRECT)
hisRef = (PRIndirectReference)o;
if (hisRef == null) return; // bugfix by John Englar
RefKey key = new RefKey(hisRef);
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 (acroForm != null) theCat.put(PdfName.ACROFORM, acroForm);
if (newBookmarks == null || newBookmarks.isEmpty())
return theCat;
PdfDictionary top = new PdfDictionary();
PdfIndirectReference topRef = getPdfIndirectReference();
Object kids[] = SimpleBookmark.iterateOutlines(this, topRef, newBookmarks, false);
top.put(PdfName.FIRST, (PdfIndirectReference)kids[0]);
top.put(PdfName.LAST, (PdfIndirectReference)kids[1]);
top.put(PdfName.COUNT, new PdfNumber(((Integer)kids[2]).intValue()));
addToBody(top, topRef);
theCat.put(PdfName.OUTLINES, topRef);
return theCat;
}
catch (IOException e) {
throw new ExceptionConverter(e);
}
}
/**
* Sets the bookmarks. The list structure is defined in
* <CODE>SimpleBookmark#</CODE>.
* @param outlines the bookmarks or <CODE>null</CODE> to remove any
*/
public void setOutlines(List outlines) {
newBookmarks = outlines;
}
/**
* 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) {
PdfReaderInstance ri = currentPdfReaderInstance;
pdf.close();
super.close();
if (ri != null) {
try {
ri.getReader().close();
ri.getReaderFile().close();
}
catch (IOException ioe) {
// empty on purpose
}
}
}
}
PdfIndirectReference add(PdfImage pdfImage, PdfIndirectReference fixedRef) throws PdfException { return null; }
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;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -