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.

Thu Dec 19, 2013 9:20 am

Hello,

Is it possible to insert text or graphic in an existing PDF in a existing page.

Thanks.

Best Regards

inyazar
 
Posts: 24
Joined: Wed Jul 03, 2013 8:38 am

Fri Dec 20, 2013 3:25 am

Hello,

Thanks for your inquiry.
I provide you with some example code as below, and attched pdf documents(original, result). Hope can help you.
Code: Select all
static void Main(string[] args)
        {
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(@"..\..\test.pdf");
            PdfPageBase page = document.Pages[0];
         
            DrawGraphics(page);

            PdfSolidBrush brush = new PdfSolidBrush(Color.Green);
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 20f);
            // draw text
            page.Canvas.DrawString("Hello World", font, brush, 20, 400);

            document.SaveToFile("result.pdf");
            System.Diagnostics.Process.Start("result.pdf");
        }
        static void DrawGraphics(PdfPageBase page)
        {
            PointF[] points = new PointF[5];
            for (int i = 0; i < points.Length; i++)
            {
                float x = (float)Math.Cos(i * 2 * Math.PI / 5);
                float y = (float)Math.Sin(i * 2 * Math.PI / 5);
                points[i] = new PointF(x, y);
            }
            PdfPath path = new PdfPath();
            path.AddLine(points[2], points[0]);
            path.AddLine(points[0], points[3]);
            path.AddLine(points[3], points[1]);
            path.AddLine(points[1], points[4]);
            path.AddLine(points[4], points[2]);

            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();
            PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f);

            page.Canvas.ScaleTransform(50f, 50f);
            page.Canvas.TranslateTransform(2f, 5f);
            page.Canvas.DrawPath(pen, path);

            PdfRadialGradientBrush brush = new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Red, Color.Blue);
            page.Canvas.TranslateTransform(3f, 0.1f);
            path.FillMode = PdfFillMode.Winding;
            page.Canvas.DrawPath(pen, brush, path);

            //restore graphics
            page.Canvas.Restore(state);
        }


Best regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Thu Dec 26, 2013 9:02 am

Hello,

Have you tried the code? Did it resolve your issue?
Thanks for your feedback.

Best regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Jan 15, 2014 12:18 pm

Hi Gary,

I need to insert text into an especific position in the PDF file.

I need to select the position with the mouse. The user will do it.

Is it posible to use PDF and PDF viewer to do it?
May I have an example in VB.NET.

Thanks.

Best regards,
Galder

inyazar
 
Posts: 24
Joined: Wed Jul 03, 2013 8:38 am

Thu Jan 16, 2014 6:19 am

Hello Galder,

Thanks for your feedback.
If you need to achieve it by programmatically, you only get the position selected by the user and pass it to the following method.
Code: Select all
page.Canvas.DrawString("This is position test", font, brush, position)
and all the code is also as below.
If you want to achieve timely interaction, sorry that Our Spire.PDF only supports in code-behind to operate the PDF file.
Code: Select all
Dim pdfdocument As New PdfDocument
pdfdocument.LoadFromFile("Sample.pdf")
Dim page As PdfPageBase
page = pdfdocument.Pages(0)
Dim font As PdfFont = New PdfFont(PdfFontFamily.Helvetica, 10.0F)
Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Blue)
Dim position As PointF = New PointF(100, 100)
page.Canvas.DrawString("This is position test", font, brush, position)
pdfdocument.SaveToFile("result.pdf", FileFormat.PDF)


If there are any questions, welcome to get it back to us.
Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Wed Jan 22, 2014 9:05 am

Hello Galder,

Has the issue been resolved? Could you please give us some feedback if convenience?

If there are any questions, welcome to get it back to us.

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Return to Spire.PDF