Random field displacement
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=1106
Printed Date: 22 Nov 24 at 6:46PM Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com
Topic: Random field displacement
Posted By: gkell
Subject: Random field displacement
Date Posted: 09 Jun 09 at 8:41PM
I use open a file(1) and populate fields accordingly then I open a file(2) that is a copy of one of the pages from file1 and populate it and merge with file1, but continue to open file2 and merge with file1 as many times as i need. The problem is randomly text is place in the wrong spots, but I have triple checked everything. example:
pdf.LoadFromFile (gsWebsiteFilePath & "\PCPPlan.pdf")
lDocumentID = pdf.SelectedDocument lDocCnt = 0
ReDim Preserve aDoc(lDocCnt) aDoc(lDocCnt) = lDocumentID ====fill====
For i = pdf.FormFieldCount To 1 Step -1 lret = pdf.FlattenFormField(i) Next
Do Until EOF
pdf.LoadFromFile (gsWebsiteFilePath & "\PCPPlan.pdf") lDocumentID = pdf.SelectedDocument lDocCnt = lDocCnt + 1 ReDim Preserve aDoc(lDocCnt) aDoc(lDocCnt) = lDocumentID
====fill====
For i = pdf.FormFieldCount To 1 Step -1 lret = pdf.FlattenFormField(i) Next
lret = pdf.SelectDocument(aDoc(0)) lret = pdf.MergeDocument(aDoc(lDocCnt))
loop
lret = pdf.SaveToFile("funstuff")
|
Replies:
Posted By: DELBEKE
Date Posted: 17 Jun 09 at 8:16AM
Hello and wellcome
Your code seem correct and i can't explain why this random problem occur.
Just a thing about your code. You don't need to keep all the document's Id in a array. When you do the merge function, the second document is unloaded and it's id is no longer valid
pdf.LoadFromFile (gsWebsiteFilePath & "\PCPPlan.pdf")
lDocumentID = pdf.SelectedDocument
lFirstDoc = lDcoumentID lDocCnt = 0
====fill====
For i = pdf.FormFieldCount To 1 Step -1 lret = pdf.FlattenFormField(i) Next
Do Until EOF 'I don't understand when EOF change to true/false
pdf.LoadFromFile (gsWebsiteFilePath & "\PCPPlan.pdf") lDocumentID = pdf.SelectedDocument lDocCnt = lDocCnt + 1 ====fill====
For i = pdf.FormFieldCount To 1 Step -1 lret = pdf.FlattenFormField(i) Next
lret = pdf.SelectDocument(lFirstDoc) lret = pdf.MergeDocument(lDocumentID)
loop
lret = pdf.SaveToFile("funstuff")
I use open a file(1) and populate fields accordingly then I open a file(2) that is a copy of one of the pages from file1 and populate it and merge with file1, but continue to open file2 and merge with file1 as many times as i need. The problem is randomly text is place in the wrong spots, but I have triple checked everything. example:
pdf.LoadFromFile (gsWebsiteFilePath & "\PCPPlan.pdf")
lDocumentID = pdf.SelectedDocument lDocCnt = 0
ReDim Preserve aDoc(lDocCnt) aDoc(lDocCnt) = lDocumentID ====fill====
For i = pdf.FormFieldCount To 1 Step -1 lret = pdf.FlattenFormField(i) Next
Do Until EOF
pdf.LoadFromFile (gsWebsiteFilePath & "\PCPPlan.pdf") lDocumentID = pdf.SelectedDocument lDocCnt = lDocCnt + 1 ReDim Preserve aDoc(lDocCnt) aDoc(lDocCnt) = lDocumentID
====fill====
For i = pdf.FormFieldCount To 1 Step -1 lret = pdf.FlattenFormField(i) Next
lret = pdf.SelectDocument(aDoc(0)) lret = pdf.MergeDocument(aDoc(lDocCnt))
loop
lret = pdf.SaveToFile("funstuff") |
|
|
Posted By: gkell
Date Posted: 17 Jun 09 at 5:08PM
I figured out the problem. It was that I populate file(a) and flatten then open a new file(b) then populate and flatten it then merge at the end; however, it should be that I populate file(a) and flatten then open file(b) then merge then populate then flatten. for example:
pdf.LoadFromFile (gsWebsiteFilePath & "\PCPPlan.pdf")
lDocumentID = pdf.SelectedDocument FirstDocumentID ====fill====
For i = pdf.FormFieldCount To 1 Step -1 lret = pdf.FlattenFormField(i) Next
Do Until EOF
pdf.LoadFromFile (gsWebsiteFilePath & "\PCPPlan.pdf") lDocumentID = pdf.SelectedDocument
lret = pdf.SelectDocument(FirstDocumentID) lret = pdf.MergeDocument(lDocumentID) ====fill====
For i = pdf.FormFieldCount To 1 Step -1 lret = pdf.FlattenFormField(i) Next
loop
lret = pdf.SaveToFile("funstuff")
|
Posted By: gkell
Date Posted: 28 Oct 09 at 6:32PM
This issue continues to happen during flattenformfield function. Here is the code used to flatten:
For i = CLng(pdf1.FormFieldCount) To 1 Step -1 If CStr(pdf1.GetFormFieldValue(i)) <> "" Then lret = pdf1.FlattenFormField(i) Else lret = pdf1.DeleteFormField(i) End If Next
If I don't flatten data is fine, however, I have to flatten to merge all generated pdfs together.
This needs to be resolved. thanks.
|
Posted By: Ingo
Date Posted: 28 Oct 09 at 8:13PM
Hi!
I don't know why you're posting one problem in three threads? Did you read the kb-article from Rowan? The important thing is that after each flatten the number of the formfields will be reduced by one. So if you're using loops with formfieldcount you have to keep an eye on this fact ;-)
Cheers, Ingo
|
Posted By: gkell
Date Posted: 29 Oct 09 at 1:34AM
Yeah, sorry about posting in three spots, but they all seem to be the same issue. So, I thought I might have a better chance of getting the issue resolved. The Link that you gave me with the code for flattening is the same as the code I us. Plus, I did copy that code for the link, just in case, to my code, but the result was the same. Yes, I know if you flatten one out of 12, you will have 11. The problem is that when flattening the data gets mixed up some how, either the data is doubled to another spot that should be something else, or the data is just missing from the form field it self. If you need more data I can try to post some more code, images, or links to files tomorrow when working in the server. let me know if you come up with anything. Thanks and sorry again.
|
Posted By: Ingo
Date Posted: 29 Oct 09 at 6:36AM
Hi!
What about the long way? Load the files one by one, flatten the fields, save the files, load the files and merge them?
Cheers, Ingo
|
Posted By: gkell
Date Posted: 30 Oct 09 at 2:41AM
I've tried to do the long way and the short way. Both produce the same results. Here is my codeIf Not (oRs.BOF And oRs.EOF) Then lFndSrcID = oRs!FundSrcID lYear = oRs!Year lMonth = oRs!Month While Not oRs.EOF documentCount = documentCount + 1 Set pdf1 = CreateQuickPDF() 'Setup Temp folder If Not TempFolder.FolderExists(sFolder & "PDFTemp\") Then TempFolder.CreateFolder sFolder & "PDFTemp\" End If If Not CBool(pdf1.LoadFromFile(gsWebsiteFilePath & "\VRdoc.pdf")) Then CreateVRPDF = False Exit Function End If '======================================================================================== 'Fill in pdf sVendorNumber = NullToString(oRs!VendorNumber) lret = pdf1.SetFormFieldValueByTitle("VR Number", sVRNumber) '======================================================================================== For i = CLng(pdf1.FormFieldCount) To 1 Step -1 If CStr(pdf1.GetFormFieldValue(i)) <> "" Then lret = pdf1.FlattenFormField(i) Else lret = pdf1.DeleteFormField(i) End If Next lret = pdf1.CompressContent lret = pdf1.SaveToFile(sFolder & "PDFTemp\" & documentCount & "_temp.pdf") lret = pdf.AddToFileList("pdflist", sFolder & "PDFTemp\" & documentCount & "_temp.pdf") lret = pdf1.RemoveDocument(pdf1.SelectedDocument) Set pdf1 = Nothing oRs.MoveNext Wend End If oRs.Close Set oRs = Nothing
If documentCount <> 0 Then If documentCount <= 1 Then lret = pdf.SaveToFile(sFolder & lFndSrcID & "_" & lMonth & "_" & lYear & ".pdf") Else lret = pdf.MergeFileListFast("pdflist", sFolder & lFndSrcID & "_" & lMonth & "_" & lYear & ".pdf") lret = pdf.ClearFileList("pdflist") End If End If
|
Posted By: gkell
Date Posted: 30 Oct 09 at 2:45AM
As you might have noticed in the code, I also initalize and kill pdf1 each loop hoping that would fix the issue, but it didn't.
|
Posted By: Ingo
Date Posted: 30 Oct 09 at 6:06AM
... If you've already tried the latest (7.17) version now it's time to visit the official support-pages ;-)
Cheers, Ingo
|
Posted By: Shotgun Tom
Date Posted: 30 Oct 09 at 4:31PM
Gkell:
Why not create all your temp files, merge them and then do the form flattening to the single merged file?
I know that works as I've created a program that merges IRS Tax Form's and the flattening is done only on the resulting merge file.
Tom
|
Posted By: gkell
Date Posted: 30 Oct 09 at 6:40PM
I have tried that too, but when I merge the temp files all the data is the same as the first page. Maybe my merging method is incorrect. You see my code, is there something to add to it so that when I merge the temps that the data doesn't stay the same as the first page?
|
Posted By: Shotgun Tom
Date Posted: 30 Oct 09 at 6:59PM
Try adding the UpdateAppearanceStream directly after you set the data for each form field.
PDF1.UpdateAppearanceStream Index
Function QuickPDF0716.PDFLibrary::UpdateAppearanceStream(Index As Long) As Long
You are using the Active X version, correct?
|
Posted By: gkell
Date Posted: 30 Oct 09 at 7:07PM
Yes, Active X version 7.16. Tried UpdateAppearanceStream, but same outcome.
|
Posted By: Ingo
Date Posted: 30 Oct 09 at 7:56PM
Are the formfield names in different documents or pages the same? I've read that this can be a problem, too.
Cheers, Ingo
|
Posted By: gkell
Date Posted: 30 Oct 09 at 8:06PM
Basically it's one template file that I populate for each recordset retreived. So every page is the same but the form field data is different in each page (for the most part). So yes the form fields are named the same because it's just one template file used to generate the multi page pdf.
|
Posted By: Ingo
Date Posted: 30 Oct 09 at 8:22PM
Really... This can be the problem. Try to find few other (different) pdf-forms and merge them. There won't be a problem. I'm pretty sure.
Cheers, Ingo
|
Posted By: gkell
Date Posted: 30 Oct 09 at 8:38PM
Well, all the pages must be the same format...? Should I just copy that pdf and load it every other time or something? Not sure how to use different if they have to be the same.
|
Posted By: Ingo
Date Posted: 30 Oct 09 at 9:06PM
What i wanna suggest is try few forms which should have different fieldnames and merge them. You'll see it will work ;-)
|
Posted By: Shotgun Tom
Date Posted: 31 Oct 09 at 2:08PM
gKell: Ingo may be on to something.
Question: If you merge the temp documents without flattening the fields is the final merged PDF correct?
|
Posted By: Ingo
Date Posted: 31 Oct 09 at 2:14PM
test it and then you'll know it
|
Posted By: nhemdal
Date Posted: 12 Nov 09 at 11:03PM
I get the same behavior using a PDF template from the IRS. If I do not flatten the fields, the PDF is correct. If I flatten the fields the PDF fields are not correct. I am using this with VB6. This is how I flatten:
Public Sub FlattenFormFields(ByVal FileNamePs$) CtNik.WriteToDebugView "Entering PdfForm.FlattenFormFields..." CtNik.WriteToDebugView "Form field count = " & CStr(pdfLibM.FormFieldCount) Call pdfLibM.LoadFromFile(FileNamePs$) If (Len(fileNameMs$) < 1) Then Err.Raise ErrorPdfFileNameIsEmptyK&, "PdfForm.Save", "The PDF file name can not be empty." Dim currentFieldL% Do While pdfLibM.FormFieldCount > 0 currentFieldL% = pdfLibM.FormFieldCount If (pdfLibM.FlattenFormField(currentFieldL%) <> 1) Then Err.Raise ErrorCouldNotFlattenPdfFormFieldK&, "PdfForm.FlattenFormFields", "Unable to flatten form field with index " & CStr(currentFieldL%) End If Loop If (pdfLibM.SaveToFile(fileNameMs$) = 0) Then Err.Raise ErrorCouldNotSavePdfFileK&, "PdfForm.Save", "Encountered " & CStr(pdfLibM.LastErrorCode) & " saving PDF " & fileNameMs$ CtNik.WriteToDebugView "Form field count = " & CStr(pdfLibM.FormFieldCount) CtNik.WriteToDebugView "Leaving PdfForm.FlattenFormFields..." End Sub
|
Posted By: Shotgun Tom
Date Posted: 13 Nov 09 at 12:42AM
Hey Nhemdal:
I have no problem flattening an IRS form.
Remember, the behavior of the flatten command causes the field count and index to change. This will create problems in the code you posted. As I posted above and again below, you need to loop backwards through the fields when flattening. Here's that snippet of code:
Private Sub FlattenFields() If QP.LoadFromFile(OutPDFFileName) = 1 Then 'How many fields to fill? Dim FieldIndex As Integer
FieldCount = QP.FormFieldCount() For i = FieldCount To 1 Step -1 QP.FlattenFormField i DoEvents
Next
If QP.SaveToFile(OutPDFFileName) = 1 Then 'do nothing Else MsgBox "Error, file could not be written. - " & OutPDFFileName & vbCrLf Msg = Msg & "Field Flattening Failed!" & Time & vbCrLf End If
Else
MsgBox "Error, merged file not loaded.", vbCritical, "Flattening Error!"
End If End Sub
|
Posted By: nhemdal
Date Posted: 13 Nov 09 at 1:24AM
I am using quickpdf 7.16. The IRS form is the 4549, and it has two pages. I rename the fields, using SetFormFieldChildTitle, in case I have to append additional 4549's to the initial one. In my test case, there are no additional PDFs merged in; however, the fields are renamed. When I use FlattenFOrmFIeld I get spurious data in random form fields. All of the form fields get set with data. I initially was calling FlattenFormField after I set a value, but same behavior. I switched to calling after all data has been set - same error. I know that the calls to SetFormFieldValueByTitle contain the correct data and have verified/validated this since if saving without flattening the PDF is correct. If I flatten to print, or send to viewer, then I get incorrect values for fields (other than what was set - even though set calls trace with correct values). I tried the following code as suggested, and I still get the same issue. I initially tried the code specified below. I have removed all unnecessary form fields from the template PDFs.
Public Sub FlattenFormFields(ByVal FileNamePs$) If (Len(fileNameMs$) < 1) Then Err.Raise ErrorPdfFileNameIsEmptyK&, "PdfForm.Save", "The PDF file name can not be empty." Call pdfLibM.LoadFromFile(FileNamePs$) Dim I As Integer Dim FieldCount As Integer FieldCount = pdfLibM.FormFieldCount() For I = FieldCount To 1 Step -1 pdfLibM.FlattenFormField I DoEvents Next If (pdfLibM.SaveToFile(fileNameMs$) = 0) Then Err.Raise ErrorCouldNotSavePdfFileK&, "PdfForm.Save", "Encountered " & CStr(pdfLibM.LastErrorCode) & " saving PDF " & fileNameMs$ End Sub
|
Posted By: nhemdal
Date Posted: 13 Nov 09 at 1:44AM
Here is the screenshot of my issue. http://www.hemdal.com/Blogs/tabid/54/Default.aspx - http://www.hemdal.com/Blogs/tabid/54/Default.aspx The left-aligned K under the 700 should say 750. If not flattened it is correct. Running flattenformfield causes this.
|
Posted By: Shotgun Tom
Date Posted: 13 Nov 09 at 2:34AM
I'd be happy to take a look at it if you'd like. How are you populating the form fields?
You can email the pdf form to t.m.o.r.a.n4511 at h.o.t.m.a.i.l. dot com
The email address is purposefully disguised to avoid getting spam mail from scrapers. Remove the periods and use the correct syntax (the @ symbol, etc.) for sending email if you'd like to send it.
Tom
|
Posted By: nhemdal
Date Posted: 13 Nov 09 at 1:40PM
Given a template PDF - downloaded IRS form slightly customized using Foxit Phantom. I created a simple vb6 project with one button with code-behindas follows:
Option Explicit
'Private pdfLib As QuickPDFAX0716.PDFLibrary Private pdfLib As Object
Private Sub cmdFlatten_Click()
On Error GoTo TrapError
Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject")
If (pdfLib Is Nothing) Then Set pdfLib = CreateObject("QuickPDFAX0716.PDFLibrary") pdfLib.UnlockKey ("...") End If
Dim outputFullPath As String Dim testPdfCount As Integer testPdfCount = 3 Dim j As Integer For j = 1 To testPdfCount outputFullPath = "C:\footest" & CStr(j) & ".pdf" If (fso.FileExists(outputFullPath)) Then fso.DeleteFile (outputFullPath) fso.CopyFile "C:\foo.pdf", outputFullPath Call pdfLib.LoadFromFile(outputFullPath) Dim I As Integer Dim FieldCount As Integer FieldCount = pdfLib.FormFieldCount() For I = FieldCount To 1 Step -1 pdfLib.FlattenFormField I Next Call pdfLib.SaveToFile(outputFullPath) Next Cleanup: Exit Sub TrapError: MsgBox Err.Description Resume Cleanup
End Sub
This will still generate the error intermittently.
|
Posted By: nhemdal
Date Posted: 13 Nov 09 at 2:50PM
Upgrading to 7.17 did not help. It does not happen on the same form field all the time either.
|
Posted By: Shotgun Tom
Date Posted: 13 Nov 09 at 4:46PM
Hi Niklas:
I have tried, over a dozen times, flattening the foo.pdf file you sent me. Each time it correctly flattens the fields.
I reduced the flattening to the smallest amount of code possible.
Start a new project. Add the QuickPDF 7.17 reference library. Add a single command button. Paste the following code into your project:
'-----------------------------------------------------------------
Option Explicit Dim QP Dim i As Integer Private ret As Long
Private Sub Command1_Click() Dim PDFFileName As String Dim SaveFileName As String Dim FieldCount As Integer PDFFileName = ' put your source file here "C:\Documents\foo.pdf" SaveFileName = 'put your destination save file pathname here "C:\Documents\footomflat.pdf" If QP.LoadFromFile(PDFFileName) = 1 Then 'How many fields to fill?
FieldCount = QP.FormFieldCount()
MsgBox FieldCount 'should be 192 for your form For i = FieldCount To 1 Step -1 QP.FlattenFormField i
Next
If QP.SaveToFile(SaveFileName) = 1 Then MsgBox "saved - " & SaveFileName & vbCrLf Else MsgBox "Error, file could not be written. - " & PDFFileName & vbCrLf End If Else MsgBox "Error, file not loaded." End If End Sub
Private Sub Form_Load() ClassName = "QuickPDFAX0717.PDFLibrary" LicenseKey = "xxxxxxxxxxxxxxxxx" 'SET YOUR LICENSE KEY HERE... Set QP = CreateObject(ClassName) ret = QP.UnlockKey(LicenseKey) MsgBox ret
End Sub
'-------------------------------------
It works correctly every time I've run on my system. If this project works correctly on yours then there has to be some other code in your project that is causing the problem.
Tom
|
Posted By: nhemdal
Date Posted: 13 Nov 09 at 6:38PM
Tom,
I still get the same behavior with the code snippet that you supplied.
VB6 SP6
XP SP2
QuickPDF 7.17 - standard license valid to version 7.99
Info reported by sysinternals ProcExp when running compiled simple vb6 test project:
QuickPDFAX0717.dll v 7.17.1029.0209
VB VM v. 6.00.0098.0002
typical mumbo jumbo ms support files
Foxit Phantom 1.0 flattens the pages correctly on my system.
-noh
|
Posted By: nhemdal
Date Posted: 13 Nov 09 at 7:01PM
Ok, I went to my native tongue C#. In addition, I included another PDF component vendor that I use in another project to make sure that I am not losing it. QuickPdf flattened file is incorrect, but other vendor's flattened version is correct. Not only is it correct, but it is 10 times smaller in size. Now, other vendor is native .NET, but in this case I need COM. The conundrum.
static void Main(string[] args)
{
string sourceFileName = @"C:\foo.pdf";
string destFileName = @"C:\flatfoo.pdf";
string otherVendorDestFileName = @"C:\otherVendorFlatFoo.pdf";
string licenseKey = "...";
QuickPDFAX0717.PDFLibrary qp = new QuickPDFAX0717.PDFLibrary();
if ( 1 == qp.UnlockKey(licenseKey) )
{
qp.LoadFromFile(sourceFileName);
int fieldCount = qp.FormFieldCount();
for (int i = fieldCount; i >= 1; i--)
{
qp.FlattenFormField(i);
}
if (File.Exists(destFileName)) { File.Delete(destFileName); }
qp.SaveToFile(destFileName);
}
OtherVendor.PDFDocument doc = new OtherVendor.PDFDocument(sourceFileName);
if (null != doc)
{
doc.FlattenFormFields ();
doc.SaveToFile (otherVendorDestFileName);
}
}
|
Posted By: nhemdal
Date Posted: 13 Nov 09 at 8:41PM
Ok, so I opened a generic Windows XP SP3 VM on my Mac to rule out some wierd config on the Wintel box I was using in previous posts. My QA group also gets this intermittent error, so it's not some wierd config issue I think. Other vendors and software work on same HW. No Adobe Reader or Foxit Reader is installed on the VM. I have VB6 SP6 on it, and I installed quickpdf 7.17 and GDViewer OCX for viewing. I created the simple VB6 projects specified in posts above, and same issue out of the gates. I'm at a loss. Product losing credibility in my book - also have yet to hear response from support.
|
Posted By: Shotgun Tom
Date Posted: 14 Nov 09 at 3:14AM
Hi Niklas:
I know this is frustrating for you. If you submitted the problem to tech support they will get back with you.
I've tried a few other things and still cannot duplicate the error you're getting. I have viewed the results in Acrobat 9 Pro, PDF XPress Viewer, Adobe Reader 8, FoxIt and a GDPicture ocx viewer I created. The results from all my test files and your error file display the same in each viewer.
However, I did note one curious thing... file size. Just for kicks I also flattened your original foo.pdf using Acrobat 9 Pro and GDPicture. The file size on each is quite different. The really strange thing is the file size of your flattened file.
Your original foo.pdf, unflattened, is 98302.
The file size results of flattening in each of the following:
Acrobat 9 Pro: 187352
GDPicture: 245658
MyFlatRoutine: 248762 (with QuickPDF)
YourFlatfoopdf: 93976
Your flattened file size is smaller than the original. How can that be? Even Acrobat, which has the smallest size is double that of the original.
I now have tried My routine and file viewing on three computers... one with XP/SP3, one on my neighbor's Vista and on a brand new Windows 7 box. All results are the same.
Something strange is happening. It drives me nuts that I can't duplicate your error.
I sent you an image of the file size comparisons to your email.
Tom
|
Posted By: gkell
Date Posted: 17 Nov 09 at 8:13PM
Actually found a fix to this issue. Basically, I decided, since flattening was the issue, to not flatten at all and instead to rename the child title to the document count and form field index. Example:
For i = pdf.FormFieldCount To 1 Step -1 lret = pdf.SetFormFieldChildTitle(i, pdf.GetFormFieldTitle(i) & documentCount & i) 'If CStr(pdf.GetFormFieldValue(i)) <> "" Then ' lret = pdf.FlattenFormField(i) 'Else ' lret = pdf.DeleteFormField(i) 'End If Next
And if you need the form to not be edited then you should use SetFormFieldReadOnly function also.
|
|