Embed Graphics2D into itext without absolute positioning - java

I'm creating PDFs with iText (LGPL) which include some Text and self-drawn (Graphics2D) images.
My current solution is to draw the images on a BufferedImage and then include it in the PDF, which has several drawbacks:
If printed, the images just look ugly, a way to circumvent this is to use larger images, and with 3000*3000 it looks ok. But this leads to the next problem: time. It takes several seconds to compress one image (I haven’t found a way to disable it, and files would be huge without compression).
PdfGraphics2d from iText looks good, but has one major drawback: it’s only able to draw to the background of the PDF, and there seems to be no way to wrap it in some kind of element.
Is there a way to draw on a PDF without having to use absolute positions? I’m using Graphics2d because it’s used also to provide a preview in the UI.

You can wrap a PdfTemplate inside an Image object without losing any of the vector image's quality. In most cases, you'll use the Image object to add raster images to a PDF document as an Image XObject. However, in this case, the PdfTemplate will be added as a Form XObject using its original vector data. Another situation when this happens, is when you add a WMF file; such as file is converted into PDF syntax automatically.

Related

JAVA: How to create "png" image from BufferedImage. Image has to be openable in DevIL library

I need to save BufferedImage as png image. At next step,I will need to open the image in another program(Avisynth). This program is able to open images, which I have drawn in ms-paint, but images created by my java program it is not able to open. Images from my program and from ms-paint are type of png, and in windows seems good. It means I am able to open it, and image contains all what I have drawn.
In external program it throws following error:
Avisynth open failure:
ImageReader error 'Could not open file' in DevIL library.
reading image"C:\Images\mask\mirror.png"
DevIL version 166.
(C:\User\admin\Documents\4555.avs)
here is code. I tried it even with commented line. I found something on google. This message is typical for bad image format. But I do not know how to do the same image like ms-paint does.
BufferedImage img = new BufferedImage(video.getWidth(), video.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) img.getGraphics();
img.getGraphics().setColor(Color.white);
c.paintAll(img.getGraphics());
File f = new File(path + ".png");
opencv_core.IplImage imgs = IplImage.createFrom(img);
opencv_highgui.cvSaveImage(f.getPath(), imgs);
//ImageIO.write(img, "png", f);
In code:
c is JComponent, whitch contains the image, whitch I want to save, and I used JavaCL library
I've looked at your attached images, and I they both seem perfectly valid PNG files, and open fine in any viewer/library that I have tried. They both use standard deflate compression, adaptive filtering and are non-interlaced.
However, there's one important difference, and that is the one written from Java has transparency (alpha channel), while the one from Paint does not. None of the images have transparent pixels, so you could try to just throw away the alpha channel, and see if that helps (only change the first line of your code):
int w = video.getWidth();
int h = video.getHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
TYPE_3BYTE_BGR is what the ImageIO PNGReader returns for an opaque PNG, so it's probably faster, but you could just as well use TYPE_INT_RGB I think.
This small change in code, should produce an image that is closer to what Paint creates. I'm quite confident that this change should produce a PNG that DevIL/Avisynth can read.
(There's also another small difference between the images, that is the Paint image explicitly contains an sRGB chunk (and some other auxiliary chunks), but that shouldn't matter).
Other than that, I see that DevIL is using LibPNG (which is probably the most standard and widely used PNG library out there) to read PNGs, so I do find it kind of strange that it cannot read this particular PNG. But, it could be something about this version, the way its built or something (I'm no C/C++ programmer, nor do I know the library in depth). You should probably talk to (file an issue with) the developers of the library/program about this.
To read/write a BufferedImage, you can use javax.imageio.ImageIO. You can also add the TwelveMonkeys extension which improves the I/O operations with imageio.
An other solution is to use JAI, but it has a memory leak issues when reading images.
The png format is a standard, supported by almost all the libraries/softwares. If Avisynth cannot open an image created with your java software, it is certainly because you saved it into a rare format.

Java Drawing shapes to file of 2D object

I have a play framework application which I want to be able to produce a product label from. I have the label design in illustrator. It consists of a black circle, white writing with a QR code in the middle, also has curved text.
I want to create a high resolution PDF and/or image file of this design on the fly. All most all of the drawing stuff I find for java relates to swing.
Anyone done this?
The basic class which allows creating an image programatically is BufferedImage and the corresponding Graphics2D class. You are not forced to use it with Swing. You can easily convert it to common graphic formats like PNG. Then you can save it as an image file or place it in a generated(e.g. with iText) PDF.
http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
In other words - yes, it can be done.
But if I ware you I would consider exporting the design from Illustrator to a file and use it as a resource in your application. But if you need to scale it programatically you ought to consider using SVG format to avoid loosing quality. Java does not have build-in support for vector images so you should look at
Apache Batik

How can I create a BufferedImage from an SVG path?

I have attempted to find this answer (on Google and Stackoverflow) without success, but I'm sure it must have been asked before, so feel free to point me onwards to the answer if it exists.
Currently, I have a Java servlet that loads a PNG from disk into a BufferedImage, writes text on top of it, and then streams back the byte[] to the client.
My desire is to replace the PNG-from-disk with a rendered SVG path, from a collection of icon-paths that I've source online (e.g. "M21.871,9.814 15.684,16.001 21.871,22.188 18.335,25.725 8.612,16.001 18.335,6.276z" and "M22.727,18.242L4.792,27.208l8.966-8.966l-4.483-4.484l17.933-8.966l-8.966,8.966L22.727,18.242z").
I've come across Batik and SVG Salamander, but am struggling to understand how I would accomplish the above with either of them, most specifically, how to render the SVG path into the BufferedImage. I need to be able to specify (a) the dimensions of the image, (b) the fill-color and (c) & (d) the stroke width and color.
Here is an example that basically uses the Transcoder API.

How to remove black border around transparent image in PDF created by iText

I have searched over bunch of sites, and I was unable to find solution for my problem.
This is the problem:
I am making PDF's in Java using iText library.
Everything works fine except one thing.
Transparent PNG images have black/gray border around non-transparent area.
I didn't set any borders in code, and actually I have tried to remove them (with no luck).
Can someone help me how to solve this problem?
The closest answer what I have found is: Resizing an image in asp.net without losing the image quality
But I cannot (don't know) interpret this code in Java.
My code is pretty big to copy/paste, but these are steps:
create document
load image from given path
manipulate image (resize, rotate, positioning)
add image to current page
save pdf file
This is what I have tried also:
http://itext-general.2136553.n4.nabble.com/template/NamlServlet.jtp?macro=print_post&node=2157267
http://itext-general.2136553.n4.nabble.com/template/NamlServlet.jtp?macro=print_post&node=2330200
I have tried more than those 2, but I didn't bookmarked them (none of them worked)
Thanks in advance
UPDATE: I forgot to mention that my original pictures don't have border. Border is created somehow by iText. I initially thought that it was bug, but since iText 5.0.2 this problem remained so now I doubt that is bug (I am currently using 5.1.3).
UPDATE 2 I forgot to add this link: http://itext-general.2136553.n4.nabble.com/template/NamlServlet.jtp?macro=print_post&node=2157261
Here is presented VB script that works, but I cannot convert to Java code (it still draws black border), so can someone help me at least with this to convert good?
You could use the java BufferedImage method, getSubImage(x, y, w, h) which allows you to crop a sub image out of an existing image. That way you could cut out the edges.
See here: Class BufferedImage

Images are black in PDF

I'm creating piecharts using JFreeChart, use chart.createBufferedImage(width,height) and give the buffered image to IReport as an image parameter. In IReport I have an image and its image expression points to this parameter, so I can render the image. But when exported to PDF, images are covered with a black rectangle. Something about transparency or RGB, I guess.
There's a thread here about this problem and some suggested solutions, but I couldn't apply them to my problem, will you help me? Thanks.
The thread: http://www2.jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=21922&page=2#24710
As suggested by #Pekka, this is likely a limitation of Transparency in PDF files.
I can't find a way to set RGB or trasparency values from JFreeChart.
The JFreeChart class includes a createBufferedImage() method that accepts an imageType, which is subsequently used to create the BufferedImage . You may have to select the optimal one empirically.

Categories

Resources