which Java / Groovy library to overlay text on an image - java

I am working on a web application (using Grails) which will generate a gift certificate. I'm thinking of a workflow like this:
The user will pick a template which will be stored as an image.
Then the text (name, date, amount etc) will be overlaid on the image to make the final certificate. There is a set of co-ordinates associated with each template which describes where to put each bit of text.
There is a kind of 'live preview' in the browser which shows the user what the final certificate will look like.
When the user is happy with the results, they download the certificate as a PDF and print it.
Can anyone recommend a library for Java (or Groovy) that will make it easy to do this? I'm not particularly worried about speed, as I suspect that the webapp will only be used by a few people at a time.
UPDATE: in the end I used the iText PDF library to overlay text on a PDF template using PDFStamper.

You can do this with the standard Java 2D graphics libraries - create a BufferedImage from the image, get its Graphics and use drawString() to put the text on top. Of course, the text would then be part of the bitmap in the resulting PDF, and not use the full printing resolution.

In addition to the answers above, I have come across the groovy library GraphicsBuilder and the Grails plugin j2D which are also potiential solutions.

You might consider using Batik to do this as SVG. Your image would be an <img> tag and your text would be one or more <text> tags. There's a converter (called FOP, I believe) which will get you PDF output.

Related

Using qoppa to create pdf with table structure just like we do using itext pdf library

Sample pdf
A sample pdf is shown in image. We need to create 2 column structure which can have text/images/figures etc. Moreover, we need to change the text format like font/size, auto wrapping etc. Text content will be dynamic as we don't know the content at compile time hence, it should be able to align itself after paragraph ends and we should not need to provide hard coded value for height. Giving absolute positions of components in qoppa to create pdf is not feasible for us because of dynamic content.
We've already explored qoppa library and we couldn't figure out how to make a pdf like shown in image. If anyone has worked on qoppa, Please do share the valuable resources available online related to qoppa. And Please, let me know if it is possible to create a pdf like this using qoppa.

Preview LaTeX output with Java

Right now I'm working on displaying LaTeX generated document with Java.
Strictly speaking, LaTeX source can be used to directly generate two formats:
DVI using latex, the first one to be supported;
PDF using pdflatex, more recent.
However rendering dvi or pdf is not available as far as I know.
Is there any way to handle those formats ? Or maybe others that makes sense ?
There are not enough details with regards to how you wish to "render" DVI or PDF from a LaTeX document. However, you could always just render the pdf using pdflatex and DVI using latex and use ICEpdf for viewing PDFs and javaDVI for viewing DVIs.
Another neat hack to display pdf in a panel is to pass the file path to an embedded web component in the application, and the web component will use whatever pdf rendering tool is available on your machine (Acrobat, Foxit, Preview, etc.)
I remember there was a post about this a long time ago.
I don't think there's a generic way to preview the rendered output without generating the file itself. You can write your own LaTeX engine which caches the output every few seconds and displays that but regardless of the storage, you have to output it somewhere physically and then render the output separately using any of the steps mentioned above.
Another approach is to convert the div output to an svg image file and render that with SVGGraphics2D. That will produce nice scalable results. Dvi files can be converted to svg on the command line (or in a script) using:
dvisvgm --no-fonts input.dvi -o output.svg
For more conversion options see this thread on how to convert pdf to clean svg.

Custom PDF creation - Large images

Looking for a Java based PDF creation library. We're currently using Apache Velocity with HTML to render PDFs on the fly.
We'd like to be able to find a way to render large images (sometimes as big as 3000 x 1700) in a creative manner within the PDF container. For instance, a scrollable image pane within a PDF. This might not be possible within a PDF, I might be wrong.
Open source would ideal.
For a good PDF library you should take a look at iText: http://itextpdf.com/
I have used images of around 5000x4000 with iText without any problems.
I don't know if it is possible to create a working scrollpane inside a PDF, unless of course you were doing it through a custom PDF creator/viewer.
iText is open source but make sure to check out the AGPL license before you use it commecrially: http://itextpdf.com/terms-of-use/agpl.php
For just creating PDF files from images iText is a little overdimensioned. Give xsPDF a chance, it has no limits for images sizes and seems to be appropriate for your problem.
Just a FYI for anyone that may run into this in the future:
I used a library called PDFBox (http://pdfbox.apache.org/) to open a pre-existing PDF and modify the PDF with a custom sized PDFRectangle with the dimensions of the image. Then inserted the image and rectangle into that new page and got the desired results.
I didn't realize you could have multiple page sizes in a single PDF.

How to generate a PDF with auto flow

I wrote a web app for generating PDF by filling data into a pre-saved PDF template, template edited by acrobat, with some text-fields. But the context of those text-fields seems in a different layer and cannot affect other existing words in template.
... But I want it affect the existing words and make them flow base on how many data fill into the text-fields.
The solution maybe use program to generate a whole PDF instead of using template. But the template changes really often in my case, I don't want waste a lot of time to adjust the position and format by coding...
Do anyone know how to use text-field with auto flow in a PDF template? just like a Word document.
PDF doesn't work like that. You need to generate the whole PDF.
Ah... but from what?
There are quite a few HTML->PDF converters out there. You could fill in your template HTML, and convert it that way.
You could develop your own input format (for your template), and write an app that reads it and builds a PDF.
The later is similar enough to HTML->PDF, that unless you can't find a converter that handles some PDF feature or other you need, I'd just go that route. There are LOTS of html->pdf apps out there. You can search SO, google, whatever. Lots.

Good format for vector graphics for generation in java and use/open/print in windows?

We have a java web application. The application generates a big image with company structure diagramm on it as jpeg. Users download the image and want to print it on DIN A2 (big paper format). The jpeg in this scale has pretty bad quality after printing.
Our idea is to generate an image in vector graphics. What vector graphics format suits the best our needs, considering that we code in java and the users have windows and use MS IE browser?
May be there is a better way archieve this? Like let the browser to display the image and let users to print the page?
PDF has been invented exactly for the same purpose. iText library enables you to draw lines, boxes, circles and insert whatever text you like. If you want to automate it a bit using templates there's JasperReports which utilizes iText.
A well known vector graphics format is Scalable Vector Graphics. It's basically an XML file (starting with <svg> tag. It can be viewed in browsers like Firefox without additional plugins.
For java, you can use the Java 2D API to create SVG format. A tutorial can be found here:
If you need the vector graphic for IE you need VML, similar to SVG but can be rendered by Internet Explorer.
RaphaelJS can be used for vector graphics independent of browser model but it's client side and you may not want to do client side processing on every page load.

Categories

Resources