use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class TestLayerUtility method createOverlay1.
private File createOverlay1() throws IOException {
File targetFile = new File(testResultsDir, "overlay1.pdf");
PDDocument doc = new PDDocument();
try {
// Create new page
PDPage page = new PDPage();
doc.addPage(page);
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
page.setResources(resources);
}
// Setup page content stream and paint background/title
PDPageContentStream contentStream = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.OVERWRITE, false);
PDFont font = PDType1Font.HELVETICA_BOLD;
contentStream.setNonStrokingColor(AWTColor.LIGHT_GRAY);
contentStream.beginText();
float fontSize = 96;
contentStream.setFont(font, fontSize);
String text = "OVERLAY";
// float sw = font.getStringWidth(text);
// Too bad, base 14 fonts don't return character metrics.
PDRectangle crop = page.getCropBox();
float cx = crop.getWidth() / 2f;
float cy = crop.getHeight() / 2f;
Matrix transform = new Matrix();
transform.translate(cx, cy);
transform.rotate(Math.toRadians(45));
transform.translate(-190, /* sw/2 */
0);
contentStream.setTextMatrix(transform);
contentStream.showText(text);
contentStream.endText();
contentStream.close();
doc.save(targetFile.getAbsolutePath());
} finally {
doc.close();
}
return targetFile;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDFTextStripper method fillBeadRectangles.
private void fillBeadRectangles(PDPage page) {
beadRectangles = new ArrayList<PDRectangle>();
for (PDThreadBead bead : page.getThreadBeads()) {
if (bead == null) {
// can't skip, because of null entry handling in processTextPosition()
beadRectangles.add(null);
continue;
}
PDRectangle rect = bead.getRectangle();
// bead rectangle is in PDF coordinates (y=0 is bottom),
// glyphs are in image coordinates (y=0 is top),
// so we must flip
PDRectangle mediaBox = page.getMediaBox();
float upperRightY = mediaBox.getUpperRightY() - rect.getLowerLeftY();
float lowerLeftY = mediaBox.getUpperRightY() - rect.getUpperRightY();
rect.setLowerLeftY(lowerLeftY);
rect.setUpperRightY(upperRightY);
// adjust for cropbox
PDRectangle cropBox = page.getCropBox();
if (cropBox.getLowerLeftX() != 0 || cropBox.getLowerLeftY() != 0) {
rect.setLowerLeftX(rect.getLowerLeftX() - cropBox.getLowerLeftX());
rect.setLowerLeftY(rect.getLowerLeftY() - cropBox.getLowerLeftY());
rect.setUpperRightX(rect.getUpperRightX() - cropBox.getLowerLeftX());
rect.setUpperRightY(rect.getUpperRightY() - cropBox.getLowerLeftY());
}
beadRectangles.add(rect);
}
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PageDrawer method shadingFill.
@Override
public void shadingFill(COSName shadingName) throws IOException {
PDShading shading = getResources().getShading(shadingName);
if (shading == null) {
Log.e("PdfBox-Android", "shading " + shadingName + " does not exist in resources dictionary");
return;
}
Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
// Paint paint = shading.toPaint(ctm);
// paint = applySoftMaskToPaint(paint, getGraphicsState().getSoftMask());
// graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
// graphics.setPaint(paint);
// graphics.setClip(null);
// lastClip = null;
// get the transformed BBox and intersect with current clipping path
// need to do it here and not in shading getRaster() because it may have been rotated
PDRectangle bbox = shading.getBBox();
if (bbox != null) {
// Area bboxArea = new Area(bbox.transform(ctm));
// bboxArea.intersect(getGraphicsState().getCurrentClippingPath());
// graphics.fill(bboxArea);
} else {
// graphics.fill(getGraphicsState().getCurrentClippingPath());
}
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class Overlay method createOverlayStream.
private COSStream createOverlayStream(PDPage page, LayoutPage layoutPage, COSName xObjectId) throws IOException {
// create a new content stream that executes the XObject content
StringBuilder overlayStream = new StringBuilder();
overlayStream.append("q\nq\n");
PDRectangle overlayMediaBox = new PDRectangle(layoutPage.overlayMediaBox.getCOSArray());
if (layoutPage.overlayRotation == 90 || layoutPage.overlayRotation == 270) {
overlayMediaBox.setLowerLeftX(layoutPage.overlayMediaBox.getLowerLeftY());
overlayMediaBox.setLowerLeftY(layoutPage.overlayMediaBox.getLowerLeftX());
overlayMediaBox.setUpperRightX(layoutPage.overlayMediaBox.getUpperRightY());
overlayMediaBox.setUpperRightY(layoutPage.overlayMediaBox.getUpperRightX());
}
AffineTransform at = calculateAffineTransform(page, overlayMediaBox);
double[] flatmatrix = new double[6];
at.getMatrix(flatmatrix);
for (double v : flatmatrix) {
overlayStream.append(float2String((float) v));
overlayStream.append(" ");
}
overlayStream.append(" cm\n");
// if debugging, insert
// 0 0 overlayMediaBox.getHeight() overlayMediaBox.getWidth() re\ns\n
// into the content stream
overlayStream.append(" /");
overlayStream.append(xObjectId.getName());
overlayStream.append(" Do Q\nQ\n");
return createStream(overlayStream.toString());
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class Overlay method calculateAffineTransform.
/**
* Calculate the transform to be used when positioning the overlay. The default implementation
* centers on the destination. Override this method to do your own, e.g. move to a corner, or
* rotate.
*
* @param page The page that will get the overlay.
* @param overlayMediaBox The overlay media box.
* @return The affine transform to be used.
*/
protected AffineTransform calculateAffineTransform(PDPage page, PDRectangle overlayMediaBox) {
AffineTransform at = new AffineTransform();
PDRectangle pageMediaBox = page.getMediaBox();
float hShift = (pageMediaBox.getWidth() - overlayMediaBox.getWidth()) / 2.0f;
float vShift = (pageMediaBox.getHeight() - overlayMediaBox.getHeight()) / 2.0f;
at.translate(hShift, vShift);
return at;
}
Aggregations