use of com.tom_roush.pdfbox.pdmodel.common.PDStream in project PdfBox-Android by TomRoush.
the class PDDocument method importPage.
/**
* This will import and copy the contents from another location. Currently the content stream is
* stored in a scratch file. The scratch file is associated with the document. If you are adding
* a page to this document from another document and want to copy the contents to this
* document's scratch file then use this method otherwise just use the {@link #addPage addPage()}
* method.
* <p>
* Unlike {@link #addPage addPage()}, this method creates a new PDPage object. If your page has
* annotations, and if these link to pages not in the target document, then the target document
* might become huge. What you need to do is to delete page references of such annotations. See
* <a href="http://stackoverflow.com/a/35477351/535646">here</a> for how to do this.
* <p>
* Inherited (global) resources are ignored because these can contain resources not needed for
* this page which could bloat your document, see
* <a href="https://issues.apache.org/jira/browse/PDFBOX-28">PDFBOX-28</a> and related issues.
* If you need them, call <code>importedPage.setResources(page.getResources());</code>
* <p>
* This method should only be used to import a page from a loaded document, not from a generated
* document because these can contain unfinished parts, e.g. font subsetting information.
*
* @param page The page to import.
* @return The page that was imported.
*
* @throws IOException If there is an error copying the page.
*/
public PDPage importPage(PDPage page) throws IOException {
PDPage importedPage = new PDPage(new COSDictionary(page.getCOSObject()), resourceCache);
PDStream dest = new PDStream(this, page.getContents(), COSName.FLATE_DECODE);
importedPage.setContents(dest);
addPage(importedPage);
importedPage.setCropBox(page.getCropBox());
importedPage.setMediaBox(page.getMediaBox());
importedPage.setRotation(page.getRotation());
if (page.getResources() != null && !page.getCOSObject().containsKey(COSName.RESOURCES)) {
Log.w("PdfBox-Android", "inherited resources of source document are not imported to destination page");
Log.w("PdfBox-Android", "call importedPage.setResources(page.getResources()) to do this");
}
return importedPage;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDStream in project PdfBox-Android by TomRoush.
the class PDXObject method createXObject.
/**
* Creates a new XObject instance of the appropriate type for the COS stream.
*
* @param base The stream which is wrapped by this XObject.
* @param resources
* @return A new XObject instance.
* @throws java.io.IOException if there is an error creating the XObject.
*/
public static PDXObject createXObject(COSBase base, PDResources resources) throws IOException {
if (base == null) {
// TODO throw an exception?
return null;
}
if (!(base instanceof COSStream)) {
throw new IOException("Unexpected object type: " + base.getClass().getName());
}
COSStream stream = (COSStream) base;
String subtype = stream.getNameAsString(COSName.SUBTYPE);
if (COSName.IMAGE.getName().equals(subtype)) {
return new PDImageXObject(new PDStream(stream), resources);
} else if (COSName.FORM.getName().equals(subtype)) {
ResourceCache cache = resources != null ? resources.getResourceCache() : null;
COSDictionary group = (COSDictionary) stream.getDictionaryObject(COSName.GROUP);
if (group != null && COSName.TRANSPARENCY.equals(group.getCOSName(COSName.S))) {
return new PDTransparencyGroup(stream, cache);
}
return new PDFormXObject(stream, cache);
} else if (COSName.PS.getName().equals(subtype)) {
return new PDPostScriptXObject(stream);
} else {
throw new IOException("Invalid XObject Subtype: " + subtype);
}
}
use of com.tom_roush.pdfbox.pdmodel.common.PDStream in project PdfBox-Android by TomRoush.
the class PDOutputIntent method configureOutputProfile.
private PDStream configureOutputProfile(PDDocument doc, InputStream colorProfile) throws IOException {
// ICC_Profile icc = ICC_Profile.getInstance(colorProfile);
// PDStream stream = new PDStream(doc, new ByteArrayInputStream(icc.getData()), COSName.FLATE_DECODE);
// stream.getCOSObject().setInt(COSName.N, icc.getNumComponents()); TODO: PdfBox-Android
PDStream stream = new PDStream(doc, colorProfile, COSName.FLATE_DECODE);
stream.getStream().setInt(COSName.N, 3);
return stream;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDStream in project PdfBox-Android by TomRoush.
the class LayerUtility method importPageAsForm.
/**
* Imports a page from some PDF file as a Form XObject so it can be placed on another page
* in the target document.
* <p>
* You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before invoking the Form XObject to
* make sure that the graphics state is reset.
*
* @param sourceDoc the source PDF document that contains the page to be copied
* @param page the page in the source PDF document to be copied
* @return a Form XObject containing the original page's content
* @throws IOException if an I/O error occurs
*/
public PDFormXObject importPageAsForm(PDDocument sourceDoc, PDPage page) throws IOException {
importOcProperties(sourceDoc);
PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE);
PDFormXObject form = new PDFormXObject(newStream);
// Copy resources
PDResources pageRes = page.getResources();
PDResources formRes = new PDResources();
cloner.cloneMerge(pageRes, formRes);
form.setResources(formRes);
// Transfer some values from page to form
transferDict(page.getCOSObject(), form.getCOSObject(), PAGE_TO_FORM_FILTER, true);
Matrix matrix = form.getMatrix();
AffineTransform at = matrix.createAffineTransform();
PDRectangle mediaBox = page.getMediaBox();
PDRectangle cropBox = page.getCropBox();
PDRectangle viewBox = (cropBox != null ? cropBox : mediaBox);
// Handle the /Rotation entry on the page dict
int rotation = page.getRotation();
// Transform to FOP's user space
// at.scale(1 / viewBox.getWidth(), 1 / viewBox.getHeight());
at.translate(mediaBox.getLowerLeftX() - viewBox.getLowerLeftX(), mediaBox.getLowerLeftY() - viewBox.getLowerLeftY());
switch(rotation) {
case 90:
at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
at.translate(0, viewBox.getWidth());
at.rotate(-Math.PI / 2.0);
break;
case 180:
at.translate(viewBox.getWidth(), viewBox.getHeight());
at.rotate(-Math.PI);
break;
case 270:
at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
at.translate(viewBox.getHeight(), 0);
at.rotate(-Math.PI * 1.5);
break;
default:
}
// Compensate for Crop Boxes not starting at 0,0
at.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
if (!at.isIdentity()) {
form.setMatrix(at);
}
BoundingBox bbox = new BoundingBox();
bbox.setLowerLeftX(viewBox.getLowerLeftX());
bbox.setLowerLeftY(viewBox.getLowerLeftY());
bbox.setUpperRightX(viewBox.getUpperRightX());
bbox.setUpperRightY(viewBox.getUpperRightY());
form.setBBox(new PDRectangle(bbox));
return form;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDStream in project PdfBox-Android by TomRoush.
the class PDCIDFontType2Embedder method buildCIDToGIDMap.
private void buildCIDToGIDMap(Map<Integer, Integer> cidToGid) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int cidMax = Collections.max(cidToGid.keySet());
for (int i = 0; i <= cidMax; i++) {
int gid;
if (cidToGid.containsKey(i)) {
gid = cidToGid.get(i);
} else {
gid = 0;
}
out.write(new byte[] { (byte) (gid >> 8 & 0xff), (byte) (gid & 0xff) });
}
InputStream input = new ByteArrayInputStream(out.toByteArray());
PDStream stream = new PDStream(document, input, COSName.FLATE_DECODE);
cidFont.setItem(COSName.CID_TO_GID_MAP, stream);
}
Aggregations