Save Java frame as a Microsoft Word or PDF document? - java

I am working on a billing program - right now when you click the appropriate button it generates a frame that shows the various charges etc, basically an invoice. Is there a way to give the user an option of saving that frame as a document, either Microsoft Word, Microsoft Works or PDF?

One approach would be to save the frame as an image, you can do that by using the following syntax to convert it to an image.
BufferedImage myImage = new BufferedImage(size.width,size.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
you can then save this image and pass it into a jasper report. From the JasperPrint object you can then save in a few different formats, including pdf. A better but similar approach would be to pass the Graphics context into JasperReports(there is a renderer to do this in jasper, and the quality is much better).

Paint JFrame in a BufferedImage. paint() method of JFrame
Save the image as jpg or png or whatever image format
Take some pdf library and create a blank pdf (e.g. iText)
Insert the image into the PDF document
Save it - done

Instead of generating a word document, I'd rather use a Java library like iText to produce a PDF document (more portable) or, even better, the JasperReport report library that can output reports in a wide range of formats (PDF, XML, HTML, CSV, XLS, RTF, TXT) as suggested by bigbrother82 in a comment. This looks cleaner to me than using an image, especially for printing (not even mentioning that your invoice may be a multi-page document).

I'd likely look at this from a slightly different direction and instead of asking how to splat the GUI form as-is into a PDF or word document I'd ask how to get that content into a Word/PDF document.
The answer to that question is Apache FOP. Generate a XSL-FO file and ask FOP to convert it into a RTF document (with a .DOC extension) or a PDF.
Normally one does this by generating an XML file containing the data you need printed. Then use an XSLT to convert that XML to XSL-FO. I however found it easier to generate a XSL-FO file directly using a templating language (such as Freemarker).

You might want to look at the online demo for Docmosis as an example which gives the user the options for requesting the document up front. That demo does a download, but it could direct the document into a frame instead and leave it to the browser to display. This style of working (as metioned by others) is looking at the problem from a different angle and deciding up front what format, rather than after the fact and then trying to save the frame contents.

Related

Render a table to an image file in Java

I have data in a table of an arbitrary size that I would like to render as an image file. What is the best way to go about this in Java? I can't seem to find any libraries written explicitly for rendering tables.
Example:
I might have three columns ("Col1", "Col2", "Col3") and three rows of data. I would expect output as an image file like this (formatting doesn't need to match exactly):
Possible solutions/workarounds I've considered:
Using some sort of pdf library to assemble a table and then converting the pdf to an image. I tried itext7, but couldn't manage to render a pdf with a table as an image.
Using a Swing JTable instance in an invisible JFrame and creating an image from that. The problem with this solution is that I need this code to work in a headless Ubuntu server environment, and it just throws a HeadlessException.
Rendering a LaTeX table with some library, though a quick Google search came up empty for such libraries that can create tables.
You can technically render this data in table and generate a PDF and later export it as an image(JPEG/PNG/TIFF,BMP) using pdfRender product of iText 7.
Please refer to this page of iText https://itextpdf.com/en/products/itext-7/convert-pdf-to-image-pdfrender

How to detect a large image in PDF and extract it as a JPEG or any format?

I'm creating a project that creates a magazine from PDF file, however each uploaded magazine should have a thump that has a cover photo, and i want to extract this image from the PDF as a JPEG in order to set it as a cover photo.
Is there any way to do it using Ghostscript or any other command line tool ?
Do you mean you want to render the first page of a PDF file to an image format ? If so then yes, Ghostscript can do that (also ImageMagick using Ghostscript, MuPDF and probably many other utilities too).
If you mean the first page contains an image, and you do actually want to extract it, then this is a harder job and you will need a PDF toolkit to do it. Ghostscript can do this,but its probably overkill, again you might find MuPDF more convenient. I have a vague memory that pdftk can extract images, but I may be mistaken. A quick search on Google should probably help, if this is what you want.
Poppler/XPDF comes with pdfimages:
Pdfimages saves images from a Portable Document Format (PDF) file as Portable Pixmap (PPM), Portable Bitmap (PBM), or JPEG files.
Pdfimages reads the PDF file PDF-file, scans one or more pages, and writes one PPM, PBM, or JPEG file for each image, image-root-nnn.xxx, where nnn is the image number and xxx is the image type (.ppm, .pbm, .jpg).
The commandline to extract all images from page 1 of a PDF is this:
pdfimages -j -f 1 -l 1 some.pdf subdir/prefix
The images will be saved to subdir/ named prefix-0000.jpeg, prefix-0001.jpeg. The -j parameter will try to get JPEG images, if possible. Direct JPEG extraction may fail, in which case the images extracted will be saved as PPM or PNM (attention, these are big, since they're uncompressed). These can be converted by ImageMagick's to JPEGs, if needed:
convert subdir/prefix-0022.ppm subdir/prefix-0022.jpeg
ABCpdf will allow you to extract images from a PDF. It's a two stage operation. First you need to identify where images appear in the document. Then you need to export them.
You need something like this...
using (Doc theDoc = new Doc()) {
theDoc.Read(theSrc);
ImageOperation op = new ImageOperation(theDoc);
op.PageContents.AddPages();
ICollection<ImageProperties> images = op.GetImageProperties();
foreach (ImageProperties pl in images) {
foreach (ImageRendition plc in pl.Renditions) {
... if plc is a good match
plc.PixMap.GetBitmap().Save(#"c:\output.jpg");
}
}
}
I work on the ABCpdf .NET software component so my replies may feature concepts based around ABCpdf. It's just what I know. :-)"

How to hide text in an PDF file?

How can I add text to a pdf document, which is not visible?
The document manipulation should be done in java. The usecase is to add further metadata to a document (in a proprietary format, about 40kb), before the document is signed and archived.
I tried:
annotation field with size 0,0
.txt file attachment
but, this annoys readers of the PDF, because they see a difference (comment / attachment bar).
Is there a comment object or a syntax to comment out lines in a PDF document?
EDIT:
I've tried adding text between PDF objects. This works, the problem is: acrobat reader asks to resave the file when closing window.
Adding the text after %EOF is not a solution, because signing is not applied to the metadata, which is a needed feature.
The proper way to add metadata to a PDF would be through XMP. It allows you to add arbitrary metadata and allows defining the metadata types inside of the same PDF file (which you really should do if you're archiving and which is a requirement in archival standards such as PDF/A).
XMP data can be extracted by readers who don't understand the PDF format using a simple text scanning algorithm yet at the same time it will be inside of the document so will be protected by the digital signature you apply.
You can read more about it here: http://www.adobe.com/products/xmp/
I have seen PDF's who had a bunch of metadata in the footer, just in color white while the background was also white, so normally you wouldn't recognize it when you're looking at the PDF. But that's quite nasty..

How to get given paragraph content of pdf file using iText library?

Is there any way to get number of paragraphs or content of given paragraph in pdf file using iText library ?. I saw some classes like Paragraph, Chunk in some code to create new pdf file but I can not find any way to get these classes in reading file. Every idea is appreciated
Is the PDF you're talking about a Tagged PDF? If not, you are making the wrong assumptions about PDF. In a PDF, content is drawn on a page. For instance: an iText PdfPTable is converted into text state operators that draw snippets of text to a canvas, as well as graphics state operators that draw paths and shapes. If the PDF isn't tagged, the lines don't know that they are borders of a table; a word doesn't know to which cell it belongs.
The same goes for paragraphs: a snippet of text doesn't know whether it belongs to a sentence, to a paragraph, to a title line,...
Due to the very nature of PDF, what you're looking for may be impossible (using iText or any other software product), or may require heuristics (artificial intelligence) to examine all text state operators and the semantics of the content to get a result that mimics how humans would interpret text.
It's very easy to achieve if your PDF is tagged correctly. See the ParseTaggedPdf example.

How to Convert HTML pages into Bmp using java

I am having thousands of HTML files, I want to convert then to .bmp (Bitmap images) using Java.
Like I select all the HTML files specify the size and then code will convert all images into bmp.
Please suggest me the simple method, which class shall i use for the same.
Is there any API for Converting Html to bmp.
One simple and easy way to do it is to rely on this 2 tools :
WkHtml2png : it cas convert HTML pages to PNG and it has really great capabilities with advanced CSS and JavaScript. I'm currently using it to convert Html to PDF.
png2bmp : it converts PNG to BMP images.
As these tools are native programs, you will have to Wrap them using basic Java API :
java.lang.ProcessBuilder
java.lang.Process
This discussion might be very useful if using WkHtml2pdf.
If the HTML is very simple, and each file is short, you could render it in a JEditorPane, then use java.awt.Robot to take a screenshot with the .createScreenCapture() method.
See this question for saving it as BMP: BufferedImage to BMP in Java
However, JEditorPane is quite limited in the HTML it accepts.
This won't work if the rendering area is larger than the screen. You might be able to create a larger JEditorPane in a window larger than the screen and capture the Graphics buffer, instead.
Why does this have to be done from Java,?
Now that i understood you right you have another problem.
Although all webbrowser should display HTML equally, they don't do.
You should think about that.
If you only have simple HTML Files like Javadocs or equal, there should be no problem with the proposal of Khaled Labidi, if you have no problem using native Librarys.
Maybe you can have a look at http://lobobrowser.org/cobra.jsp.
Render your HTML and CSS and then try to convert it all to BMP.
I think there is no really easy way to do that.

Categories

Resources