Apache PDFBox merge two pages into one - java

I have two PDF files, one containing text (also available in RTF format) and one containing a background image. Now, I'd like to merge those two pages into one by putting the text pdf above the background pdf.
One solution I came across was to use this approach to convert the pdf files to images and draw them onto a new page. However, I wonder if this is the best solution since the PDF won't contain any text afterwards that you could copy&paste but just images.
Is there a solution to merge two pages without converting them to images first?

Related

Detect if a PDF is black and white or not in Java

I have to print hundreds of single page PDF files and I wonder if I can detect if a PDF is black and white or color, I want to send black and white pdfs to a print queue and color ones to another.
I'm processing these pdfs in Java, can someone suggest me some technique?
I think you could give a try to PDFBox
In this example they extract an .icc (color profile) so may be what you are looking for.
How do you extract color profiles from a PDF file using pdfbox (or other open source Java lib)

IText7: Creating Highlight for an PdfImageXObject

We converted our documents to PDF from tiff. This created PdfImageXObjects within the resulting PDF. The users markup the PDFs with using software provided by our document management system client. The markups are stored outside of the PDF itself so they are really just screen overlays. My problem is that when I attempt to merge the markups with the PDFs the highlights are dull since they must be transparent to show the text underneath. One option is to extract PdfImageXObjects as bitmaps and convert the b/w images to RGB and perform a bitwise operation to replace the whitespace and not the text, a time consuming prospect. Is there a way do this that is less time and resource intensive?

Clip rectangular part from pdf and convert to JPEG image java

I have a requirement where I need to clip some rectangular part of OCRed pdf (Initially PDF was Scanned so we have perform OCR) into image.
I was not able to find any library which can achieve this. So I have splitted into two parts.
1. Clip Rectangular part from PDF using iText. The result will be in PDF.
2. Convert clipped PDF into images using pdfBox.
But in the process of converting clipped PDF into images using pdfBox the result is not as expected. As for eg we are not able to get checkbox in JPEG image if the clipped pdf contain only checkbox.
I have searched in StackOverflow for all the possible solution but with no success.
My code is same as the solution provided by Tilman Hausherr here. Ihave also tried this
Is there any direct way to achivve the above two steps in one or get some better way to convert pdf to image.
Please don't mark it as duplicate as I am not able to get the solution even after too many search.

Text extraction from PDF file, with differents sizes and colors fonts, using PDFBOX - Java

I'm developing a java application that extracts text from a pdf file, for this reason I use PDFBox library. I can extract most of the text from the file, but I can not extract certain words that have a larger size and a different color.
I have tried to use the method setAverageCharTolerance of pdfstripper class, but I have not had any results.
I wonder if anyone knows of any way to get extract all the text from the pdf file, whether this is large or small or different colors?

How to reduce size of RTF with embedded images?

