de.intarsys.pdf.content.common.CSContentTools Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpod Show documentation
Show all versions of jpod Show documentation
This is a fork of http://sourceforge.net/projects/jpodlib/ as development seems to be frozen.
We're providing some bug fixes along with deployments to maven.
package de.intarsys.pdf.content.common;
import de.intarsys.pdf.cds.CDSMatrix;
import de.intarsys.pdf.cds.CDSRectangle;
import de.intarsys.pdf.content.CSContent;
import de.intarsys.pdf.content.CSDeviceBasedInterpreter;
import de.intarsys.pdf.content.CSError;
import de.intarsys.pdf.content.CSWarning;
import de.intarsys.pdf.content.ICSExceptionHandler;
import de.intarsys.pdf.cos.COSObject;
import de.intarsys.pdf.pd.PDForm;
import de.intarsys.pdf.pd.PDPage;
import de.intarsys.pdf.pd.PDResources;
import java.awt.geom.Rectangle2D;
/**
* A collection of tools to work with page content.
*/
public class CSContentTools {
private static final ICSExceptionHandler ignoreExceptionHandler = new ICSExceptionHandler() {
@Override
public void error(CSError error) {
// ignore
}
@Override
public void warning(CSWarning warning) {
// ignore
}
};
private CSContentTools() {
}
/**
* Get the rectangle containing all graphics artifacts on the page (stemming
* from the content stream).
*
* @param page The page whose content is evaluated.
* @param border A border width to be added to the clipping rectangle.
* @return Get the rectangle containing all graphics artifacts on the page
* (stemming from the content stream).
*/
public static Rectangle2D getBoundingBoxClipped(PDPage page, double border) {
CSBoundingBoxCollector bbCollector = new CSBoundingBoxCollector();
CSDeviceBasedInterpreter interpreter = new CSDeviceBasedInterpreter(null, bbCollector);
interpreter.setExceptionHandler(ignoreExceptionHandler);
CSContent content = page.getContentStream();
interpreter.process(content, page.getResources());
if (bbCollector.getBoundingBox() != null) {
return bbCollector.getBoundingBox();
} else {
return page.getCropBox().toNormalizedRectangle();
}
}
/**
* Get a {@link PDForm} containing all graphics artifacts on the page
* (stemming from the content stream).
*
* @param page The page whose content is evaluated.
* @param border A border width to be added to the clipping rectangle.
* @return Get a {@link PDForm} containing all graphics artifacts on the
* page (stemming from the content stream).
*/
public static PDForm getFormClipped(PDPage page, double border) {
PDForm form = (PDForm) PDForm.META.createNew();
CSContent content = page.getContentStream();
if (content == null) {
form.setBytes(new byte[0]);
} else {
form.setBytes(content.toByteArray());
}
if (page.getResources() != null) {
COSObject cosResources = page.getResources().cosGetObject().copyDeep();
PDResources resources = (PDResources) PDResources.META.createFromCos(cosResources);
form.setResources(resources);
}
Rectangle2D rect = CSContentTools.getBoundingBoxClipped(page, border);
CDSRectangle bbox = new CDSRectangle(rect);
form.setBoundingBox(bbox);
CDSMatrix matrix = new CDSMatrix();
matrix.translate(-bbox.getLowerLeftX(), -bbox.getLowerLeftY());
// int rotation = PDFGeometryTools.normalizeRotation(page.getRotate());
// if (rotation == 0) {
// } else if (rotation == 90) {
// matrix.rotate(Math.toRadians(-90));
// } else if (rotation == 180) {
// matrix.rotate(Math.toRadians(-180));
// } else if (rotation == 270) {
// matrix.rotate(Math.toRadians(-270));
// } else {
// }
form.setMatrix(matrix);
return form;
}
}