Pdfbox : Draw image in rotated page - java

I have a simple A4 pdf document with a property /Rotate 90 : The original version of my pdf is landscape but printed portrait.
I am trying to draw a small image at the bottom left of the portait document.
Here is my code so far :
File file = new File("rotated90.pdf");
try (final PDDocument doc = PDDocument.load(file)) {
PDPage page = doc.getPage(0);
PDImageXObject image = PDImageXObject.createFromFile("image.jpg", doc);
PDPageContentStream contents = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, false, true);
contents.drawImage(image, 0, 0);
contents.close();
doc.save(new File("newpdf.pdf"));
}
Here is the end result : As you can see the image was placed at the top left (which was the 0,0 coordinate before rotation) and was not rotated.
I tried playing with drawImage(PDImageXObject image, Matrix matrix) without success.
Here is the orignal document pdf with 90° rotation

Here's a solution for a page that is rotated 90°:
PDPageContentStream cs = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true);
PDImageXObject image = ....
cs.saveGraphicsState();
cs.transform(Matrix.getRotateInstance(Math.toRadians(90), page.getCropBox().getWidth() + page.getCropBox().getLowerLeftX(), 0));
cs.drawImage(image, 0, 0);
cs.restoreGraphicsState();
cs.close();
If it is only the image, then you don't need the save/restore.
Solution for a page that is rotated 270°:
cs.transform(Matrix.getRotateInstance(Math.toRadians(270), 0, page.getCropBox().getHeight() + page.getCropBox().getLowerLeftY()));
For 180°:
cs.transform(Matrix.getRotateInstance(Math.toRadians(180), page.getCropBox().getWidth() + page.getCropBox().getLowerLeftX(), page.getCropBox().getHeight() + page.getCropBox().getLowerLeftY()));

Related

Rotate PDF around its center using PDFBox in java

PDDocument document = PDDocument.load(new File(input));
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.PREPEND, false, false);
cs.transform(Matrix.getRotateInstance(Math.toRadians(45), 0, 0));
I am using the above code to rotate the PDF.
For the above image, i am getting following output
From that code, the content of the page has been moving out of the frame and the rotation is not happening around its center. But i want to get the output as
Please suggest me some options. Thanks in advance.
There are two major ways to rotate the page content and make it appear in a viewer as if the rotation happened around the middle of the visible page: Either one actually does rotate around the middle of it by concatenating the rotation with translations or one moves the crop box so that the page area center follow the rotation.
Actually rotating around the center
To do this we envelop the rotation between two translations, the first one moves the origin of the coordinate system to the page center and the second one moves it back again.
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();
(RotatePageContent test testRotateCenter)
Obviously you can multiply the matrices and only add a single transformation to the PDF.
Pulling the crop box along
To do this we calculate the move of the page center and move the boxes accordingly.
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()));
(RotatePageContent test testRotateMoveBox)
Scaling the content down to fit after rotation
If one wants to scale down the rotated content to make it all fit, one can do this as an easy extension of the first variant:
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();
(RotatePageContent test testRotateCenterScale)
Changing the crop box to make all former page area remain visible
If one wants instead to change the crop box to make everything fit without scaling, one can do this as an easy extension of the second variant:
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();
Rectangle rectangle = cropBox.transform(matrix).getBounds();
PDRectangle newBox = new PDRectangle((float)rectangle.getX(), (float)rectangle.getY(), (float)rectangle.getWidth(), (float)rectangle.getHeight());
page.setCropBox(newBox);
page.setMediaBox(newBox);
(RotatePageContent test testRotateExpandBox)
Sample results
The following image shows an output for each of the methods above:
Actually rotating around the center
Scaling the content down to fit after rotation
Pulling the crop box along
Changing the crop box to make all former page area remain visible
Image 4 is not at the same scale as the others, it should show larger.

Convert JPEG to PDF using PDFBox

I did split PDF to JPEG images using PDFBox version 2.0.2. At first, I did just coding as sample like thatÖ
BufferedImage image = pdfRenderer.renderImageWithDPI(pageCounter, 300, ImageType.RGB);
And now, I want to convert this image to PDF, but the image DPI is so large.
I really want to reduce dpi. So I tried this, but it also didn't work:
PDImageXObject pdImageXObject = JPEGFactory.createFromImage(doc, bimg, 0.5f, 100);
How can I reduce DPI?
This is my source code:
InputStream in = new FileInputStream(imagePath);
BufferedImage bimg = ImageIO.read(in);
float width = bimg.getWidth() ;
float height = bimg.getHeight();
PDPage page = new PDPage(new PDRectangle(width, height));
doc.addPage(page);
//PDStream stream = new PDStream(doc, in);
PDImageXObject pdImageXObject = JPEGFactory.createFromImage(doc, bimg, 0.5f, 10);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.drawImage(pdImageXObject, 0, 0);
contentStream.close();
}
} finally {
System.out.println("ddd");
doc.save(pdfPath);
doc.close();
}
}
There are 2 problems:
1) page size. It is not done in pixels but in page units. 1 unit = 1/72 inch. So your rectangle would be calculated like this:
PDPage page = new PDPage(new PDRectangle(width / 300 * 72, height / 300 * 72));
2) scale image. At 300dpi it must be scaled by 72/300 because the 1:1 is 72 dpi.
float scale = 72 / 300;
contentStream.drawImage(pdImage, 0, 0, pdImage.getWidth()*scale, pdImage.getHeight()*scale);
Btw using JPEGFactory is not a good idea, because some quality will be lost. Use LosslessFactory instead.
About your use of the dpi parameter of JPEGFactory - that is just metadata. It doesn't scale anything.
If you really want to "reduce dpi", then render the PDF at 72 dpi instead of 300, then you don't need the scaling when creating the new PDF.
I had a similar problem and I solved it using the following:
PDPage page = new PDPage(PDRectangle.A4);
PDImageXObject pdImage = PDImageXObject.createFromFile(imgFile, doc);
PDPageContentStream contents = new PDPageContentStream(doc, page, false, false);
contents.drawImage(pdImage, 0, 0, PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight());

