Affine transformation of text to add to a PDF document

Hello, I’m trying to apply an affine transformation to the text that I have to place on a PDF document.

I wrote the following code, but it doesn’t work. The text is printed horizontally and is not rotated of 45 degrees.
What am I doing wrong?

  public static void main(String[] args) {
    // Crea un nuovo documento
    Document doc = new Document();
    Page page = doc.getPages().add();

    // Crea un TextFragment
    TextFragment textFragment = new TextFragment("Testo ruotato di 45 gradi");
    textFragment.setPosition(new Position(200, 500));
    textFragment.getTextState().setFont(FontRepository.findFont("Arial"));
    textFragment.getTextState().setFontSize(12);

    // Applica la trasformazione affine
    applyAffineTransform(page, 200, 500, 45);

    // Aggiungi il TextFragment alla pagina
    page.getParagraphs().add(textFragment);

    // Salva il documento
    doc.save("C:\\test\\doc\\pdfstamp\\output.pdf");
  }

  public static void applyAffineTransform(Page page, double x, double y, double angle) {
    // Converti l'angolo da gradi a radianti
    double radians = Math.toRadians(angle);
    
    // Crea una matrice di trasformazione affine
    double cos = Math.cos(radians);
    double sin = Math.sin(radians);
    ConcatenateMatrix matrix = new ConcatenateMatrix(cos, sin, -sin, cos, x, y);
    
    // Applica la matrice al contenuto della pagina
    page.getContents().add(matrix);
  }

@fabio.parise

To apply an affine transformation to text in a PDF document and achieve a rotation of 45 degrees, you should set the rotation directly on the TextFragment instead of trying to apply a transformation matrix. The TextFragment class has a property for rotation that allows you to specify the angle directly.

Here’s how you can modify your code to rotate the text correctly:

public static void main(String[] args) {
    // Create a new document
    Document doc = new Document();
    Page page = doc.getPages().add();

    // Create a TextFragment
    TextFragment textFragment = new TextFragment("Testo ruotato di 45 gradi");
    textFragment.setPosition(new Position(200, 500));
    textFragment.getTextState().setFont(FontRepository.findFont("Arial"));
    textFragment.getTextState().setFontSize(12);
    
    // Set the rotation of the text
    textFragment.getTextState().setRotation(45); // Set rotation to 45 degrees

    // Add the TextFragment to the page
    page.getParagraphs().add(textFragment);

    // Save the document
    doc.save("C:\\test\\doc\\pdfstamp\\output.pdf");
}

In this updated code, the line textFragment.getTextState().setRotation(45); is crucial as it directly sets the rotation of the text fragment to 45 degrees. This approach is simpler and more effective than applying an affine transformation matrix, which may not yield the desired results for text rotation.

By using the TextFragment’s rotation property, you can easily control the orientation of the text as needed.

If you have any further questions or need additional assistance, feel free to ask!

I’m managing a complex system where I don’t have direct control of the transformations that are applied to the text. I receive an affine transformation matrix that I need to apply to the text to be inserted into the document.

@fabio.parise

Can you please share below information as well so that we can further proceed to investigate the scenario and its feasibility?

  • Sample output generated at the moment using above code
  • Sample expected output PDF
  • Sample values of affine transformation matrix that you receive

The affine transformation can be a generic combination of translation, scale and rotation operations. There is no particular example. I need to manage the generic case, not a single specific case.
As input I have an object of class java.awt.geom.AffineTransform (or similar structure as com.aspose.pdf.Matrix) containing the affine transformation to apply to the text.
As an example code you can refer to this:

public static void main(String[] args) {
    // Crea un nuovo documento
    Document doc = new Document();
    Page page = doc.getPages().add();

    // Crea un TextFragment
    TextFragment textFragment = new TextFragment("Testo ruotato di 45 gradi");
    textFragment.getTextState().setFont(FontRepository.findFont("Arial"));
    textFragment.getTextState().setFontSize(12);

    // Applica la trasformazione affine
    applyAffineTransform(page, matrix);

    // Aggiungi il TextFragment alla pagina
    page.getParagraphs().add(textFragment);

    // Salva il documento
    doc.save("C:\\test\\doc\\pdfstamp\\output.pdf");
  }

@fabio.parise

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-44518

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.