Unable to get text box using PDF Writer !!

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to get text box using PDF Writer !!

jerrin.cvarghese
 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
                                image.setAbsolutePosition(0, 0);
                                pdfDirectContent.addImage(image);TextBox.pdffinal.pdf
Reply | Threaded
Open this post in threaded view
|

Re: Unable to get text box using PDF Writer !!

iText mailing list
On 14/09/2010 12:29, jerrin.cvarghese wrote:
>   I am using the Itext java lib for scaling the pdf.

Let me specify: you are using iText to scale pages
in a PDF document, more specifically by taking the
content stream of each page and applying a transformation.

> But this final Pdf do not contain the text
> box.

The text box to which you are referring is an annotation.
Annotations are NOT part of the content stream.
You are not copying or scaling that text box.

> If i use pdfCopy , then i will not be able to scale the pdf.

That's documented in chapter 6: PdfCopy preserves
the interactive content, PdfWriter doesn't.

> Is my understanding correct ?

I think that you assumed that annotations are part
of the content stream of a page. If that's so,
your understanding was wrong.

> Is there any way i can achieve scaling and adding of form
> fields.

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.

> Similarly , some hidden fields are getting visible.

I'm guessing that you aren't talking about fields
(as fields aren't copied), but I assume that you
are talking about OCGs?

In any case: you are doing something complex, using
code that assumes you're facing a simple task.
--
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/
Reply | Threaded
Open this post in threaded view
|

Re: Unable to get text box using PDF Writer !!

jerrin.cvarghese
>> 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
FileOutputStream("C:/final.pdf"));
pdfDocument.open();

//Lets consider pageN  = 1
pageDic = pdfReader.getPageN(pageN);
pdfWriter.addToBody(pdfReader.getPdfObject(pageDic.get(PdfName.ANNOTS)) ); => Throws NullPointerException
 
page = pdfWriter.getImportedPage(pdfReader, pageN); image = Image.getInstance(page); image.scalePercent(50); image.setAbsolutePosition(0, 0); pdfDirectContent.addImage(image);

Exception Traces:
-----------------
java.lang.NullPointerException
        at com.itextpdf.text.pdf.PdfWriter.getNewObjectNumber(PdfWriter.java:2285)
        at
com.itextpdf.text.pdf.PRIndirectReference.toPdf(PRIndirectReference.java:84)
        at com.itextpdf.text.pdf.PdfArray.toPdf(PdfArray.java:170)
        at
com.itextpdf.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:157)
        at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:396)
        at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:376)
        at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:329)
        at com.itextpdf.text.pdf.PdfWriter.addToBody(PdfWriter.java:780)
        at com.hp.example.junit.Test1.setPageAttributes(Test1.java:120)
        at com.hp.example.junit.Test1.main(Test1.java:177)
Exception in thread "main" ExceptionConverter: java.io.IOException: The document has no pages.
        at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
        at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1171)
        at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:780)
        at com.itextpdf.text.Document.close(Document.java:409)
        at com.hp.example.junit.Test1.close(Test1.java:165)
        at com.hp.example.junit.Test1.main(Test1.java:179)


  Please help me to define the code properly for adding these annotatoins to the pdf.
Reply | Threaded
Open this post in threaded view
|

Re: Unable to get text box using PDF Writer !!

iText mailing list
On 16/09/2010 14:44, jerrin.cvarghese wrote:

>
>>> 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
> FileOutputStream("C:/final.pdf"));
> pdfDocument.open();
>
> //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".

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/
Reply | Threaded
Open this post in threaded view
|

Re: Unable to get text box using PDF Writer !!

jerrin.cvarghese
 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 ?
Reply | Threaded
Open this post in threaded view
|

Re: Unable to get text box using PDF Writer !!

iText mailing list
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 ?

Yes,
you should use the e-mail address that was provided with the license.

Summarized:
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".

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/