Forum Home
Press F1
 
Thread ID: 142148 2016-05-05 00:18:00 Help needed with VBA and PDFs Tony (4941) Press F1
Post ID Timestamp Content User
1420119 2016-05-05 00:18:00 I'm trying to dynamically modify an existing PDF document using data from an Access database, and my head is starting to hurt. I can get access to the file, and I can insert a form field, but what I really want to do is just insert some text that will then become part of the PDF. I'm finding it really hard to see how this might be done, and I don't know whether it is because I am being thick (no laughter please) or whether it can't be done. The Adobe documentation is really opaque, and when I look on the Adobe forums, most of the respondents seem to be somewhat aloof and unhelpful.

Any help will be gratefully accepted - and stop me living on Panadol :)
Tony (4941)
1420120 2016-05-05 02:07:00 Seeing what you have tried and what is failing would help. I dont suggest using a code tag, can't see it on my phone. Kame (312)
1420121 2016-05-05 04:57:00 OK, here ' s what I have so far. It is all just bits and pieces stolen from places on the net and my own code while I try stuff out:
Starts here --->
Sub PF1code()

FileNm = "C:\Users\Tony\Desktop\access\Email development\SubsTemplateForm.pdf" ' File location
Set gApp = CreateObject("AcroExch.app")

Set avDoc = CreateObject("AcroExch.AVDoc")
If avDoc.Open(FileNm, "") Then
Set pdDoc = avDoc.GetPDDoc()

Debug.Print pdDoc.GetFileName ' this does show the file name

Set jso = pdDoc.GetJSObject
'
Dim thePDF As CAcroPDDoc
Dim theSelect As CAcroPDTextSelect
Dim thePage As CAcroPDPage
Dim thePageSize As Object
Dim theRect As Object

' this bit is me trying stuff

Set thePage = pdDoc.AcquirePage(0)
Set thePageSize = thePage.GetSize

Set theRect = CreateObject("AcroExch.Rect")
theRect.Left = 100
theRect.Right = 225
theRect.bottom = 668
theRect.Top = 686

Set theSelect = pdDoc.CreateTextSelect(0, theRect)

Dim res As Variant

res = SetTextSelection
res = ShowTextSelect

' This next bit works

Dim rectangle As CAcroRect

Dim rect(3) As Integer
' Current Member Type
rect(0) = 165 ' x lower left
rect(1) = 668 ' y lower left
rect(2) = 225 ' x upper right
rect(3) = 686 ' y upper right


' add a form field
Set Field = jso.addField("myFormField", "text", 0, rect)
Field.Value = "Full"
jso.flattenPages
pdDoc.Save PDSaveIncremental, "C:\Users\Tony\Desktop\access\Email development\test document(2).pdf" ' Save changes to the PDF document
pdDoc.Close


' Close the PDF; the True parameter prevents the Save As dialog from showing
avDoc.Close (True)

' Some cleaning
Set gApp = Nothing
Set avDoc = Nothing
Set pdDoc = Nothing
Set jso = Nothing

endif

End Sub

<---- ends here

All very messy, but I ' m just trying stuff out.
Tony (4941)
1420122 2016-05-05 10:32:00 Well considering you are using javascript to insert the field, you should use addWatermarkFromText to insert text, the name may seem confusing but it will serve your purpose.

Look at Adobe's livedocs for their javascript API to understand the parameters.
Kame (312)
1420123 2016-05-05 21:35:00 I would also try another filename in this line one without brackets

pdDoc.Save PDSaveIncremental, "C:\Users\Tony\Desktop\access\Email development\test document(2).pdf" 'Save changes to the PDF document
beama (111)
1420124 2016-05-05 22:04:00 I would also try another filename in this line one without brackets

pdDoc.Save PDSaveIncremental, "C:\Users\Tony\Desktop\access\Email development\test document(2).pdf" 'Save changes to the PDF documentAre you saying that the brackets will confuse it? As far as I can see that is one of the few bits that is actually working!
Tony (4941)
1420125 2016-05-05 22:07:00 Well considering you are using javascript to insert the field, you should use addWatermarkFromText to insert text, the name may seem confusing but it will serve your purpose.

Look at Adobe's livedocs for their javascript API to understand the parameters.Thanks. I'll check that out. What I was sort of expecting to be able to do was something like:


Set Text = jso.addText("text string I want to add", 0, rect) ' made up code and syntax
Tony (4941)
1420126 2016-05-05 23:49:00 Up till now I've not been actually saving the PDFs. I've just discovered that "flattenpages" will actually convert a form field to just text on the page. I've also had a look at watermarks and it looks like they would also work. So I think I'm good to go - until the next roadblock that is.

Any opinion whether there is any advantage of watermarks against form fields?

Thanks everyone for the help.
Tony (4941)
1420127 2016-05-05 23:49:00 Are you saying that the brackets will confuse it? As far as I can see that is one of the few bits that is actually working!

if its working leave it alone, you ll find there are some funny quirks with vba
beama (111)
1420128 2016-05-06 00:14:00 Whoops! Scrub "flattenpages". I need the final document to be combination of pre-created text, dynamic data that I will insert, and pre-created form fields for the eventual recipient to fill in. If I flatten the pages then all the form fields including the pre-existing ones, will disappear. So it looks like it is watermarks or the bush... :) Tony (4941)
1 2