Adding text to existing PDF

The PDF Creator .NET Library enables you to create secure PDF documents on the fly and to view, print and process existing PDF documents. If you have any questions about PDF Creator .Net, please post them here.
Post Reply
timmed
Posts: 4
Joined: Fri Nov 10 2006

Adding text to existing PDF

Post by timmed »

I have an existing PDF document that I'm trying to open in vb.net and then simply add a block of text and save it back.

I found some sample code but it doesn't even compile, let alone work right. Does anyone have a sample of how to do this?

With PDF1
.Open "c:\temp\test.pdf", "" ' open a sample PDF document
.ReportState = acReportStateDesign ' switch the control to design mode
' create a text to add to the document
.CreateObject acObjectTypeText, " Text 1"
' text to display
.ObjectAttribute("Text 1", "Text") = "Hello There"
' position the text
.ObjectAttribute("Text 1", "Left") = 200
.ObjectAttribute("Text 1", "Top") = 200
.ObjectAttribute("Text 1", "Right") = 600
.ObjectAttribute("Text 1", "Bottom") = 400
' set background color to yellow
.ObjectAttribute("Text 1", "BackColor") = "7FFFFF"
.Save "c:\temp\testFrame.pdf", 1 ' save the modified document
End With
End Sub

Thanks!
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

Hello,

This sample code opens an existing pdf file and adds two text objects and a new page to it.

Code: Select all

Private Sub ExistingPDF_Click()

On Error GoTo report_error

' To run this we need to have an existing pdf file here
' it is called "TestFile.pdf" and saved in the same folder as this exe.


With CreatorRef
    'Open a file generated by the pdf creator
    .Open App.Path & "TestFile.pdf", ""
    .SetLicenseKey Licensee, ActivationCode
    ' Set report state to design mode
    ' NEVER CHANGE THE MODE USING .ReportState = acReportStateRun
    .DoCommandTool acCommandToolDesignMode
    
    ' get a reference to the object that we just created
    Set obj = .GetObjectByName("picture1")
    If obj Is Nothing Then
       MsgBox "Unable to find object"
        Exit Sub
    End If
    
    'Position the text
    With obj
        .Attribute("Bottom") = 4000
    End With
   
    ' Add a page to it
    .AddPage (2)
   
    'Create a Text on the second page
    .CreateObject acObjectTypeText, "Page2"
    
    ' get a reference to the object that we just created
    Set obj = .GetObjectByName("Page2")
    If obj Is Nothing Then
        MsgBox "Unable to find object"
        Exit Sub
    End If
    
    'Position the text
    With obj
        .Attribute("Left") = 400
        .Attribute("Top") = 1500
        .Attribute("Right") = 800
        .Attribute("Bottom") = 1500
        .Attribute("Text") = "Page 2"
    End With
     
    ' compile the document and switch to Run mode
    .DoCommandTool acCommandToolRunDocument
 
   .Save App.Path & "\EditedDocument.pdf", 0
End With

DocumentName = App.Path & "\EditedDocument.pdf"
    
Exit Sub

report_error:
    MsgBox Err.Description
    MsgBox Err.Source

End Sub
Hope this helps.
Custom Brand the Amyuni PDF Printer Driver http://www.amyuni.com/en/developer/branding/index.html

Amyuni PDF Converter tested for true PDF performance. View results - http://www.amyuni.com/benchmark
Post Reply