You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Eli Zupke (Jira)" <ji...@apache.org> on 2021/03/26 20:39:00 UTC

[jira] [Created] (PDFBOX-5142) LayerUtility.importPageAsForm distorts rotated pages

Eli Zupke created PDFBOX-5142:
---------------------------------

             Summary: LayerUtility.importPageAsForm distorts rotated pages
                 Key: PDFBOX-5142
                 URL: https://issues.apache.org/jira/browse/PDFBOX-5142
             Project: PDFBox
          Issue Type: Bug
    Affects Versions: 2.0.23
            Reporter: Eli Zupke
         Attachments: out.pdf

The {{LayerUtility}} function {{importPageAsForm}} does not work correctly when the imported page has a rotation of 90 or 270. The function changes the dimensions of the rotated page such that it keeps the original, unrotated aspect ratio, resulting in the content of the page being stretched out.

The following code demonstrates this problem, and the resulting file is included as an attachment to this issue.
{code:java}
package pdftest;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.multipdf.LayerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;

public class Main {

	public static void main(String[] args) {

		PDDocument d = new PDDocument();

		LayerUtility layerUtility = new LayerUtility(d);

		for (int i = 0; i < 360; i += 90) {
			PDPage page = new PDPage(PDRectangle.LETTER);
			d.addPage(page);

			try (PDPageContentStream contents = new PDPageContentStream(d, page, PDPageContentStream.AppendMode.APPEND,
					true)) {
				
				/* Generate Test Input Document */
				PDDocument source = new PDDocument();
				PDPage sourcePage = new PDPage(new PDRectangle(500, 200));
				PDPageContentStream sourceStream = new PDPageContentStream(source, sourcePage);

				PDRectangle box = sourcePage.getCropBox();
				sourceStream.beginText();
				sourceStream.newLineAtOffset(box.getUpperRightX() / 2, box.getUpperRightY() / 2);
				sourceStream.setFont(PDType1Font.TIMES_ROMAN, 12);
				sourceStream.showText("This Way Up! " + i);
				sourceStream.endText();
				sourceStream.addRect(0, 0, box.getUpperRightX(), box.getUpperRightY());
				sourceStream.addRect(5, 5, box.getUpperRightX() - 10, box.getUpperRightY() - 10);
				sourceStream.setLineWidth(2);
				sourceStream.stroke();
				
				sourceStream.close();
				source.addPage(sourcePage);
				sourcePage.setRotation(i);
				
				/* Place the Test Input Document into the Output Document */
				PDFormXObject form = layerUtility.importPageAsForm(source, 0);
				source.close();
				contents.drawForm(form);

			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		try {
			d.save(new File("out.pdf"));
			d.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Complete.");
	}

}

{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org