BufferedImage color saturation

I'm writing a simple scanning application using jfreesane and Apache PDFBox.
Here is the scanning code:
InetAddress address = InetAddress.getByName("192.168.0.17");
SaneSession session = SaneSession.withRemoteSane(address);
List<SaneDevice> devices = session.listDevices();
SaneDevice device = devices.get(0);
device.open();
device.getOption("resolution").setIntegerValue(300);
BufferedImage bimg = device.acquireImage();
File file = new File("test_scan.png");
ImageIO.write(bimg, "png", file);
device.close();
And making PDF:
PDDocument document = new PDDocument();
float width = bimg.getWidth();
float height = bimg.getHeight();
PDPage page = new PDPage(new PDRectangle(width, height));
document.addPage(page);
PDImageXObject pdimg = LosslessFactory.createFromImage(document, bimg);
PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);
stream.drawImage(pdimg, 0, 0);
stream.close();
document.save(filename);
document.close();
And here is the result:
As you can see the PDF image is more "pale" (saturation? - sorry, I'm not good at color theory and don't know how to name it correctly).
What I have found out:
Printing BufferedImage to JLabel using JLabel(new ImageIcon(bimg))
constructor produces the same result as with PDF ("pale" colors)
so I guess PDFBox is not the reason.
Changing scanning resolution -
no effect.
bimg.getTransparency() returns 1 (OPAQUE)
bimg.getType() returns 0 (TYPE_CUSTOM)
PNG file:
http://s000.tinyupload.com/index.php?file_id=95648202713651192395
PDF file
http://s000.tinyupload.com/index.php?file_id=90369236997064329368
There was an issue in JFreeSane with colorspaces, it was fixed in version 0.97:
https://github.com/sjamesr/jfreesane/releases/tag/jfreesane-0.97

How to move image to the top of the PDF page using Apache PDFBox?

I am using PDFBox to generate reports in Java. One of my requirements is to create a PDF document which contains the company logo at the top of the page. I am not able to find the way to accomplish that.
I have the following method in a Java class:
public void createPdf() {
PDDocument document = null;
PDPage page = null;
ServletContext servletContext = (ServletContext) FacesContext
.getCurrentInstance().getExternalContext().getContext();
try {
File f = new File("Afiliado_2.pdf");
if (f.exists() && !f.isDirectory()) {
document = PDDocument.load(new File("Afiliado_2.pdf"));
page = document.getPage(0);
} else {
document = new PDDocument();
page = new PDPage();
document.addPage(page);
}
PDImageXObject pdImage = PDImageXObject.createFromFile(
servletContext.getRealPath("/resources/images/logo.jpg"),
document);
PDPageContentStream contentStream = new PDPageContentStream(
document, page, AppendMode.APPEND, true);
contentStream.drawImage(pdImage, 0, 0);
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly closed:
document.save("Afiliado_2.pdf");
document.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The image is currently appearing in the bottom of the PDF. I know the line I need to modify is contentStream.drawImage(pdImage, 0, 0); but what coordinates do I need to specify so that is appears in the top of the page?
Typically the coordinate system for a page in PDF starts at the lower left corner. So with
contentStream.drawImage(pdImage, 0, 0);
you are drawing your image at that point. You can get the boundaries of your page using
page.getMediaBox();
and use that to position your image e.g.
PDRectangle mediaBox = page.getMediaBox();
// draw with the starting point 1 inch to the left
// and 2 inch from the top of the page
contentStream.drawImage(pdImage, 72, mediaBox.getHeight() - 2 * 72);
where PDF files normally specify 72 points to 1 physical inch.

Draw transparent lines with PDFBox

I would like to draw lines and polygons with transparent lines in PDFBox. Here is some sample code of how I am drawing a blue line, but I cannot figure out to change the alpha value of the color.
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setStrokingColor(66, 177, 230);
contentStream.drawLine(100, 100, 200, 200);
As of PDFBox 2.0 appendRawCommands is deprecated.
float alpha = 0.5f;
PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
graphicsState.setStrokingAlphaConstant(alpha);
stream.setGraphicsStateParameters(graphicsState);
// draw line here
You can achieve this by using a custom extended graphics state:
PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
graphicsState.setStrokingAlphaConstant(0.5f);
COSName graphicsStateName = page.getResources().add(graphicsState);
try (PDPageContentStream cs = new PDPageContentStream(document, page, true, true, true)) {
cs.appendRawCommands("/" + graphicsStateName.getName() + " gs\n");
// draw your line here.
}
You cannot use the alpha value of the java.awt.Color as PDFBox only uses the RGB value. As per javadoc of public void setStrokingColor(Color color) it just:
Set the stroking color, specified as
RGB.
One option could be that you set the background color as the stroking color to make your line invisible.
NOTE - Invisible != Transparent (so you won't get the see through effect)

Categories