0

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.

jeff_ang24
  • 119
  • 4
  • 12
  • Creating a `PdfTemplate` then wrap it inside an image and rotate that image is one option. Creating a pattern color with the rotated text is another option. Using a `PdfPCell` event is the option you probably already found. Whatever works best depends on the exact use case. Looking at your screen shot, I don't see cells. Looking at your screen shot, I'd suggest using a pattern color, but I'm not sure if that's your exact use case. – Bruno Lowagie May 23 '16 at 06:40
  • If pdfpcell in itextsharp can make a 45 degree rotation, this case was solved early... lol... i will try to use pdftemplate as your suggestion... – jeff_ang24 May 25 '16 at 01:41
  • hmm... I still do not get the information from "Creating a PdfTemplate then wrap it inside an image and rotate that image". Does it mean that I create rotated text template inside an image then using these template as watermark for creating the new pdf...? – jeff_ang24 May 25 '16 at 02:37
  • @Bruno I try using your tutorial at [link](http://developers.itextpdf.com/examples/graphics/pattern-colors#1325-textpattern.java) . I get the point, but my result was ![pattern color](https://www.dropbox.com/s/h69tk71rn6okej5/pattern%20color.PNG?dl=0), and my code was: at [link](https://www.dropbox.com/s/yyih7vmwa1cunrg/pattern_color.txt?dl=0) – jeff_ang24 May 25 '16 at 03:28

1 Answers1

1

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.

mkl
  • 90,588
  • 15
  • 125
  • 265