NEW2BI

I am wondering if there is a way to load into memory a PDF file, filling in all of it fillable fields, then save the entire adobo page to a .TIFF image file (so I won't have to print and scan)

I am assuming I will need to buy some special Adobe Plug-in. I already have the Adobe Reader COM object working and it loads the PDF file just fine and displays it. But I want to be able to fill the fillable fields from data entered on a winform and store the data to the Adobe doc in the background (not displayed), then save it to an image.
I am using VB.NET 2005.

Thanks in advance



Re: Visual Basic General Fill in adobe fields and save to image

~DigBoy~

You don't have to buy anything. The product IText provides all the libraries to interract with a PDF

http://www.lowagie.com/iText/

here's an example of how we used it to egt you started (shortened for brevity's sake).

Code Snippet

Imports java.IO 'Requires vjlib library

Imports System.Runtime.InteropServices

Imports com.lowagie.text

Imports com.lowagie.text.pdf

Imports com.lowagie.text.pdf.AcroFields

Imports com.lowagie.text.pdf.PdfReader

Imports com.lowagie.text.pdf.PdfStamper

Module PDFdocuments

Private duration As TimeSpan

Public Function PrintPDF() As Boolean

Dim pdfReader As PdfReader

Dim filename As String = "C:\Documents and Settings\All Users\Application Data\PinksManager\PinkDoc" & PinkId & ".pdf"

Dim isnew As Boolean = False

Try

If Not IO.Directory.Exists("C:\Documents and Settings\All Users\Application Data\PinksManager") Then

IO.Directory.CreateDirectory("C:\Documents and Settings\All Users\Application Data\PinksManager")

isnew = True

Else

If Not IO.File.Exists(filename) Then isnew = True

End If

If isnew Then

Application.DoEvents()

Dim t As DataTable = dh.RunSelectQuery(PinkQuery(PinkId))

If t Is Nothing Then

Throw New DataException("The record could not be retrieved: " & dh.LastException.Message)

Return False

End If

Dim r As DataRow = t.Rows(0)

'We need the domain name to see which acrobat form to use. The username is shortened for the form.

Dim username As String = CStr(r("CompletedBy"))

Dim dom As String = ""

If username.Length >= 2 And username.ToLower <> "admin" Then

