I am using the Itext java lib for scaling the pdf. For that i use PdfWriter to get the page which is then treated as image instance and scaled and finally added to PdfContentByte. But this final Pdf do not contain the text box. If i use pdfCopy , then i will not be able to scale the pdf.
Is my understanding correct ? I have attached the original PDF (TextBox.pdf) and the generated pdf(final.pdf) where u can observe that the top-left text box is missing. Is there any way i can achieve scaling and adding of form fields. Similarly , some hidden fields are getting visible . Please find the sample code below:
pdfWriter = PdfWriter.getInstance(pdfDocument,new FileOutputStream("C:/TextBox.pdf"));
pdfDocument.open(); pdfDirectContent = pdfWriter.getDirectContent();
page = pdfWriter.getImportedPage(reader1, pageN);
image = Image.getInstance(page);
image.scalePercent(xx); // xx is % value
>> The annotations are in the /Annots entry of the
page dictionary. You could try to fetch them,
apply a transformation to the rectangle that defines
their position, and copy them into the new document
I am randomly using many pdfs and so will not be sure on the exact position of this annotations. I tried to get these annotations from the page dictionary and write it to the new PDf but it fails .
Exception in thread "main" ExceptionConverter: java.io.IOException: The document has no pages.
Please help me to define the code properly for adding these annotatoins to the pdf.
>>> The annotations are in the /Annots entry of the
> page dictionary. You could try to fetch them,
> apply a transformation to the rectangle that defines
> their position, and copy them into the new document
> I am randomly using many pdfs and so will not be sure on the exact
> position of this annotations. I tried to get these annotations from the page
> dictionary and write it to the new PDf but it fails .
> My Code:
> PdfReader pdfReader= **
> pdfWriter = PdfWriter.getInstance(pdfDocument,new
> //Lets consider pageN = 1
> pageDic = pdfReader.getPageN(pageN);
> pdfWriter.addToBody(pdfReader.getPdfObject(pageDic.get(PdfName.ANNOTS)) );
Wow no, that's a serious shortcut you're trying to take here.
I sincerely hope you didn't expect that to work.
Did you read part 4 of the second edition of iText in Action?
Start there, and come back later once you've mastered chapter 13.
Nothing we can say to help you will be of value to you if you don't
understand part 4 of "iText in Action".
On 16/09/2010 20:23, jerrin.cvarghese wrote:
> Its ok for me to buy and go through the Itext In Action. But before that i
> would like to know if Itext will be able to achive my requirement or not .
> Please note that we have a liecence for Itext. So do i need to come
> through another channel / forum ?
you should use the e-mail address that was provided with the license.
You want to scale an existing PDF.
When you try to do this, you notice that all the content
(whatever is in the content stream of a page) is scaled down.
The annotations however (the interactive features that are
not part of the content) is not scaled down.
It isn't sufficient to import pages (as PdfImportedPage objects),
but you also need to retrieve, adapt and copy every single annotation
that is in the page.
iText offers all the functionality that allows you to do this,
but you'll need custom development and good knowledge of both
the PDF specification and iText to achieve this: this is not
something that comes "out of the box".