jeff_ang24
jeff_ang24

Reputation: 129

Rotating text inside PDFPCell iTextPDF C#

I'm currently working for PDF Generator application in C# using iTextPDF library for my organization. How can I make a forty degree rotation to a text inside PDFPCell object. Can I do some tricks for making it work instead of manually using PDFContentByte method, such as ShowTextAligned.

I also provide the sample image about my letter criteria. Thanks. text is rotated using contentbyte class method.

Upvotes: 0

Views: 2780

Answers (1)

mkl
mkl

Reputation: 95908

How can I make a forty degree rotation to a text inside PDFPCell object. Can I do some tricks for making it work instead of manually using PDFContentByte method, such as ShowTextAligned.

This depends on the iText(Sharp) version you use.

In version 5.x and earlier you either have to use ShowTextAligned or you have to manipulate the content stream before and after writing the text to rotate the coordinate system. For drawing the text in a very high level API, therefore, you would pay by very low level content stream manipulation.

In version 7, on the other hand, arbitrary rotation of cell contents is possible in the high level API. For example you can do it like this (for iText/Java; porting to iTextSharp/.Net should be trivial):

try (   PdfWriter pdfWriter = new PdfWriter(target);
        PdfDocument pdfDocument = new PdfDocument(pdfWriter)    )
{
    Document document = new Document(pdfDocument);
    Table table = new Table(new float[]{150, 150});
    Cell cell = new Cell();
    cell.setRotationAngle(40 * Math.PI / 180.0)
        .add(new Paragraph("Test Watermark Text Copyright Test Only"))
        .setBorder(Border.NO_BORDER);
    table.addCell(cell);
    table.addCell(cell.clone(true));
    table.addCell(cell.clone(true));
    table.addCell(cell.clone(true));
    document.add(table);
}

(RotateCellContent.java)

This results in

screenshot


This been said, though, your sample image seems to indicate that you simply want to repeat that rotated text again and again as a background. In this case a solution as suggested by @Bruno in his comment might be more apropos.

I'd suggest using a pattern color, but I'm not sure if that's your exact use case.

This would have the additional advantage that the text would not get in the way of copy&paste / text extraction.

Upvotes: 1

Related Questions