本文整理汇总了Java中org.apache.pdfbox.util.Matrix类的典型用法代码示例。如果您正苦于以下问题:Java Matrix类的具体用法?Java Matrix怎么用?Java Matrix使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Matrix类属于org.apache.pdfbox.util包,在下文中一共展示了Matrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRotateMoveBox
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java">
* Rotate PDF around its center using PDFBox in java
* </a>
* <p>
* This test shows how to rotate the page content and then move its crop box and
* media box accordingly to make it appear as if the content was rotated around
* the center of the crop box.
* </p>
*/
@Test
public void testRotateMoveBox() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf") )
{
PDDocument document = PDDocument.load(resource);
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);
Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0);
cs.transform(matrix);
cs.close();
PDRectangle cropBox = page.getCropBox();
float cx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float cy = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
Point2D.Float newC = matrix.transformPoint(cx, cy);
float tx = (float)newC.getX() - cx;
float ty = (float)newC.getY() - cy;
page.setCropBox(new PDRectangle(cropBox.getLowerLeftX() + tx, cropBox.getLowerLeftY() + ty, cropBox.getWidth(), cropBox.getHeight()));
PDRectangle mediaBox = page.getMediaBox();
page.setMediaBox(new PDRectangle(mediaBox.getLowerLeftX() + tx, mediaBox.getLowerLeftY() + ty, mediaBox.getWidth(), mediaBox.getHeight()));
document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-move-box.pdf"));
}
}
示例2: testMultiPageJFreeChart
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
@Test
public void testMultiPageJFreeChart() throws IOException {
File parentDir = new File("target/test/multipage");
// noinspection ResultOfMethodCallIgnored
parentDir.mkdirs();
File targetPDF = new File(parentDir, "multipage.pdf");
PDDocument document = new PDDocument();
for (int i = 0; i < 4; i++) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 800, 400);
drawOnGraphics(pdfBoxGraphics2D, i);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 30);
matrix.scale(0.7f, 1f);
contentStream.saveGraphicsState();
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.restoreGraphicsState();
contentStream.close();
}
document.save(targetPDF);
document.close();
}
示例3: testRotateCenter
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java">
* Rotate PDF around its center using PDFBox in java
* </a>
* <p>
* This test shows how to rotate the page content around the center of its crop box.
* </p>
*/
@Test
public void testRotateCenter() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf") )
{
PDDocument document = PDDocument.load(resource);
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);
PDRectangle cropBox = page.getCropBox();
float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
cs.transform(Matrix.getTranslateInstance(tx, ty));
cs.transform(Matrix.getRotateInstance(Math.toRadians(45), 0, 0));
cs.transform(Matrix.getTranslateInstance(-tx, -ty));
cs.close();
document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-center.pdf"));
}
}
示例4: testRotateCenterScale
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java">
* Rotate PDF around its center using PDFBox in java
* </a>
* <p>
* This test shows how to rotate the page content around the center of its crop box
* and then crop it to make all previously visible content fit.
* </p>
*/
@Test
public void testRotateCenterScale() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf") )
{
PDDocument document = PDDocument.load(resource);
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);
Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0);
PDRectangle cropBox = page.getCropBox();
float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
Rectangle rectangle = cropBox.transform(matrix).getBounds();
float scale = Math.min(cropBox.getWidth() / (float)rectangle.getWidth(), cropBox.getHeight() / (float)rectangle.getHeight());
cs.transform(Matrix.getTranslateInstance(tx, ty));
cs.transform(matrix);
cs.transform(Matrix.getScaleInstance(scale, scale));
cs.transform(Matrix.getTranslateInstance(-tx, -ty));
cs.close();
document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-center-scale.pdf"));
}
}
示例5: deleteCharsInPath
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
void deleteCharsInPath() {
for (List<TextPosition> list : charactersByArticle) {
List<TextPosition> toRemove = new ArrayList<>();
for (TextPosition text : list) {
Matrix textMatrix = text.getTextMatrix();
Vector start = textMatrix.transform(new Vector(0, 0));
Vector end = new Vector(start.getX() + text.getWidth(), start.getY());
if (linePath.contains(lowerLeftX + start.getX(), lowerLeftY + start.getY()) ||
(checkEndPointToo && linePath.contains(lowerLeftX + end.getX(), lowerLeftY + end.getY()))) {
toRemove.add(text);
}
}
if (toRemove.size() != 0) {
System.out.println(toRemove.size());
list.removeAll(toRemove);
}
}
}
示例6: process
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
@Override
public void process(Operator operator, List<COSBase> arguments)
throws IOException {
if (arguments.size() < 6) {
throw new MissingOperandException(operator, arguments);
}
// concatenate matrix to current transformation matrix
COSNumber a = (COSNumber) arguments.get(0);
COSNumber b = (COSNumber) arguments.get(1);
COSNumber c = (COSNumber) arguments.get(2);
COSNumber d = (COSNumber) arguments.get(3);
COSNumber e = (COSNumber) arguments.get(4);
COSNumber f = (COSNumber) arguments.get(5);
Matrix matrix = new Matrix(a.floatValue(), b.floatValue(), c.floatValue(),
d.floatValue(), e.floatValue(), f.floatValue());
context.getCurrentTransformationMatrix().concatenate(matrix);
}
示例7: process
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
@Override
public void process(Operator operator, List<COSBase> arguments)
throws MissingOperandException {
if (arguments.size() < 6) {
throw new MissingOperandException(operator, arguments);
}
COSNumber a = (COSNumber) arguments.get(0);
COSNumber b = (COSNumber) arguments.get(1);
COSNumber c = (COSNumber) arguments.get(2);
COSNumber d = (COSNumber) arguments.get(3);
COSNumber e = (COSNumber) arguments.get(4);
COSNumber f = (COSNumber) arguments.get(5);
// Set both matrices to
// [ a b 0
// c d 0
// e f 1 ]
Matrix matrix = new Matrix(a.floatValue(), b.floatValue(),
c.floatValue(), d.floatValue(), e.floatValue(), f.floatValue());
context.setTextMatrix(matrix);
context.setTextLineMatrix(matrix.clone());
}
示例8: process
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
@Override
public void process(Operator operator, List<COSBase> arguments)
throws MissingOperandException {
if (arguments.size() < 2) {
throw new MissingOperandException(operator, arguments);
}
Matrix tlm = context.getTextLineMatrix();
if (tlm == null) {
return;
}
COSNumber tx = (COSNumber) arguments.get(0);
COSNumber ty = (COSNumber) arguments.get(1);
Matrix matrix = new Matrix(1, 0, 0, 1, tx.floatValue(), ty.floatValue());
tlm.concatenate(matrix);
context.setTextMatrix(tlm.clone());
}
示例9: computeBoundingBox
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
/**
* Computes the bounding box from given Rectangle2D in device space units.
*
* @param rect
* the chars string.
* @param font
* the current font.
* @param trm
* the current text rendering matrix.
* @return the bounding box from given char string.
*/
protected Rectangle computeBoundingBox(BoundingBox rect, PDFont font,
Matrix trm) {
if (rect != null && font != null) {
Matrix fontMatrix = font.getFontMatrix();
Point ll = new SimplePoint(rect.getLowerLeftX(), rect.getLowerLeftY());
Point ur = new SimplePoint(rect.getUpperRightX(), rect.getUpperRightY());
// glyph space -> text space
transform(ll, fontMatrix);
transform(ur, fontMatrix);
// text space -> device space
transform(ll, trm);
transform(ur, trm);
return new SimpleRectangle(ll, ur);
}
return null;
}
示例10: strikesThrough
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
boolean strikesThrough(TextPosition textPosition)
{
Matrix matrix = textPosition.getTextPos();
// TODO: This is a very simplistic implementation only working for horizontal text without page rotation
// and horizontal rectangular strikeThroughs with p0 at the left bottom and p2 at the right top
// Check if rectangle horizontally matches (at least) the text
if (p0.getX() > matrix.getXPosition() + textPosition.getWidth() * .1f || p2.getX() < matrix.getXPosition() + textPosition.getWidth() * .9f)
return false;
// Check whether rectangle vertically is at the right height to underline
double vertDiff = p0.getY() - matrix.getYPosition();
if (vertDiff < 0 || vertDiff > textPosition.getFont().getFontDescriptor().getAscent() * textPosition.getFontSizeInPt() / 1000.0)
return false;
// Check whether rectangle is small enough to be a line
return Math.abs(p2.getY() - p0.getY()) < 2;
}
示例11: underlines
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
boolean underlines(TextPosition textPosition)
{
Matrix matrix = textPosition.getTextPos();
// TODO: This is a very simplistic implementation only working for horizontal text without page rotation
// and horizontal rectangular underlines with p0 at the left bottom and p2 at the right top
// Check if rectangle horizontally matches (at least) the text
if (p0.getX() > matrix.getXPosition() + textPosition.getWidth() * .1f || p2.getX() < matrix.getXPosition() + textPosition.getWidth() * .9f)
return false;
// Check whether rectangle vertically is at the right height to underline
double vertDiff = p0.getY() - matrix.getYPosition();
if (vertDiff > 0 || vertDiff < textPosition.getFont().getFontDescriptor().getDescent() * textPosition.getFontSizeInPt() / 500.0)
return false;
// Check whether rectangle is small enough to be a line
return Math.abs(p2.getY() - p0.getY()) < 2;
}
示例12: drawImage
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
/**
* Draw an image at the x,y coordinates, with the given size.
*
* @param image
* The image to draw.
* @param x
* The x-coordinate to draw the image.
* @param y
* The y-coordinate to draw the image.
* @param width
* The width to draw the image.
* @param height
* The height to draw the image.
* @throws IOException
* If there is an error writing to the stream.
* @throws IllegalStateException
* If the method was called within a text block.
*/
public void drawImage (final PDImageXObject image,
final float x,
final float y,
final float width,
final float height) throws IOException
{
if (inTextMode)
{
throw new IllegalStateException ("Error: drawImage is not allowed within a text block.");
}
saveGraphicsState ();
final AffineTransform transform = new AffineTransform (width, 0, 0, height, x, y);
transform (new Matrix (transform));
writeOperand (resources.add (image));
writeOperator ((byte) 'D', (byte) 'o');
restoreGraphicsState ();
}
示例13: drawImage
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
AffineTransform tf = new AffineTransform();
tf.concatenate(baseTransform);
tf.concatenate(transform);
tf.concatenate((AffineTransform) xform.clone());
PDImageXObject pdImage = imageEncoder.encodeImage(document, contentStream, img);
try {
contentStreamSaveState();
int imgHeight = img.getHeight(obs);
tf.translate(0, imgHeight);
tf.scale(1, -1);
contentStream.transform(new Matrix(tf));
Object keyInterpolation = renderingHints.get(RenderingHints.KEY_INTERPOLATION);
if (RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR.equals(keyInterpolation))
pdImage.setInterpolate(false);
contentStream.drawImage(pdImage, 0, 0, img.getWidth(obs), imgHeight);
contentStreamRestoreState();
} catch (IOException e) {
throwException(e);
}
return true;
}
示例14: drawText
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
@Override
public void drawText(AttributedCharacterIterator iterator, IFontTextDrawerEnv env)
throws IOException, FontFormatException {
PDPageContentStream contentStream = env.getContentStream();
contentStream.beginText();
Matrix textMatrix = new Matrix();
textMatrix.scale(1, -1);
contentStream.setTextMatrix(textMatrix);
StringBuilder sb = new StringBuilder();
boolean run = true;
while (run) {
Font attributeFont = (Font) iterator.getAttribute(TextAttribute.FONT);
if (attributeFont == null)
attributeFont = env.getFont();
Number fontSize = ((Number) iterator.getAttribute(TextAttribute.SIZE));
if (fontSize != null)
attributeFont = attributeFont.deriveFont(fontSize.floatValue());
PDFont font = applyFont(attributeFont, env);
Paint paint = (Paint) iterator.getAttribute(TextAttribute.FOREGROUND);
if (paint == null)
paint = env.getPaint();
/*
* Apply the paint
*/
env.applyPaint(paint);
boolean isStrikeThrough = TextAttribute.STRIKETHROUGH_ON
.equals(iterator.getAttribute(TextAttribute.STRIKETHROUGH));
boolean isUnderline = TextAttribute.UNDERLINE_ON.equals(iterator.getAttribute(TextAttribute.UNDERLINE));
boolean isLigatures = TextAttribute.LIGATURES_ON.equals(iterator.getAttribute(TextAttribute.LIGATURES));
run = iterateRun(iterator, sb);
String text = sb.toString();
/*
* If we force the text write we may encounter situations where the font can not
* display the characters. PDFBox will throw an exception in this case. We will
* just silently ignore the text and not display it instead.
*/
try {
showTextOnStream(env, contentStream, attributeFont, font, isStrikeThrough, isUnderline, isLigatures,
text);
} catch (IllegalArgumentException e) {
if (font instanceof PDType1Font && !font.isEmbedded()) {
/*
* We tried to use a builtin default font, but it does not have the needed
* characters. So we use a embedded font as fallback.
*/
try {
if (fallbackFontUnknownEncodings == null)
fallbackFontUnknownEncodings = findFallbackFont(env);
if (fallbackFontUnknownEncodings != null) {
env.getContentStream().setFont(fallbackFontUnknownEncodings, attributeFont.getSize2D());
showTextOnStream(env, contentStream, attributeFont, fallbackFontUnknownEncodings,
isStrikeThrough, isUnderline, isLigatures, text);
e = null;
}
} catch (IllegalArgumentException ignored) {
e = ignored;
}
}
if (e != null)
System.err.println("PDFBoxGraphics: Can not map text " + text + " with font "
+ attributeFont.getFontName() + ": " + e.getMessage());
}
}
contentStream.endText();
}
示例15: exportGraphic
import org.apache.pdfbox.util.Matrix; //导入依赖的package包/类
@SuppressWarnings("SpellCheckingInspection")
void exportGraphic(String dir, String name, GraphicsExporter exporter) {
try {
PDDocument document = new PDDocument();
PDFont pdArial = PDFontFactory.createDefaultFont();
File parentDir = new File("target/test/" + dir);
// noinspection ResultOfMethodCallIgnored
parentDir.mkdirs();
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D imageGraphics = image.createGraphics();
exporter.draw(imageGraphics);
imageGraphics.dispose();
ImageIO.write(image, "PNG", new File(parentDir, name + ".png"));
for (Mode m : Mode.values()) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 400, 400);
PdfBoxGraphics2DFontTextDrawer fontTextDrawer = null;
contentStream.beginText();
contentStream.setStrokingColor(0, 0, 0);
contentStream.setNonStrokingColor(0, 0, 0);
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15);
contentStream.setTextMatrix(Matrix.getTranslateInstance(10, 800));
contentStream.showText("Mode " + m);
contentStream.endText();
switch (m) {
case FontTextIfPossible:
fontTextDrawer = new PdfBoxGraphics2DFontTextDrawer();
fontTextDrawer.registerFont(
new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
break;
case DefaultFontText: {
fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
fontTextDrawer.registerFont(
new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
break;
}
case ForceFontText:
fontTextDrawer = new PdfBoxGraphics2DFontTextForcedDrawer();
fontTextDrawer.registerFont(
PdfBoxGraphics2DTestBase.class.getResourceAsStream("DejaVuSerifCondensed.ttf"));
fontTextDrawer.registerFont("Arial", pdArial);
break;
case DefaultVectorized:
default:
break;
}
if (fontTextDrawer != null) {
pdfBoxGraphics2D.setFontTextDrawer(fontTextDrawer);
}
exporter.draw(pdfBoxGraphics2D);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 20);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
matrix.scale(1.5f, 1.5f);
matrix.translate(0, 100);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.close();
}
document.save(new File(parentDir, name + ".pdf"));
document.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}