0

I am trying to center the content of my page after scaling it by a factor X. I have tried using the Matrix.translate function but I always end up getting the wrong position, except when scaling with a factor of 0.5 (which makes totally sense to me). My current code:

for (int i = 0; i < doc.getNumberOfPages(); i++) {
                pdfBuilder.addPage(doc.getPage(i));
                PDPage p = pdfBuilder.getDocument().getPage(i);
                Matrix matrix = new Matrix();
                float scaleFactor = 0.7f;
                float pageHeight = p.getMediaBox().getHeight();
                float pageWidth = p.getMediaBox().getWidth();
                float translateX = pageWidth *  (1 - scaleFactor);
                float translateY = pageHeight * (1 - scaleFactor);
                
                matrix.scale(scaleFactor, scaleFactor);
                matrix.translate(translateX, translateY);
                PDPageContentStream str = new PDPageContentStream(pdfBuilder.getDocument(), p, AppendMode.PREPEND,
                        false);
                str.beginText();
                str.transform(matrix);
                str.endText();
                str.close();

            }

I have also tried other boxes like the cropBox and bBox but I think I am totally wrong in what I do right now. Please help me! :)

Update I finally found a solution. The new translation values I am using now look like the following.

float translateX = (pageWidth * (1- scaleFactor)) / scaleFactor / 2;
float translateY = (pageHeight * (1- scaleFactor)) / scaleFactor / 2;
SemmelJochen
  • 87
  • 1
  • 8
  • 1
    I can't test it, but I'd rather use something like `pageWidth * scalefactor / 2`. – Tilman Hausherr Sep 15 '22 at 03:12
  • Sadly that doesn't work either. Somehow after the matrix calculation, the content always aligns wrong. E.G. I scale the page by a factor of 0.2 the content centers in the bottom left. – SemmelJochen Sep 15 '22 at 07:14
  • 1
    Ok, after trying different translations for multiple hours I found a solution. I will add it to my question. It's a mix of what @TilmanHausherr and I did. So thank you Tilman :) – SemmelJochen Sep 15 '22 at 07:24
  • Please make an answer of the solution, maybe explain why the "1-scalefactor" – Tilman Hausherr Sep 15 '22 at 07:54
  • 1
    Beware: (1) The crop box may be the box you should use instead of the media box. (2) Your code implicitly assumes that the lower left corner of the (media/crop) box is the origin of the coordinate system. This often is the case but not always. (3) Your code only scales the static content, not annotations. – mkl Sep 15 '22 at 08:01

1 Answers1

0

Update I finally found a solution. The new translation values I am using now look like the following.

float translateX = (pageWidth * (1- scaleFactor)) / scaleFactor / 2;
float translateY = (pageHeight * (1- scaleFactor)) / scaleFactor / 2;

First of all, it is important to note what @mkl said.

  1. The crop box may be the box you should use instead of the media box.
  2. The code implicitly assumes that the lower left corner of the (media/crop) box is the origin of the coordinate system. This often is the case but not always.
  3. The code only scales the static content, not annotations.

Now the explanation of the translation (e.G. translation for the page height). PLEASE NOTE THAT I AM NOT A MATHEMATICIAN AND I JUST TRIED DIFFERENT WAYS AND THIS IS THE ONE THAT WORKED FOR ME

  • Firsly, we multiply the page height with the opposite of the scale factor pageHeight * (1 - scaleFactor). We need the opposite because the smaller we scale something the more it needs to move from a given position. If we use the normal scale factor here, the smaller we scale an image the less it will translate to the centre.
  • Now the problem is that the translation is still off. Overall it moves the scaled content in the right direction, but just not into the centre. Therefore I tried dividing the calculated factor in the step before through the half of the scale factor. We use the half here because we want the content to appear in the centre. I don't know exactly why it is precisely this value, but as I said it just worked for me!

If you know why this works, feel free to edit this answer :)

SemmelJochen
  • 87
  • 1
  • 8