| Method from com.lowagie.text.pdf.PdfCopy Detail: |
public PdfIndirectReference add(PdfOutline outline) {
return null;
}
|
PdfIndirectReference add(PdfPage page,
PdfContents contents) throws PdfException {
return null;
}
|
public void addAnnotation(PdfAnnotation annot) {
}
|
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;
}
Add an imported page to our output |
public void addPage(Rectangle rect,
int rotation) {
PdfRectangle mediabox = new PdfRectangle(rect, rotation);
PageResources resources = new PageResources();
PdfPage page = new PdfPage(mediabox, new HashMap(), resources.getResources(), 0);
page.put(PdfName.TABS, getTabs());
root.addPage(page);
++currentPageNumber;
}
|
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
}
}
}
}
Signals that the Document was closed and that no other
Elements will be added.
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 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);
}
}
Copy the acroform for an input document. Note that you can only have one,
we make no effort to merge them. |
protected PdfArray copyArray(PdfArray in) throws IOException, BadPdfFormatException {
PdfArray out = new PdfArray();
for (Iterator i = in.listIterator(); i.hasNext();) {
PdfObject value = (PdfObject)i.next();
out.add(copyObject(value));
}
return out;
}
Translate a PRArray to a PdfArray. Also translate all of the objects contained
in it |
protected PdfDictionary copyDictionary(PdfDictionary in) throws IOException, BadPdfFormatException {
PdfDictionary out = new PdfDictionary();
PdfObject type = PdfReader.getPdfObjectRelease(in.get(PdfName.TYPE));
for (Iterator it = in.getKeys().iterator(); it.hasNext();) {
PdfName key = (PdfName)it.next();
PdfObject value = in.get(key);
// System.out.println("Copy " + key);
if (type != null && PdfName.PAGE.equals(type)) {
if (!key.equals(PdfName.B) && !key.equals(PdfName.PARENT))
out.put(key, copyObject(value));
}
else
out.put(key, copyObject(value));
}
return out;
}
Translate a PRDictionary to a PdfDictionary. Also translate all of the
objects contained in it. |
protected PdfIndirectReference copyIndirect(PRIndirectReference in) throws IOException, BadPdfFormatException {
PdfIndirectReference theRef;
RefKey key = new RefKey(in);
IndirectReferences iRef = (IndirectReferences)indirects.get(key);
if (iRef != null) {
theRef = iRef.getRef();
if (iRef.getCopied()) {
return theRef;
}
}
else {
theRef = body.getPdfIndirectReference();
iRef = new IndirectReferences(theRef);
indirects.put(key, iRef);
}
PdfObject obj = PdfReader.getPdfObjectRelease(in);
if (obj != null && obj.isDictionary()) {
PdfObject type = PdfReader.getPdfObjectRelease(((PdfDictionary)obj).get(PdfName.TYPE));
if (type != null && PdfName.PAGE.equals(type)) {
return theRef;
}
}
iRef.setCopied();
obj = copyObject(obj);
addToBody(obj, theRef);
return theRef;
}
Translate a PRIndirectReference to a PdfIndirectReference
In addition, translates the object numbers, and copies the
referenced object to the output file.
NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
file they came from, because each file has its own namespace. The translation
we do from their namespace to ours is *at best* heuristic, and guaranteed to
fail under some circumstances. |
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:
case 0:
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;
}
}
Translate a PR-object to a Pdf-object |
protected PdfStream copyStream(PRStream in) throws IOException, BadPdfFormatException {
PRStream out = new PRStream(in, null);
for (Iterator it = in.getKeys().iterator(); it.hasNext();) {
PdfName key = (PdfName) it.next();
PdfObject value = in.get(key);
out.put(key, copyObject(value));
}
return out;
}
Translate a PRStream to a PdfStream. The data part copies itself. |
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);
}
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);
|
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;
}
}
}
|
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);
return theCat;
}
catch (IOException e) {
throw new ExceptionConverter(e);
}
}
|
public PdfImportedPage getImportedPage(PdfReader reader,
int pageNumber) {
if (currentPdfReaderInstance != null) {
if (currentPdfReaderInstance.getReader() != reader) {
try {
currentPdfReaderInstance.getReader().close();
currentPdfReaderInstance.getReaderFile().close();
}
catch (IOException ioe) {
// empty on purpose
}
currentPdfReaderInstance = reader.getPdfReaderInstance(this);
}
}
else {
currentPdfReaderInstance = reader.getPdfReaderInstance(this);
}
return currentPdfReaderInstance.getImportedPage(pageNumber);
}
Grabs a page from the input document |
public boolean isRotateContents() {
return this.rotateContents;
}
Getter for property rotateContents. |
protected int setFromIPage(PdfImportedPage iPage) {
int pageNum = iPage.getPageNumber();
PdfReaderInstance inst = currentPdfReaderInstance = iPage.getPdfReaderInstance();
reader = inst.getReader();
setFromReader(reader);
return pageNum;
}
convenience method. Given an imported page, 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));
}
}
convenience method. Given a reader, set our "globals" |
public void setRotateContents(boolean rotateContents) {
this.rotateContents = rotateContents;
}
Setter for property rotateContents. |