Quantcast

Copy and stamp the first page of a pdf

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Copy and stamp the first page of a pdf

Bugzilla from vito.detullio@gmail.com
Hi all.

I'm a new iText user... what I need to do, given a pdf is:
copy the first page (so, starting from a 5-pages pdf I need to end
with a 6-pages pdf) and stamp some text on it.

Toying around the apis, all I made is a method like this:

(original is an InputStream with the original pdf)
...
PdfReader reader = new PdfReader(original);
int n = reader.getNumberOfPages();
if (n == 0)
    return original; // no pages

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper stamp = new PdfStamper(reader, baos);

// copy the first page
byte[] page1 = reader.getPageContent(1);  // content of the first page
stamp.insertPage(1, reader.getPageSize(1)); // insert a very first blank page
reader.setPageContent(1, page1); // copy the content of the "old" first page

// "stamp" the "new" first page
PdfContentByte layer_over = stamp.getOverContent(1);
layer_over.beginText();
layer_over.setFontAndSize(
    BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
BaseFont.EMBEDDED),
    this.text_size);
layer_over.moveText(this.left_spacing, this.bottom_spacing);
layer_over.setLeading(this.text_size);
layer_over.setTextRenderingMode(
    PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE_CLIP);
for (final String text: texts)
    layer_over.newlineShowText(text);
layer_over.endText();

stamp.close();
return new ByteArrayInputStream(baos.toByteArray());

but acrobat reader say it cannot find the right font for the generated
pdf (?) and show me the copy of the first page with some strange
characters...

--
Vito De Tullio - [hidden email]

------------------------------------------------------------------------------
_______________________________________________
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
|  
Report Content as Inappropriate
star

Re: Copy and stamp the first page of a pdf

iText Software
Vito De Tullio wrote:
> Hi all.
>
> I'm a new iText user... what I need to do, given a pdf is:
> copy the first page (so, starting from a 5-pages pdf I need to end
> with a 6-pages pdf) and stamp some text on it.

OK.

> Toying around the apis, all I made is a method like this:

This is absolutely forbidden:
> byte[] page1 = reader.getPageContent(1);  // content of the first page
> stamp.insertPage(1, reader.getPageSize(1)); // insert a very first blank page
> reader.setPageContent(1, page1); // copy the content of the "old" first page

Using setPageContent() copies the content stream of a page
WITHOUT THE RESOURCES!!!

> but acrobat reader say it cannot find the right font for the generated
> pdf (?) and show me the copy of the first page with some strange
> characters...

Adobe Reader tells you that you're using a font that can't
be found in the resources.

You should use PdfImportedPage if you want to copy the content
AND the necessary resources.

This is documented in the book.
--
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info

------------------------------------------------------------------------------
_______________________________________________
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
|  
Report Content as Inappropriate
star

Re: Copy and stamp the first page of a pdf

pankaj.pandey
In reply to this post by Bugzilla from vito.detullio@gmail.com
Hi,

I have a similar requirement. Did you found a way to copy the pages from source PDF and then add a page to it?

It would be really helpful if you can share how you achieved this.

Thanks in advance.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Copy and stamp the first page of a pdf

iText Software
On 18/04/2012 13:37, pankaj.pandey wrote:
> Hi,
>
> I have a similar requirement. Did you found a way to copy the pages from
> source PDF and then add a page to it?
>
> It would be really helpful if you can share how you achieved this.

It would be really helpful if you read the documentation:
http://www.manning.com/lowagie2/samplechapter6.pdf

------------------------------------------------------------------------------
Better than sec? Nothing is better than sec when it comes to
monitoring Big Data applications. Try Boundary one-second
resolution app monitoring today. Free.
http://p.sf.net/sfu/Boundary-dev2dev
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Loading...