We have some code which produces an RTF document from a RTF template. It is basically doing string search and replaces of special tags within the RTF file. This is accessible via a web page.
Typically, the processing time for this is really quick.
However, we need to embed an image within a template. We've been embedding these as JPEG images using Word's "Insert/Picture/From File..." functionality. But we've found that the resultant RTF file size is massively dependant upon the image.
For example, I've inserted a 20k JPEG logo (which is basically a solid background with some text). The RTF file increased in size from around 390k (without the image) to 510k (with the image).
Then we inserted a JPEG containing a screenshot, i.e. the image contains text, multiple colours, etc. The JPEG is around 150k. Using this image, the RTF file increased in size from 390k to 3.5MB.
So the encoding that Word uses for storing images into an RTF doesn't perform linearly. I'm guessing it is dependant upon what is in the JPEG image.
I need to keep the size of the RTF templates to a minimum to try and keep our file processing times to a minimum.
Does anyone have any ideas on how to minimize the size of the RTF files with embedded images?
Is there any way of controlling the encoding that Word uses? I can't see any options anywhere.
Does anyone know what type of binary encoding Word/RTF uses?
Thanks in advance.
Here is the best solution
http://support.microsoft.com/kb/224663
Excerpt:
SYMPTOMS
When you save a Microsoft Word document that contains an EMF,
PNG, GIF, or JPEG graphic as a different file format (for example,
Word 6.0/95 (.doc) or Rich Text Format (.rtf)), the file size of the
document may dramatically increase.
For example, a Microsoft Word 2000 document that contains a JPEG
graphic that is saved as a Word 2000 document may have a file size of
45,568 bytes (44.5KB). However, when you save this file as Word 6.0/95
(.doc) or as Rich Text Format (.rtf), the file size may grow to
1,289,728 bytes (1.22MB).
CAUSE
This functionality is by design in Microsoft Word. If an
EMF, a PNG, a GIF, or a JPEG graphic is inserted into a Word document,
when the document is saved, two copies of the graphic are saved in the
document. Graphics are saved in the applicable EMF, PNG, GIF, or JPEG
format and are also converted to WMF (Windows Metafile) format.
RESOLUTION
Warning If you use
Registry Editor incorrectly, you may cause serious problems that may
require you to reinstall your operating system. Microsoft cannot
guarantee that you can solve problems that result from using Registry
Editor incorrectly. Use Registry Editor at your own risk.
To prevent Word from saving two copies of the graphic in the document,
and to reduce the file size of the document, add the
ExportPictureWithMetafile=0 string value to the Microsoft Windows
registry.
An image in an RTF file gets stored as a WMF, uncompressed. On mac, it it would be macpict. Your best bet to keep the file size down is to link the image to the document rather than insert a copy in the document. The trade-off is that you have to keep the files together.
EDIT
Is compressing the RTF an option? Using zip/rar, you'll get your file size back, but you'll have to uncompress, first obviously. There are supposed to be tools that will do rtf compression, but I have never used them.
We have done a similar project over at work. Only we're not using that "Insert/Picture/From File..." functionality. Our template has a tag named [photos], as I presume your own does also. When we process the document we replace the tag with the RTF codes needed to display images. We're putting them within a table and we're displaying two images on each row, plus a row on top for the title.
So, you might place a tag [photos] in your template. Then you replace the tag with the RTF Codes. You can find some good references to these codes on the web. For eg. here
.
Now, my code looks something like this:
\par {\rtf1\ansi\deff0{\trowd\cellx8810 {title}\intbl\qc\cell\row}{\trowd\cellx4405\cellx8810{\pict\jpegblip\picwgoal4000\pichgoal3000\piccropl-50\piccropr-50\piccropt-50\piccropb-50\hex
Your image as an array of bytes in hexadecimal }\intbl\cell{\pict\jpegblip\picwgoal4000\pichgoal3000\piccropl-50\piccropr-50\piccropt-50\piccropb-50\hex
Your other image }\intbl\cell\row}
if you get your image into a byte array, you may use BitConverter.ToString(array) to get your hex code. only you'll need to replace dashes "-" by "";
Our files will take up less than 1/10th of the space a "normal" RTF will. If we open the doc's code with an editor such as Notepad++, we can see the RTF codes, but if we open the document and save it as RTF (changing its name), it'll go from 1.5Mb to 50Mb!!
I'm guessing DaveParillo's reply justifies it: I'm only writing each image once.
Hope it helps.
Cheers mate
Initially, keep in mind that each byte is stored using 2 characters (two bytes), this means that the increments at least is the double size of original picture.
Other things that you need is that Word and Word Pad insert different (flavor or format) of the same image plus other fields (that RTF can to be displayed without them).
Here are some scripts used to insert images in RTF (https://joseluisbz.wordpress.com/2011/06/22/script-de-clases-rtf-para-jsp-y-php/), and one example of use (https://joseluisbz.wordpress.com/2011/07/16/subiendo-imagenes-png-y-jpg-y-archivos-a-mysql-con-php-y-jsp-y-mostrarlos-en-rtf-usando-clases/)
Now, maybe you will need replace the original Image with another (http://joseluisbz.wordpress.com/2013/07/26/exploring-a-wmf-file-0x000900/).
The Swartbees answer worked perfectly for me. I first reduced the image quality to "0" using G.I.M.P. Save as jpeg functionality. After following the microsoft solution suggested by Swartbees above I reinserted the picture into the file and the size increase was negligible 229k to 279k (as opposed to 29000kb).
Thanks for your suggestions guys.
Yes, by removing the redundant characters. And to do this you must insert them back into your stream.
For instance if you have over twenty f characters in one line, then you can replace with f[20] in your stream. It is a start.
-Best of luck.

Categories