Quantcast

itext - PdfCopy/SmartCopy vs PdfWriter

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

itext - PdfCopy/SmartCopy vs PdfWriter

cmo
Hi there,

I have a hopefully simple question.

Why is it, that a PdfCopy/SmartCopy doe's not worry about paper sizes and rotations when adding
an imported page via copy.addPage(page) and a writer does worry via writer.addtemplate(page).

Why im asking this?

My problem is, that i want to merge pages but also scale them down and have them exceute a custom
PdfPageEventHelper so that I get the same page, a little smaller with header and footer.

It seems Copy/Smart does not trigger the OnEndPageEvent() and can't scale the imported page.
So I have to use the Writer and addtemplate(page, matrix) to get the desired result.

There lies my problem -  now it seems I have to worry about paper sizes and rotation.
Rotations/scaling I can handle with a matrix, but the resulting pages does not have the same size as the imported page.

How do I set them to match the source page size. When using
  document.setPageSize(importedPage.getSize());
  document.newPage();

I get wired results - even if I use
 importedPage.getSizeWithRotation(); (on rotated pages)

the result size is diffent from the source size.
Is there a way to get/set the size the way Copy/SmartCopy does it?

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

Re: itext - PdfCopy/SmartCopy vs PdfWriter

iText Software
On 17/08/2012 12:19, cmo wrote:
> Why is it, that a PdfCopy/SmartCopy doe's not worry about paper sizes and
> rotations when adding
> an imported page via copy.addPage(page) and

You mean that PdfCopy/SmartCopy preserve the page boundaries and
rotation (so they do worry about paper size and rotation).

> a writer does worry via
> writer.addtemplate(page).

PdfWriter doesn't worry about the rotation (it doesn't look at the
rotation key in the MediaBox). As for the page size, it only looks at
the MediaBox and not at the other page boundaries (in your case: it
doesn't worry about the CropBox).

> Why im asking this?

Good question. We want to avoid that people use PdfWriter /
PdfImportedPage for the purpose of concatenating PDFs, and that's why
PdfWriter worries less about page boundaries and rotation than
PdfStamper, PdfCopy and PdfSmartCopy.

> My problem is, that i want to merge pages

Merge: PdfCopy / PdfSmartCopy

> but also scale them down

Why? PDF files don't have a resolution. In Adobe Reader, you can zoom
in, zoom out, and you have different print options. I've never
understood the arguments people forward when they say "we want to scale
down a PDF." Maybe you'll be the first one providing me a good argument,
so I'm curious to read on.

> and have
> them exceute a custom
> PdfPageEventHelper so that I get the same page, a little smaller with header
> and footer.

Aha, what you actually want to do, is to change the page boundaries so
that you can add extra content. Are you sure that implies scaling down
the document? Because it won't be sufficient to scale down the content,
you'll also have to scale down all the interactive features (otherwise
all the links and annotations will end up on the wrong place; actually
when using PdfWriter, all interactivity will get lost completely).

> It seems Copy/Smart does not trigger the OnEndPageEvent()

please start by downloading chapter 6 of the book "iText in Action -
Second Edition": http://www.manning.com/lowagie2/samplechapter6.pdf

Now read p188 (the 36th page in the extract of the book):

ADDING CONTENT WITH PDFCOPY
In previous sections, I explained that PdfImportedPage is a read-only
subclass of PdfTemplate. You can’t add any content to an imported page.
This wasn’t a big deal when using imported pages with PdfWriter and
PdfStamper because we could easily add content over or under the
imported page. When using PdfCopy, it would be interesting if we could
somehow add extra content too.

What this paragraph says is basically that PdfCopy and its subclass
PdfSmartCopy are for read-only use only, which explains why the
following line has no effect:
copy.setPageEvent(new WatermarkCreator("DRAFT"));
I'll make sure an Exception is thrown when using this method in the next
release of iText.

> and can't scale
> the imported page.

But you don't want to scale down the imported page: you just want to
create some extra space for a header and a footer! Typically, you'll do
that by changing the rectangles defining the page boundaries at the
level of PdfReader. This is an example that does the opposite:
http://1t3xt.be/?104
It 'crops' pages (makes them smaller). You need to update the example to
make pages bigger.

> So I have to use the Writer and addtemplate(page, matrix) to get the desired
> result.

Ouch, yes, that's to be used only in the case the 'good solution'
doesn't meet your requirements. For instance: because you discover that
your document had invisible content that was cropped and that is visible
after removing the cropping.

> There lies my problem -  now it seems I have to worry about paper sizes and
> rotation.

Yes, you do. See page 167 of the chapter you've just downloaded (the
last code snippet of section 6.2.1).
You have to ask the page for its rotation, and in your case, you need
this to define the rotation of the new page you're creating.

> Rotations/scaling I can handle with a matrix, but the resulting pages does
> not have the same size as the imported page.

The MediaBox is the same, but did you check the other page boundaries?
Especially the CropBox deserves your attention.

> How do I set them to match the source page size. When using
>    document.setPageSize(importedPage.getSize());
>    document.newPage();

That's not sufficient is it? You're copying the MediaBox (getSize() /
setSize()), but not the other page boundaries.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
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
cmo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: itext - PdfCopy/SmartCopy vs PdfWriter

cmo
> You mean that PdfCopy/SmartCopy preserve the page boundaries and
> rotation (so they do worry about paper size and rotation).

Yes, excatly.

> PdfWriter doesn't worry about the rotation (it doesn't look at the
> rotation key in the MediaBox). As for the page size, it only looks at
> the MediaBox and not at the other page boundaries (in your case: it
> doesn't worry about the CropBox).

I'm using itext(sharp) for like 2 days now - approx. the same day I first glanced at a pdf (internally).
I believe some Pdf basics 101 is in order.

> Aha, what you actually want to do, is to change the page boundaries so
> that you can add extra content. Are you sure that implies scaling down
> the document? Because it won't be sufficient to scale down the content,
> you'll also have to scale down all the interactive features (otherwise
> all the links and annotations will end up on the wrong place; actually
> when using PdfWriter, all interactivity will get lost completely).

Didn't know the implications but it starts to sound like a rather stupid idea.
I'm thinking you are right - no need to "scale down" the page if I can just extend the boundaries.

> please start by downloading chapter 6 of the book "iText in Action -
> Second Edition": http://www.manning.com/lowagie2/samplechapter6.pdf

Done.

> But you don't want to scale down the imported page: you just want to
> create some extra space for a header and a footer! Typically, you'll do
> that by changing the rectangles defining the page boundaries at the
> level of PdfReader. This is an example that does the opposite:
> http://1t3xt.be/?104
> It 'crops' pages (makes them smaller). You need to update the example to
> make pages bigger.

Ok, here you lost me - if I do this, doesn't it mean the resulting page will be bigger too?
Say 40x40 -> 2* (5x40) for header/footer = 50x40
I would like to have excatly the same (physical?!) size in the resulting page (A4 for instance).
Never the less.  I think I need some reading.
Seems I'm missing quite a bit of boxes and boundaries

> The MediaBox is the same, but did you check the other page boundaries?
> Especially the CropBox deserves your attention.

Ok. I will have a look at it.
Loading...