JPanel to be set to A4 size - java

I'm creating a text editor in Java that will have output to PDF. In order to sync data that appears in my program and the output PDF as close as I can, I'd have to have a JPanel which has the same size as an A4 paper (or at least to be in scale). I have tried converting its mm dimension (297x210) to pixels, but opening a regular size A4 document in PDF results in a bigger page than my JPanel.
Would certain size in pixels match the size of every document (.doc,.pdf,etc) created in A4 size (displaying it at 100%), or are there variations from program to program?
I'm trying to make sense of the whole conversion deal... Do I have to visually match the size in the Adobe reader, or is there some kind of factor that you multply with page size in inches or milimeters? Is there anyone who knows how does the whole page size format deal works?

In my experience, there is no such thing as a printer standard. The behavior of printers or software like Adobe Acrobat Reader vary dramatically. Some might automatically shrink your A4-sized panel to fit in a single page with margins while others might print off 4 pages with a single pixel column on page two and a single pixel row on page 3.
Unfortunately you must go about it by trial and error. Make it work for Acrobat Reader and then try printing it out and seeing if it comes out the same. It helps to provide any and all hints as to how to format the page. Lacking these hints, much of this software tends to guess about what your intentions are.

have look at 2D Graphics tutorial an to take Graphics to the BufferedImage, becasue direct output from Swing GUI to creates 1pixes == 1 DPI
there are
Working with Images
Printing
a few examples

From my experience all printable sizes use 72 pixels=1 inch based measurements. There could be difference because default win DPI=96.

Related

Calculate a fitting text size

I want to calculate the size my Font needs, so my text will be displayed fully in one line without clipping.
Example
|-100px---HERE'S MY TEXT---100px-|
I have the DPI and all there stuff. Testing isn't a way, I am using Libgdx and the text ist display with an Button (Scene2D ui).
Call BitmapFont.getBounds(). The TextBounds it returns will tell you what you need. You can try a medium font, the go up or down based on how big or small the bounds are. I use this method to help scale UI sizes from old Droid 1s up to the new HD displays.
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/BitmapFont.html

Scrollable Image in PDF

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.

How to scale down the size and quality of an BufferedImage?

I'm working on a project, a client-server application named 'remote desktop control'. What I need to do is take a screen capture of the client computer and send this screen capture to the server computer. I would probably need to send 3 to 5 images per second. But considering that sending BufferedImage directly will be too costly for the process, I need to reduce the size of the images. The image quality need not to be loss less.
How can I reduce the byte size of the image? Any suggestions?
You can compress it with ZIP very easily by using GZIPInputStream and its output counterpart on the other end of the socket.
Edit:
Also note that you can create delta images for transmission, you can use a "transpartent color" for example (magic pink #FF00FF) to indicate that no change was made on that part of the screen. On the other side you can draw the new image over the last one ignoring these magic pixels.
Note that if the picture already contains this color you can change the real pink pixels to #FF00FE for example. This is unnoticable.
An other option is to transmit a 1-bit mask with every image (after painting the no-change pixels to an arbitrary color. For this you can change the color which is mostly used in the picture to result in the best compression ratio (optimal huffman-coding).
Vbence's solution of using a GZIPInputStream is a good suggestion. The way this is done in most commercial software - Windows Remote Desktop, VNC, etc. is that only changes to the screen-buffer are sent. So you keep a copy on the server of what the client 'sees', and with each consecutive capture you calculate what is different in terms of screen areas. Then you only send these screen areas to the client along with their top-left coords, width, height. And update the server copy of the client 'view' with just these new areas.
That will MASSIVELY reduce the amount of network data you use, while I have been typing this answer, only 400 or so pixels (20x20) are changing with each keystroke. This on a 1920x1080 screen is just 1/10,000th of the screen, so clearly worth thinking about.
The only expensive part is how you calculate the 'difference' between one frame and the next. There are plenty of libraries out there to do that cheaply, most of them very mathematical (discrete cosine transform type stuff, way over my head), but it can be done relatively cheaply.
See this thread for how to encode to JPG with controllable compression/quality. The slider on the left is used to control the level.
Ultimately it would be better to encode the images directly to a video codec that can be streamed, but I am a little hazy on the details.
One way would be to use ImageIO API
ImageIO.write(buffimg, "jpg", new File("buffimg.jpg"));
As for the quality and other parameters- I'm not sure, but it should be possible, just dig deeper.

Java-generated PDF renders fine on screen, but does not print correctly

I'm generating a complex PDF from a swing application by printing my JComponent.
This PDF is created by getting a proxy Graphics2D object from an iText PdfTemplate object.
The PDF is viewable on-screen, but when printed in banner mode on a Lexmark 4650 from windows printing cuts off after the fourth page, with most items not being printed.
Is there a good way to look at the contents of the PDF to see if something is out of line? The PDF seems to be larger than it needs to be, given the information it contains.
Or, a way to get a useful error from the Lexmark printer?
Download the PDF File
EDIT 2011-10-18 13:45:00 PST: replaced PDF with a smaller version with less PDF shape data. Still not printing correctly.
Here is the output from the printer. You can see that printout cuts off shortly after the 410 depth.
We have seen printing fail when there is not enough memory - printing needs a much bigger raster than screen. Does increasing memory help?
There was a rendering issue where a line was being drawn to a coordinate of Integer.MIN_VALUE, this made the printer very unhappy.

Automatically scaling font to fit panel

I have an Swing app whose main panel is divided into a 3x2 grid of charts, and the app can be resized, with the charts (JFreeChart) auto scaling. One of these panels I would like to display the Apdex rating in, which is just text (e.g. '0.89 [0.5]*'). We use the application to display on a monitor visible to everyone, and scale multiple instances of the app that monitor different data centers. Scaling the Apdex font size to fit available panel space is what I'm after.
Any good ideas?
After re-reading and rethinking the question I would suggest for you to try calculating it yourself by use of FontMatrics stringWidth with the string and iteratively increasing the font size until you can, i.e. the size evaluated by you versus the available space.
A ready algorithm would be nice but I didn't hear of any.
Good luck, Boro.
I'd render it off-screen at some suitably large point size, as shown here, and then down-sample it using AffineTransformOp, as shown here.

Categories