If Not My.User.Name.IndexOf("\") = -1 Then

dom = username.Substring(0, username.IndexOf("\")).ToUpper

username = username.Replace(dom, "").Replace("\", "")

Else

dom = ""

End If

End If

'Get the file name of the logo image depending on the document's creater's domain.

Dim ImageName As String = ""

Select Case dom.ToUpper

Case "MADISONRF", "ACCOUNTING", "PAYROLL"

ImageName = "MRF_Logo.JPG"

Case "NESC"

ImageName = "NESC_Logo.JPG"

Case "NATLEMP"

ImageName = "NatlEmp_Logo.JPG"

Case "TOTALTECH"

ImageName = "TTS_Logo.JPG"

Case Else

ImageName = "MRF_Logo.JPG"

End Select

Dim cb As PdfContentByte

Dim Logo As Image

Dim TempPath As String = My.Application.Info.DirectoryPath & "\Documents\Contract.PDF"

pdfReader = New PdfReader(TempPath)

'EXPLICIT OBJECT INSTANTIATION

Dim stamp2 As New pdf.PdfStamper(pdfReader, New FileOutputStream(filename))

Dim form2 As pdf.AcroFields

'Place the company logo in the allocated space -- this is one document that imports one of four images.

cb = stamp2.getOverContent(1)

Logo = Image.getInstance(My.Application.Info.DirectoryPath & "\Documents\" & ImageName)

Logo.setAbsolutePosition(45, 685)

Logo.scaleAbsoluteWidth(125)

Logo.scaleAbsoluteHeight(70)

cb.addImage(Logo)

form2 = stamp2.getAcroFields()

form2.setField("txtCompany", r("FullCompanyName"))

form2.setField("txtCompanyRep", "")

form2.setField("txtOfficeLoc", r("StateName"))

form2.setField("txtBranch", r("BranchName"))

form2.setField("txtSSN", (r("SSN")))

form2.setField("txtFirstName", r("FirstName"))

form2.setField("txtLastName", r("LastName"))

form2.setField("txtInitial", r("Initial"))

stamp2.close()

End If

'Not sure how to convert it to a TIFF but you are welcome to brwose through the voluminous list of objects through intellisense.

Catch ex As Exception

Throw New Exception("Failed to create the PDF document: " & ex.Message)

Return False

End Try

End Function

End Module





Re: Visual Basic General Fill in adobe fields and save to image

NEW2BI

Thanks! What about after I get all the fields populated, can I then save the pdf to a .tif image file as well or will I have to figure another way out for that





Re: Visual Basic General Fill in adobe fields and save to image

~DigBoy~

I think it was you who put a similar post out there yesterday or the day before on that subject. I spent about 10 minutes poring through the libraries of the lowagie class but could not find anything that smelled of that kind of operation. I would pose a question to the IText site's forum (if there is one) to see what someone there has to say about it. I'm sure there is some way to do it because Adobe has that export option. The question is does the 3rd party wraper class have that feature implemented





Re: Visual Basic General Fill in adobe fields and save to image

NEW2BI

Hello All,

After a week of reading through Adobe SDK documentation and looking through a lot of forums, I have successfully written a test application to convert PDF to .TIFF. Note: you must have the Adobe standard or Professional 7.0 or greater installed to make this work. I did it using the trial version of Adobe Pro 8.0. You will have to set a reference to the Acrobat COM object. Here is my coding in VB.NET:

Code Block

Public Class Form1

Dim PDFApp As Acrobat.AcroApp

Dim PDDoc As Acrobat.CAcroPDDoc

Dim AVDoc As Acrobat.CAcroAVDoc

Private Sub btnBrowsePath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowsePath.Click

Dim result As DialogResult

OpenFileDialog1.FileName = ""

OpenFileDialog1.AddExtension = True

OpenFileDialog1.DefaultExt = ".TIF"

OpenFileDialog1.Filter = "Adobe PDF Files (*.PDF) | *.PDF"

OpenFileDialog1.InitialDirectory = "DeskTop"

result = OpenFileDialog1.ShowDialog()

If (result = Windows.Forms.DialogResult.OK) Then

Me.txtPDFPath.Text = OpenFileDialog1.FileName

End If

End Sub

Private Sub btnLoadPDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadPDF.Click

' Dim javaString As String

Dim JSObj As Object, strPDFText As String

' Create Acrobat Application object

PDFApp = CreateObject("AcroExch.App")

' Create Acrobat Document object

PDDoc = CreateObject("AcroExch.PDDoc")

' Open PDF file

PDDoc.Open(Me.txtPDFPath.Text)

' Create AV doc from PDDoc object

AVDoc = PDDoc.OpenAVDoc("TempPDF")

' Hide Acrobat application so everything is done in silent mode

PDFApp.Hide()

' Create Javascript bridge object

JSObj = PDDoc.GetJSObject()

' Test making change to one of the text fields

' Syntax for GetField method GetField( strFieldName )

JSObj.GetField("Text2.0.0.0.0").value = "JESSICA"

' Test pulling data from a PDF text field and showing in a Windows Messagebox

strPDFText = JSObj.GetField("Text2.0.0.2.0").value

' Attempt to save PDF to TIF image file.

' SaveAs method syntax .SaveAs( strFilePath, cConvID )

' For TIFF output the correct cConvid is "com.adobe.acrobat.tiff"

' cCovid MUST BE ALL LOWERCASE.

JSObj.SaveAs("C:\TestTiffs\TestGood.tif", "com.adobe.acrobat.tiff")

PDDoc.Close()

PDFApp.CloseAllDocs()

' Clean up

System.Runtime.InteropServices.Marshal.ReleaseComObject(JSObj)

JSObj = Nothing

System.Runtime.InteropServices.Marshal.ReleaseComObject(PDFApp)

PDFApp = Nothing

System.Runtime.InteropServices.Marshal.ReleaseComObject(PDDoc)

PDDoc = Nothing

System.Runtime.InteropServices.Marshal.ReleaseComObject(AVDoc)

AVDoc = Nothing

End Sub

End Class





Re: Visual Basic General Fill in adobe fields and save to image

~DigBoy~

I'm impressed! It took only a week to navigate the SDK labarynth successfully. I hope you left a trail of bread crumbs to find your way out.

Thanks for posting the solution. I'll keep it in my toolbox.





Re: Visual Basic General Fill in adobe fields and save to image

NEW2BI

One more thing to add that I ran into is that on the SaveAs method, you cannot use a system or root level folder or the SaveAs will fail.