Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Oct 16, 2019 5:11 am

I have 1 PDF file available, please see picture 1
I use the code to add text to the pdf file as follows:
// Code
string pdffile = @ "C: \ 201908291055.pdf";

            PdfDocument doc = new PdfDocument ();
            doc.LoadFromFile (pdffile);
string HeaderStr = "Im OK ADD TEXT";
            PdfTrueTypeFont font = new PdfTrueTypeFont (new Font ("Arial", 13f, FontStyle.Regular), true);
            PdfBrush brush = PdfBrushes.Blue;

            float postx = 371.33963f;
            float posty = 320.942139f;
            string HeaderStr = "Im OK ADD TEXT";
            RectangleF rectf = new RectangleF (postx, posty, 0, 0);
            doc.Pages [0] .Canvas.DrawString (HeaderStr, font, brush, rectf);
            string result = @ "C: \ itemok.pdf";
            doc.SaveToFile (result);
            doc.Close ();
// end code.
but the end result did not work as expected.
Please help me, I am thinking the pdf file is PdfPageRotateAngle.RotateAngle270. make it affect adding text,
Help me
tks,
MR THUY
Last edited by thuytd86 on Sun Oct 20, 2019 9:57 am, edited 1 time in total.

thuytd86
 
Posts: 13
Joined: Wed Oct 16, 2019 4:17 am

Wed Oct 16, 2019 6:42 am

Hi,

Thanks for your inquiry.
To help us investigate your issue accurately, please offer us your input Pdf file. You could upload it here or send us([email protected]) via email.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Wed Oct 16, 2019 7:49 am

Hi,
When I open the PFD file in a browser it was very nice,
But using Spire.PDF is completely different,
I could not determine the exact coordinates of the file pdf.

thuytd86
 
Posts: 13
Joined: Wed Oct 16, 2019 4:17 am

Wed Oct 16, 2019 11:03 am

Hi,

Thanks for your information.
Since the page of your Pdf file is rotated, please create a Pdf page template without rotation first, then the coordinate will be normal and draw the text on the new page.

Code: Select all
            PdfDocument doc = new PdfDocument();
          doc.LoadFromFile(@"……\filePDF.pdf");

            //create a new pdf
            PdfDocument newPdf = new PdfDocument();
            foreach (PdfPageBase page in doc.Pages)
            {
                //create a page template without rotation.
                PdfPageBase newPage;
                if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
                    newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
                else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
                    newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
                else
                    newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
                page.CreateTemplate().Draw(newPage, new PointF(0, 0));

                string HeaderStr = "Im OK ADD TEXT";
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f, FontStyle.Regular), true);
                PdfBrush brush = PdfBrushes.Blue;

                // reset the position of the text
                float postx = 250.33963f;
                float posty = 480.942139f;

                RectangleF rectf = new RectangleF(postx, posty, 0, 0);
                newPage.Canvas.DrawString(HeaderStr, font, brush, rectf);
            }
                newPdf.SaveToFile(@"result.pdf", FileFormat.PDF);

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Wed Oct 16, 2019 2:03 pm

Hello team
Everything works so well, I didn't think of how to create a pdfdocument template, which is awesome
Thanks very much

thuytd86
 
Posts: 13
Joined: Wed Oct 16, 2019 4:17 am

Thu Oct 17, 2019 1:09 am

Hi,

Thanks for your feedback.
Any question, welcome to contact us. Have a nice day.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.PDF