Print Page | Close Window

Text Manipulation / Adding Text to existing PDF

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2407
Printed Date: 25 Apr 25 at 11:19AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Text Manipulation / Adding Text to existing PDF
Posted By: Halv
Subject: Text Manipulation / Adding Text to existing PDF
Date Posted: 17 Sep 12 at 10:33PM
Greetings,

I looked over the Quick PDF Library 8 Developer Guide and used the sample code provided on the site but I am having some trouble to achieve what I want. When I used the sample code and the qp.DrawText, it overwrote my PDF file with just the input which was specified in the code. So I am not sure if it's because of the way it was saved with qp.SaveToFile that this happened or if it is working as intended.

Basically my question is if I have an existing PDF file which QP.function should I be using to add text to the file so that it does not overwrite the original PDF file? I am not editing any text, I only want to add text to the PDF file. I apologize in advance if someone mentioned this already. I tried searching but did not find anything similar to what I am looking for.

Thanks in advance,

Halv





Replies:
Posted By: Halv
Date Posted: 18 Sep 12 at 12:58AM
Greetings,

I figured it out. Now I feel bad for asking this silly question. I had to load the PDF file, do the qp.DrawText, then save it to a different file name.

Halv


Posted By: AndrewC
Date Posted: 18 Sep 12 at 9:12AM
Also, if you find that the DrawText is not drawing the text in the correct orientation, size or position then you will need to call QP.NormalizePage(0); to fix the page.

  QP.LoadFromFile('pages.pdf', '');
  for i := 1 to QP.PageCount() do
  begin
    QP.SelectPage(i);
    QP.NormalizePage(0);

    QP.DrawText('My TEXT');
  end
  QP.SaveToFile('stamped_pages.pdf');


Andrew.


Posted By: Halv
Date Posted: 18 Sep 12 at 9:14PM
Thanks for the tip. I actually did run into a problem with the DrawText on what you advised on and I tried using your example but I am not sure if I did something wrong. This is what I have (Using Microsoft Visual Studio 2010 C#).

        private void btnDrawText_Click(object sender, EventArgs e)
        {
            // Setup the parameters
            string fileName = @"..\..\Test Files\Daily Service Log.pdf";
            qp.LoadFromFile(fileName, "");
            // Check to see if the library has been successfully unlocked
            if (qp.Unlocked() == 1)
            {
                qp.NormalizePage(0);
                qp.DrawText(50, 50, "This is a test.");
                qp.SaveToFile(@"..\..\Test Files\flattened_pdf_form.pdf");
                System.Diagnostics.Process.Start(@"..\..\Test Files\flattened_pdf_form.pdf");
            }
            else
            {
                // If library could not be unlocked warn the user
                MessageBox.Show("License key could not be validated. The library was not unlocked.");
            }
 
When I run that code the DrawText would show up at the bottom left of the screen. I did not mention this but the Daily Service Log PDF file was created with Microsoft Word 2010 by saving it as a PDF file. I'm not sure if that may be causing a problem but just throwing it out there. I'll be working on this more and update if anything changes.


Posted By: Halv
Date Posted: 18 Sep 12 at 9:52PM
Ah I'm not sure why I could not get the qp.NormalizePage to work but I looked over the Quick PDF Library 8 Developer Guide and used qp.SetOrigin(1) and that did it for me!

private void btnDrawText_Click(object sender, EventArgs e)
        {
            // Setup the parameters
            string fileName = @"..\..\Test Files\Daily Service Log.pdf";
            qp.LoadFromFile(fileName, "");
            // Check to see if the library has been successfully unlocked
            if (qp.Unlocked() == 1)
            {
                qp.SetOrigin(1);
                qp.DrawText(50, 50, "This is a test.");
                qp.SaveToFile(@"..\..\Test Files\flattened_pdf_form.pdf");
                System.Diagnostics.Process.Start(@"..\..\Test Files\flattened_pdf_form.pdf");
            }
            else
            {
// If library could not be unlocked warn the user
                MessageBox.Show("License key could not be validated. The library was not unlocked.");
            }
        }

Thanks for the tip once again. It helped me look over the developer guide again and understand the difference in versions of Adobe Acrobat and why the point of origin was starting at the bottom left instead of the top left. I apologize though I should have read the guide more carefully. It answers a lot of questions. Hopefully I'll be good to go now.

Halv


Posted By: AndrewC
Date Posted: 19 Sep 12 at 12:56AM
Halv,

SetOrigin(1) will set the orgin (0,0) to the top left of the page normally.

You code looks good.  I have moved the LoadFromFile to be inside the QP.IsUnlocked call as LoadFromFile will fail also if the library is not unlocked.

private void btnDrawText_Click(object sender, EventArgs e)
        {
            // Setup the parameters
            string fileName = @"..\..\Test Files\Daily Service Log.pdf";
            // Check to see if the library has been successfully unlocked
            if (qp.Unlocked() == 1)
            {
                qp.LoadFromFile(fileName, "");
                qp.SelectPage(1);
                qp.SetOrigin(1);
                qp.DrawText(50, 50, "This is a test.");
                qp.SaveToFile(@"..\..\Test Files\flattened_pdf_form.pdf");
                System.Diagnostics.Process.Start(@"..\..\Test Files\flattened_pdf_form.pdf");
            }
            else
            {
// If library could not be unlocked warn the user
                MessageBox.Show("License key could not be validated. The library was not unlocked.");
            }
        }

NormalizePage is required for a fre reasons.  The drawing commands in a PDF are grouped together into something called a contentstream.  DrawText basically appends a few commands to the end of the content stream.  Often developers just create the content stream to get the job jone and they don't reset the graphics state back to the default state.  The don't expect others to be adding to the content stream.  So many content streams leave the page scaling, rotation etc in an unknown state and this affects our DrawText function.  NormalizePage basically wraps the existing content stream in with   savestate  <oldcontentstream> restorestate  commands so that we can restore the contentstram to a known state before calling DrawText.  This comment will hopefully help others in the forum understand why NormalizePage is required.


Posted By: Halv
Date Posted: 19 Sep 12 at 2:00AM
Thank you AndrewC for the help and advice. I'll make sure to add the LoadFromFile within the "if" statement.

Halv



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk