Do you own a Debenu Quick PDF Library version 7, 8, 9, 10, 11, 12, 13 or iSEDQuickPDF license? Upgrade to Debenu Quick PDF Library 14 today!

Debenu Quick PDF Library - PDF SDK Community Forum Homepage
Forum Home Forum Home > For Users of the Library > I need help - I can help
  New Posts New Posts RSS Feed - Text Manipulation / Adding Text to existing PDF
  FAQ FAQ  Forum Search   Register Register  Login Login

Text Manipulation / Adding Text to existing PDF

 Post Reply Post Reply
Author
Message
Halv View Drop Down
Beginner
Beginner


Joined: 17 Sep 12
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote Halv Quote  Post ReplyReply Direct Link To This Post Topic: Text Manipulation / Adding Text to existing PDF
    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


Back to Top
Halv View Drop Down
Beginner
Beginner


Joined: 17 Sep 12
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote Halv Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
AndrewC View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 08 Dec 10
Location: Geelong, Aust
Status: Offline
Points: 841
Post Options Post Options   Thanks (0) Thanks(0)   Quote AndrewC Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Halv View Drop Down
Beginner
Beginner


Joined: 17 Sep 12
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote Halv Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Halv View Drop Down
Beginner
Beginner


Joined: 17 Sep 12
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote Halv Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
AndrewC View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 08 Dec 10
Location: Geelong, Aust
Status: Offline
Points: 841
Post Options Post Options   Thanks (0) Thanks(0)   Quote AndrewC Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Halv View Drop Down
Beginner
Beginner


Joined: 17 Sep 12
Status: Offline
Points: 9
Post Options Post Options   Thanks (0) Thanks(0)   Quote Halv Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 11.01
Copyright ©2001-2014 Web Wiz Ltd.

Copyright © 2017 Debenu. Debenu Quick PDF Library is a PDF SDK. All rights reserved. AboutContactBlogSupportOnline Store