Combined PDF loses Format Value scripts on open

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Combined PDF loses Format Value scripts on open

Post by GeoffW »

I've been using Doc.Pages.InsertPagesFromDoc to combine PDF documents. When I merge two PDFs that contain fields with javascript, everything seems to go okay but when I open resulting PDF in PDF-XChange Editor (v7 327.0) the Format Value custom action scripts seem to have disappeared - the field property window shows the Format Value category has gone back to None.. I would put this down to some sort of conflict between the two PDF forms being combined, except the same PDF document opens and works okay Adobe Acrobat DC, and even in an old version (v2.5) of PDF-XChange viewer. (You can tell the scripts must be there and working in these other programs because the tax-file-number gets formatted from "123123123" to "123 123 123".) I am setting the NeedAppearances flag in the AcroFields dictionary when I save the combined PDF, because I am also filling some of these fields.

So, any idea where I start trying work this one out?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Combined PDF loses Format Value scripts on open

Post by Sasha - Tracker Dev Team »

Hello GeoffW,

Two input documents, combined file and a small sample project to recreate this would be perfect.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Re: Combined PDF loses Format Value scripts on open

Post by GeoffW »

I may have narrowed it down...

The fields in question use javascript to re-format tax-file-numbers from "123456789" to "123 456 789" - for display, the exported value remains the same. For this reason I have been setting the NeedAppearances flag in the acrofields dictionary. What I've discovered is that if I do not do that then the javascript for these fields all stays in place. In this case the fields don't display properly until the user focuses on the fields and moves away - and there by runs the javascript to change the display - but at least the scripts remain functional.

So let's try changing my question to: I only have the Core API, and this was the only way I found to set the NeedAppearances flag (FiDoc is an IPXC_Document):

Code: Select all

function TPdfXEnh.SetNeedAppearances: boolean;
var
  cdoc: IPXS_Document;
  root, acro: IPXS_PDFVariant;
begin
  Result := false;
  CheckNotEmpty;
  if not FiDoc.HasAcroForm then
    Exit;
  cdoc := FiDoc.CosDocument;
  if not Assigned(cdoc) then
    Exit;
  cdoc.LockDocument(false);
  try
    CheckResult(cdoc.Get_Root(root), 'Failed to get CosDocument.Root');
    if not Assigned(root) then
      Exit;
    CheckResult(root.Dict_Get('AcroForm', acro), 'Failed to get AcroForm dictionary');
    if not Assigned(acro) then
      Exit;
    acro.Dict_SetBool('NeedAppearances', true);
    Result := true;
  finally
    cdoc.UnlockDocument;
    cdoc := nil;
  end;
end;
Is this wrong? Is there something else I need to be doing?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Combined PDF loses Format Value scripts on open

Post by Sasha - Tracker Dev Team »

Hello GeoffW,

That is a correct way of setting this flag, before writing the document, only you need to LockExclusive (for write) not Lock (for read).

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Re: Combined PDF loses Format Value scripts on open

Post by GeoffW »

Thanks for the correction.

Next stage in isolation I have a simplified PDF and used the same document twice in my merge. When I import field data into the merged file and write to the field AND set NeedAppearances the Format Value javascript disappears on open in PDF-XChange v7 (not Adobe or PDF-XChange v2.5) - I've attached an example problem PDF - see the Person-TFN field. If I only write to the field OR set NeedAppearances the problem does not happen.

So we've talked about the NeedAppearances flag, are there any tricks required for setting the field value? At the moment I simply loop through the AcroForm fields (IPXC_FormField references), and when I get a name match to what I'm importing I call field.SetValueText(mytextvalue). Is there anything else I should be doing? When I searched the examples for SetValueText they all seem to be part of creating fields, I'm just trying to update existing fields on a PDF form that I've opened. Can you point me to any other examples that might help?
Attachments
_example.pdf
(30.63 KiB) Downloaded 144 times
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Re: Combined PDF loses Format Value scripts on open

Post by GeoffW »

Okay, here is a sample project that reproduces the problem pdf seen in my prior post. That PDF

* In Adobe Acrobat and PDF-XChange Viewer v2.5, will open and keep its javascript associated with Format Value on field "Person-TFN". This javascript makes sure the input of "123456789" is displayed as "123 456 789" when the file is opened.

* In PDF-XChange Editor v7 327.0 (and 326.0), the javascript seems to get lost and no reformatting of the displayed value occurs.

The attached sample Delphi project shows how that PDF was created. (I just noticed the embedded readme file was not quite complete, but I think the code is fairly self explanatory.)
Attachments
PdfCoreApiTest.zip
(142.33 KiB) Downloaded 132 times
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Combined PDF loses Format Value scripts on open

Post by Sasha - Tracker Dev Team »

Hello GeoffW,

I've reproduced this on C# and redirected this to the appropriate developer who can look at this. The thing is that the Core level does not execute scripts at all - if you want use the scripts correctly, then the Editor SDK should be used for correct update. What we want to see, is why the execution of those 3 steps behave like that - it's not normal.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Re: Combined PDF loses Format Value scripts on open

Post by GeoffW »

Not sure whether to be happy or sad that it wasn't just me doing something wrong :wink: But very pleased you've been able to reproduce the problem.

Yes, I do understand that Core won't actually run the javascript - but if the javascript stayed where it was supposed to be, and the NeedAppearances flag is obeyed, then everything seems to come out okay when the file is opened in a reader. (It will mean that the results of the core FlattenAllFields function won't look right in such instances.)

In this particular use-case I am merging multiple PDFs, pre-filling what fields I can from a database and then saving the result to a temporary file which is opened with the associated PDF reader for user to: complete/print/email or save to permanent location as desired (much easier to use existing products for those last user-directed steps than trying to reimplement all the user interface aspects required).
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Combined PDF loses Format Value scripts on open

Post by Sasha - Tracker Dev Team »

Hello GeoffW,

Just had an idea and tried it. The problem is that you are inserting the pages, that have the fields with same names. This causes this behavior, as these are different fields and should be named differently. When inserting new pages, you should rename the fields first if there are any duplicates.
I've tried this with your files - renamed the Person-TFN field in the docb file - and everything worked correctly.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Re: Combined PDF loses Format Value scripts on open

Post by GeoffW »

Hi, yes I assumed it was very likely related to the name overlap, but I will make two observations:

* I would have expected at least one of them should stay unbroken.

* The output files work in (current) Adobe and old PDF-XChange Viewer v2.5.

Of course it is up to Tracker Software whether these observations matter.

P.S. I thought I had tried renaming one of the duplicated fields and still lost the javascript ... I will try again and double-check (the real files obviously have more fields etc. than the sample - it might be that I only renamed one out of three, and so still lost the javascript).
GeoffW
User
Posts: 19
Joined: Mon Jul 23, 2018 10:01 am

Re: Combined PDF loses Format Value scripts on open

Post by GeoffW »

I just tried the field renaming thing again. I am attaching what I used as the second file of the merge (copy of the first but with Person-TFN renamed to Person-TFN-1 and the extra field/boxes deleted so it is visually obvious it is different). I also altered the demo project code so it would find and fill both fields (not that it actually matters in this case).

In my test the resulting combined PDF had a fully functional first field (Person-TFN still had all its javascript), but a broken second field (Person-TFN-1 had the javascript missing from the Format Value properties).
Attachments
Sample01c.pdf
(28.35 KiB) Downloaded 125 times
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Combined PDF loses Format Value scripts on open

Post by Sasha - Tracker Dev Team »

Hello GeoffW,

I've forwarded that to the appropriate developer - we'll let you know when we will have some results.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply