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 Jul 04, 2018 4:57 pm

I wonder if it is possible to add a curved text into last page of an existing pdf file or add an image and set a text over it.

Thanks in advanced

fenderEvrby
 
Posts: 5
Joined: Mon May 21, 2018 6:29 pm

Thu Jul 05, 2018 2:20 am

Dear fenderEvrby,

Thanks for your inquiry.
1. Sample code for adding image and setting a text over it.
Code: Select all
            PdfDocument doc = new PdfDocument("sample.pdf");
            PdfPageBase page = doc.Pages[0];
            Image image = Image.FromFile(@"F:\image\image1.jpg");
            PdfImage pdfimage = PdfImage.FromImage(image);
            float x = 5;
            float y = 10;
            //draw image
            page.Canvas.DrawImage(pdfimage, x, y);
            PdfUnitConvertor uinit = new PdfUnitConvertor();
            //get the image size in point
            SizeF NewSize = uinit.ConvertFromPixels(image.Size, PdfGraphicsUnit.Point);
            System.Drawing.Font font = new System.Drawing.Font("Calibri", 18, FontStyle.Regular);
            PdfTrueTypeFont ttf = new PdfTrueTypeFont(font, true);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
            string text = "text";
            Graphics g = Graphics.FromImage(new Bitmap(1, 1));
            g.PageUnit = GraphicsUnit.Point;
            //measure the text and get its size in point
            SizeF sim = g.MeasureString(text, font);
            page.Canvas.DrawString(text, ttf, brush, x + (NewSize.Width - sim.Width) / 2, y + (NewSize.Height - sim.Height) / 2);
            doc.SaveToFile("14313.pdf");

2. Sample code for adding curved text.
Code: Select all
            PdfDocument doc = new PdfDocument();         
            PdfPageBase page = doc.Pages.Add();
            string s = "test test test";
            var brush = new PdfSolidBrush(Color.Black);
            var font = new Font("Arial", 20);
            PdfTrueTypeFont tfont = new PdfTrueTypeFont(font, true);
            var origin = new Point(200, 200);
            int radius = 50;
            var format = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            page.Canvas.TranslateTransform(origin.X, origin.Y);
            //you might need to change the arithmetic to draw your text
            foreach (var c in s)
            {
                page.Canvas.RotateTransform(170f / s.Length);
                page.Canvas.DrawString(c.ToString(), tfont, brush, new PointF(0, -radius), format);
            }
            doc.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle270;
            doc.SaveToFile("CurvedText14313.pdf");

Hope this helps.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Jul 10, 2018 3:12 am

Dear fenderEvrby,

Greetings from E-iceblue.
Did the code I provided help you solve your issue ? Could you please give us some feedback at your convenience ?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF