简体   繁体   中英

addind text with itext7

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.

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)).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM