I would like to convert a string of text into an image. The issue is, I want the text to wrap if it is wider than the length of the image, and the height of the image to be dynamically sized to perfectly fit the text, so that I know how much space the text takes up.
I'm working in Java and there are several things I have tried:
Rendering HTML in a JPanel and saving as a BufferedImage. The problem here was that most of the css I used was ignored by the JPanel and the image was unusable.
Using ImageMagick and img4Java. The two big failures with this solution was that I needed the command-line tool installed, which I can't do on our server. The second was that I couldn't easily convert the image to buffered image for use in the rest of the app.
Does anyone know a way to do this in Java?
Thanks!
In this example, an arbitrary panel is rendered into a BufferedImage and displayed in an adjacent panel at half-scale. The example uses a grid of labels, but you can use the wrap feature of JTextArea or the geometry supplied by TextLayout, examined here.
You might use a label containing HTML for the line-wrap, as shown here.
To get an image of that, see LabelRenderTest.
Related
The first page at this PDF displays the following white decorated text on top of an image.
When using the PDFBox utility PrintImageLocations, this graphics is not extracted as an image, only the background image is extracted, without the white decorated text. When converting to Word doc, the decorated text is extracted as a shape with properties which can be modified, such as fill color, border color, and much more.
Is it possible to extract that shape from the PDF, using PDFBox? How?
The simplest way to extract such graphics is to reverse engineer those that can be into ScaledVectorGraphics as here I had to change colour from white to magenta otherwise it would look like a snowscape.
I dont use PDFbox so cant say how easy that may be possible .I simply exported page 1 as SVG using
MuPDF\mutool.exe convert -o page1.svg -O no-reuse-images Xcel_Energy-AR2018.pdf 1
However you will get all SVG output such as the lower text and note the extra header text in the top left corner and lower left corner page number that were not visible behind the pixel grapics.
Note: that everything (thus any conventional text and image pixels are converted to SVG objects) there is no easier way to extract all the PostScript Printer style moves and lineto's. So yes it is overkill as it needs parsing to get just the object of interest (more easily done in a GUI such as inkscape or InDesign where it was constructed). It is not a good methodology for shape recognition since the y x values are described as rectangles, and will have positions and scalars that most likely vary from page to page, thus there are no constants other than filled appearance. The filled object would best be "seen" by regeneration as pixels for visual symbol recognition (much like OCR).
I'm trying to create a PDF using iText library. The PDF has to embed an animation, which is a set of bitmaps. And there are buttons to control the animation. To understand it, see the following file:
http://www.texample.net/media/tikz/examples/PDF/wankel-motor.pdf
How can I create such a file from iText?
I know that I can embed a video file with this code:
http://itextpdf.com/examples/iia.php?id=188
But here I don't want to embed a video file, but a set of bitmaps and to add the control buttons.
Thanks in advance,
V. Henley
The document doesn't have any images. Instead it has a large series of Widget annotations inside a rectangle of which the lower-left and upper-right corners have the following coordinates:
llx = 4.981
lly = 287.238
urx = 322.053
ury = 542.754
These widget annotations are buttons that overlap each other and of which the appearance is defined using PDF syntax (paths consisting of straight lines and Bézier curves).
There are also a number of smaller buttons with arrows and other symbols. When clicking them, some JavaScript is executed:
The animations is actually JavaScript alternating the visibility of the different buttons. If you want to know more about the JavaScript that is used, you have to dig into the document using iText RUPS (which is what I did to create the above screen shot).
The principle that is used to create this PDF is identical to the principle that was used in the Calculator example, which is an example that was distributed with the same book you refer to.
Finally, I've been able to create such a PDF using latex code:
This is the latex code:
\documentclass[10pt]{article}
\usepackage{animate}
\usepackage{graphicx}
\usepackage{media9}
\pagestyle{empty}
\begin{document}
\AtEndDocument{%
\label{lastpage}%
}
\animategraphics[width=9.54cm,height=6.36cm,controls]{2}{C:/Users/casa/Documents/programacion/tex/construc_}{0}{3}
\end{document}
So after compiling such code with a latex compiler, it produces a pdf similar to the one I posted.
Kind Regards
Coming from AWT/Swing, I've started experimenting a bit with JavaFX the last few days. I realized that what I used to do in thousands lines of codes can now be done in a few hundred.
One problem I came across is, however, the following: I'm trying to develop a small painting app where the user can choose brush size and color for its strokes. For all the strokes the user makes, I use the JavaFX class Path and add these paths to a Group (which is added to a Pane) where they are - automagically - painted. Now I want to store the resulting image as a jpg and try to raster all the paths in a BufferedImage. However, I found no functions in the API that help me do that.
I tried to use Canvas and its GraphicsContext, but that did not help. How could I raster all the JavaFX Paths from a list on an image?
Take a snapshot of your Group to get a JavaFX Image.
Use SwingFXUtils to convert your JavaFX image snapshot to a buffered image.
Use ImageIO to convert your buffered image to a jpeg, png, etc
Looking for tips/tools to make this happen (using PDF/Velocity Templates/Java):
I have an image that has large dimension (4000 x 2000) and that image is obviously not viewable in a PDF. I can modify the size of the image to fit into the PDF.
But what I want to do is to view the actual size of the image in the PDF. I want to be able to scroll the image from left to right.
How can I keep the true size of the image, without destroying the set container size of the PDF?
That's not quite a basic PDF feature, but it might be implementable using some embedded scripts inside the PDF, assuming that the user has a PDF viewer capable of running scripts.
Now, a PDF is not supposed to be used as a dynamic document where you can scroll. How would that work on paper after printing? If you want scrollable documents, then you should stick to HTML. I strongly advise you not to try to do this.
If, however, you still want to go down this route, then here are a couple of suggestions:
As I said, you could do it with embedded JavaScript, but then it depends on how you're generating the PDF: FOP, iText, another PDF writer library, or just plain println concatenation, since few libraries support embedding JavaScript.
You could just make the image fit into the page, but keep its original resolution, so that you end up with a high resolution page that the user can zoom in and scroll. This way you leave the scrolling to the PDF viewer itself, you'll just have to let the user know that the image has a higher resolution and should be magnified. And frankly 4000px isn't that large, it's not even enough to keep up with today's standard printer DPIs.
I want to create thousands of dynamic images with Java. I've created a JFrame and a LayeredPane and put a JLabel on this. Then I am writing image contents in this JLabel. Then I create this image of this LayeredPane like this
BufferedImage image = (BufferedImage)comp.createImage(width, hight);
It creates the image perfectly but its size is 20KB (more or less). Can I reduce the image size without losing its quality. I have just text and colour in JLabel.
OR
Is there another way to create images based on Text?
Thanks in Advance
You can reduce its size on disk by saving as JPEG and reducing the Q. This has least impact on quality. Resampling has major impact.
For Text-only images containing only a little number of colors, storing them as GIF files (with a palette of only 2 or 3 colors) could be a lot smaller that JPEG, which is optimized for storing full-color pictures. I would start fiddling around with Paint.NET or some other tool to find out what's possible and which format is the best, before programming the Image-to-file-encoding.