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());
Related
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()));
I have successfully converted an Image to Pdf. My issue is that the pdf is displaying half of the width
My Code:
#FXML
private void print() {
try {
WritableImage nodeshot = stackPane.snapshot(new SnapshotParameters(), null);
File file = new File("C:/Users/Andre Kelvin/Desktop/TheNode.png");
ImageIO.write(SwingFXUtils.fromFXImage(nodeshot, null), "png", file);
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
PDImageXObject pdimage;
PDPageContentStream content;
pdimage = PDImageXObject.createFromFile("C:/Users/Andre Kelvin/Desktop/TheNode.png", doc);
content = new PDPageContentStream(doc, page);
content.drawImage(pdimage, 0, 0);
content.close();
doc.addPage(page);
doc.save("C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
doc.close();
file.delete();
//This Line Automatically Opens the user defualt pdf file viewer
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
} catch (Exception e) {
}
}
I have tried getting the root node width and height by using this line:
content.drawImage(pdimage, 0, 0,(float)stackPane.getPrefWidth(),(float)stackPane.getPrefHeight());
and this:
content.drawImage(pdimage, 0, 0,(float)stackPane.getMaxWidth(),(float)stackPane.getMaxHeight());
it will just display a blank white page.
This is the actual image that is Converted to pdf:
And this is the pdf of the image:
Neither the preferred size properties nor their min/max counterparts allow you to reliably determine the size of a Region. Those are just indicators and the calculated values may not match. Furthermore the Region may be resized to sizes other than the preferred size. Last but not least those properties may contain special values Region.USE_PREF_SIZE(=Double.NEGATIVE_INFINITY) and Region.USE_COMPUTED_SIZE(=-1) and even do so by default.
If you need to get the size of a node, use the boundsInLocal property:
Bounds bounds = stackPane.getBoundsInLocal();
In this case it's simpler to get the size of the snapshot instead though.
Furthermore the page size of the PDPage may not be large enough to contain the whole image. You need to scale the image instead or change the page size of the PDPage.
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
This can be done platform-independent using the HostServices available via the Application instance.
Example
#Override
public void start(Stage primaryStage) {
Button button = new Button("print");
StackPane root = new StackPane(button);
button.setOnAction(evt -> {
try {
WritableImage nodeshot = root.snapshot(new SnapshotParameters(), null);
// store image in-memory
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(SwingFXUtils.fromFXImage(nodeshot, null), "png", output);
output.close();
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
PDImageXObject pdimage;
PDPageContentStream content;
pdimage = PDImageXObject.createFromByteArray(doc, output.toByteArray(), "png");
content = new PDPageContentStream(doc, page);
// fit image to media box of page
PDRectangle box = page.getMediaBox();
double factor = Math.min(box.getWidth() / nodeshot.getWidth(), box.getHeight() / nodeshot.getHeight());
float height = (float) (nodeshot.getHeight() * factor);
// beware of inverted y axis here
content.drawImage(pdimage, 0, box.getHeight() - height, (float) (nodeshot.getWidth() * factor), height);
content.close();
doc.addPage(page);
File outputFile = new File("C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
doc.save(outputFile);
doc.close();
getHostServices().showDocument(outputFile.toURI().toString());
} catch (Exception e) {
}
});
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
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
I am generating charts using jfreechart.Now as per my requirement i need to export that chart into pdf using itext in java.Here i am able to export jfreechart to pdf but it is coming at the bottom of the pdf file whereas i want it to be top.
Here is my code..
PdfWriter writer = null;
Document document = new Document();
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
How to set PdfWriter to write at the top of the pdf file.Please help me.
You are adding the template at the bottom of the page:
contentByte.addTemplate(template, 0, 0);
There are different options you can choose from.
You can add the template at another coordinate:
contentByte.addTemplate(template, 36, 400);
This will add the document at position x = 36 and y = 400. In this case, you need to do your math: instead of 400, you should take the top coordinate of the page (e.g. y = 842) minus a margin (e.g. 36) minus the height of the image.
If can be easier to choose a different option:
Image img = Image.getInstance(template);
document.add(img);
Now you're template is added in the flow of the document, just like all other high-level objects (just like Paragraphs and PdfPTables).
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)