The Usecase is to sign a given pdf document. I have already done the signing part, which places a visible image somewhere inside the pdf.
Now i have to let the user decide where to place the visible part of the signature.
I want to present the pdf document to the user and let him place a (draggable) rectangle somewhere inside the document.
In the end i want to get out the coordinates and the page of this rectangle.
I do not want to manipulate the pdf, just get out the coordinates and page.
I can also guarantee that adobe pdf reader is installed on the machine (windows) of the user.
Do i have to use a pdf renderer library (which?) or could i use the existing adobe pdf reader?
I decided to use jPedal and draw a rectangle onto it by extending the PdfDecoder and overwriting the paintComponent method.
Related
I print documents (pdf, doc, docx...) via javax.print and it works fine, but I need to add a custom overlay text on any place at document; the text is necessary to insert a printed reference from my code to the document.
How do I do this?
I do not believe adding text (watermarking) is an aspect of javax.print.
You can pre-process each page of each document via Graphics2d and add in the text/graphic individually based on that document's type, then send that document to the printer.
There are several threads here and elsewhere on "watermarking", which is essentially what you're looking to do. The process is different based on the type of document you're working with. Single-page images, multi-page images, PDFs, DOCs, and DOCXs all require different ways to be processed.
Here are some threads to get you started:
GIF/JPEG/TIFF: How can I watermark an image in Java?
GIF/JPEG/TIFF: Adding a watermark over an image programmatically using Java
DOCX: Creating watermark programmatically
PDF: Watermarking PDFs via iText
I was going through the itext api docs & I was able create a pdf with a watermark image or text but did not find a method to get/extract watermark content from pdf.
So I have a pdf document containing watermarked text/image & I want to extract that text or img and validate which I am not able to do.
How to extract watermark content using iText apis? Or is there any other way to validate watermark content?
By validate I mean if I have an existing pdf/image with some watermarked text [as done in 2nd link in above ref], I want to check whether it has expected text/image.
References:
http://itextpdf.com/themes/keyword.php?id=226
http://www.java-connect.com/itext/add-watermark-in-PDF-document-using-java-iText-library.html
How to extract watermark content using iText apis? Or is there any other way to validate watermark content?
Extracting watermark content?
There is nothing special about watermarks in PDFs in contrast to regular page content. They merely
appear pretty early in the content stream and other content later in the stream, therefore, is drawn above it; or they
appear pretty late in the content stream but have some kind of transparency applied.
Actually there is another type of watermarks which is special, the so-called Watermark Annotations. As these annotation can easily be lost when documents are merged or otherwise manipulated, though, they hardly ever are used.
Furthermore different PDF generating software suites offering a way to add watermarks do so in their respective individual way. Thus, you cannot even recognize watermarks by some special operations done in some specific unique pattern.
Already the iText examples you referred to apply different kinds of watermarks
MovieCountries2 simply draws some gray large Text using an angled base line.
StampStationery copies a complete page from some PDF (which itself may visually have foreground and background material) into a separate object inside the target PDF and adds a reference to this object at the beginning of every page of the target.
InsertPages similarly references a page from some PDF on every newly generated target document page.
Thus, blind watermark extraction is virtually impossible.
Validating watermark content!
You might try some validation, though, if you know what you are searching for. You simply do not merely search some (in PDF not existing) fixed watermark stream but instead the whole page content.
iText offers the classes of the parser package which allow extraction of text and/or bitmap images from content streams. Look at the samples referenced from the keywords PARSING PDF > EXTRACTING IMAGES and PARSING PDF > EXTRACTING TEXT.
You merely have to check whether the image or text which you expect can be found by these classes positioned and styled as you expect.
I have a library which generates pdf document with images.
I want to be able to add text after each image. What is the syntax for that? How to insert text into pdf documents?
I have to use the library I have, not another one.
First of all, mkl is correct, have a look at the specification for all of the details. PDF is an exact language, if you make mistakes they will routinely be punished severely once you open the PDF in viewers.
Secondly, when you think about putting text on the page, don't forget that besides the text operators to draw the text on the page, you'll also have to specify the font to use to draw this text. Which will include making sure there is a font resource included in the PDF file if your library doesn't automatically handle all of that for you.
If you want to cut corners (I shiver while writing this) and perhaps don't read the specification as thoroughly, try this.
1) Create a PDF file that looks more or less like what you want.
2) Use a tool such as pdfToolbox from callas (http://www.callassoftware.com/callas/doku.php/en:download) or Browser from Enfocus (http://www.enfocus.com/en/products/browser). Both of these tools allow you to investigate the low-level structure of a PDF file, including looking at the actual page description code. This will show you how fonts are embedded (if you have to do it yourself that could be very handy) and how text is rendered on the page (and how you set the font, size, color etc... to use).
Is this possible?
I am getting the directory and filename of a existing PDF document.
C:\temp\FileName.pdf
Use PDF Renderer to either view the document or print the document
I have PDF Renderer included in my Eclipse Project, just not sure how to use it
Then use PrintDialog to print out the PDF Document.
I think that PDF Renderer has to do something to make the PDF document printable
I want have 2 buttons on my form
View PDF -> opens the pdf document in read only
Print PDF -> gets the pdf document ready to print then calls PrintDialog to select a local printer and then prints
I am not having much luck finding any PDFRenderer sample code that shows how to open the PDF document in view mode.
I haven't seen code on how to send the PDF Renderer result to a PrintDialog to print.
You certainly can implement PDF rendering in your application. The question is, if it is really necessary or not. Since you dindn't specify which library you want to include, I can't help you with the PDFRenderer.
However, for purpose of launching an external PDF viewer and how to implement a print button, those API classes are useful:
org.eclipse.swt.program.Program
org.eclipse.swt.printing.PrintDialog
At least it'll help to (partially) solve your problem.
I have a program. It outputs to pdf, but that is close to impossible to read from again. So i need a additional file attached to my PDF in order to be able to make it editable in my program. Attaching a file to PDF is a good idea, but that is visible to the user, which i don't wan't it to be.
An alternative is to hide my readable file format inside an image which would be added to the PDF somewhere to the top of the first page, before everything else... Even to metadata if that's possible...
That way I can extract image from pdf using a PDF library (iText), and read from it.
My question is how to add image to PDF to be as well hidden as it could be (visually and by accesibility). And it has to be in a place which would be same for any created document (somewhere on the top, or on the very bottom of the document, or to the part of the document which isn't displayed at all... I'm really guessing here, I'm not really familliar with PDF file format)...
Any ideas?
P.S. It's not really important which image is it, I could be a e.g. completly transparent image, 1x1 pixels.
I'm not sure what you mean by Image, but you can "extend" the PDF reference.
A PDF consists of objects: PDF numbers, PDF names, PDF strings, PDF arrays, PDF dictionaries, PDF streams. What you probably want, is to add an entry to a dictionary (pick one: the root dictionary, the info dictionary, the root of the page tree,...) that isn't defined in the PDF reference, so that it isn't rendered in a PDF viewer.
The key of such an entry must be a PDF name. To avoid clashes with existing names (names that are part of a current PDF spec, or will be part of a future spec), it is advised to register a four-letter key with ISO. For instance, Adobe registered adbe, iText registered ITXT and use that name with an underscore. For instance, ITXT_OriginalData would be a good name if we needed the functionality you describe.
The value of such an entry will be a PDF stream. In iText, you need the PdfStream class for this.