Warm tip: This article is reproduced from stackoverflow.com, please click
c# itext7

Superimposing a landscape page with 90 degree rotation onto a landscape page with 0 rotation

发布于 2020-05-04 03:08:36

I have used the superimpose example to superimpose one pdf onto another. I am running into some trouble when trying to superimpose a page from a source file with a rotation of 90 degrees onto the destination page which has 0 rotation but is landscape (it's wider than it is tall).

When i first tried to stamp the content as in the example, it got the content rotated -90 as in the left example in the image below. When I tried to set the rotation of the destination page, the source content was placed in the right orientation but the destination page had turn (right example in the image below).

var sourcePageRotation = sourcePage.GetRotation();
destinationPage.SetRotation(sourcePageRotation);

How can I manipulate either the source or destination page to change the rotation value without actually changing the way it displays?

enter image description here

Questioner
Alexander Don'valderath
Viewed
23
mkl 2020-02-18 20:21

(As you referenced a Java example, I'll also refer to iText for Java.)

In the SuperImpose example the pages to superimpose are added using

canvas.addXObject(page, 0, 0);

but there are other PdfCanvas.addXObject overloads, too, in particular

public PdfCanvas addXObject(PdfXObject xObject, float a, float b, float c, float d, float e, float f)

The 6 floats represent an affine transformation applied to the XObject, e.g. a rotation. Thus, you can rotate the imported page XObject from the source document to match the orientation of the destination document page, e.g. replace

canvas.addXObject(page, 0, 0);

by

canvas.addXObject(page, 0, 1, -1, 0, page.getHeight(), 0);

or, if that makes the page contents stand upside down, by

canvas.addXObject(page, 0, -1, 1, 0, 0, page.getWidth());

Just like the example you referenced I here assume the lower left corner of the pages involved are the respective coordinate system origin. If that assumption is wrong, one needs to adapt the last two float parameters respectively in the replacements above.