Reputation: 63
I want to add text into every page of my pdf which is rotaded from bottom to top.
Like this:
Here is my code:
PdfDocument srcDocument = new PdfDocument(new PdfReader(file));
PdfDocument destDocument = new PdfDocument(new PdfWriter(newfile));
int pagesCount = srcDocument.GetNumberOfPages();
for (int i = 1; i <= pagesCount; i++)
{
srcDocument.CopyPagesTo(i, i, destDocument);
PdfCanvas pdfCanvas = new PdfCanvas(srcDocument.GetPage(i));
}
srcDocument.Close();
destDocument.Close();
In this I got stuck. I don't know how to go further : write and rotate text.
Upvotes: 1
Views: 181
Reputation: 949
So, as per the comments on the original question, you basically need to print on a Canvas object, and rotate the Paragraph. The Rectangle basically defines the coordinates where you will place the Canvas. Something like this should work:
PdfDocument srcDocument = new PdfDocument(new PdfReader(file));
PdfDocument destDocument = new PdfDocument(new PdfWriter(newfile));
FontProgram fontProgram =
FontProgramFactory.CreateFont(@"C:\temp\calibri.ttf");
PdfFont calibri = PdfFontFactory.CreateFont(fontProgram, PdfEncodings.WINANSI);
int pagesCount = srcDocument.GetNumberOfPages();
for (int i = 1; i <= pagesCount; i++)
{
srcDocument.CopyPagesTo(i, i, destDocument);
PdfCanvas pdfCanvas = new PdfCanvas(destDocument.GetPage(i));
Canvas canvas = new Canvas(pdfCanvas, new Rectangle(36, 750, 100, 50));
canvas.Add(new Paragraph("0001").SetRotationAngle(1.5708).SetFont(calibri).SetFontSize(4));
canvas.Close();
}
srcDocument.Close();
destDocument.Close();
Alternatively, you can set a Style, if you plan on reusing a lot, something like:
Style rotatedStuff = new Style()
.SetRotationAngle(1.5708)
.SetFont(calibri)
.SetFontSize(4);
and then just apply it to the Paragraph with the AddStyle() method.
Mind you that if you provide with a float number, it's in rads, so 90° is 1.5708 rad (1 Degree (°) = 0.01745 Radian (rad)).
Upvotes: 2