Not Getting Changes to Write to File

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

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Sean - Tracker, Chris - Tracker Supp, 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
ptoon@einformatics.com
User
Posts: 6
Joined: Fri Mar 01, 2019 7:29 pm

Not Getting Changes to Write to File

Post by ptoon@einformatics.com » Fri Mar 01, 2019 7:50 pm

Hello,

I'm a first time poster and am brand new to your SDK, so I'm sorry if I'm doing something wrong here, but I'm stuck, and my code seems similar to other sets of code I've found on these forums, and I'm stumped as to why this isn't working. I'm hoping you can help me figure it out.

I'm capturing a signature from a device using that devices SDK, and I'm just trying to open the PDF, find a field that is basically a placeholder for the signature, append the image of the signature, delete the original field, and then write the new PDF out to a new file which I'll save back to my application's directory.

Here is the important part of my code:

Code: Select all

List<string> signatures = new List<string>();
                    doc = inst.OpenDocumentFromFile(localPath + "EISIGN_" + row.FileName,null,null,0,0);
                    uint pages = doc.Pages.Count;
                    int rotation = doc.Pages[0].Rotation;
                    if (doc.HasAcroForm)
                    {
                        List<IPXC_FormField> fields = new List<IPXC_FormField>();
                        fields.Add(doc.AcroForm.GetFieldByName("Signature1"));
                        foreach (IPXC_FormField field in fields)
                        {
                            if (field.FullName == "Signature1")
                            {
                                //Generate signature data/Capture Signature
                                DynamicCapture sign = new DynamicCapture();
                                SigCtl signature = new SigCtl();
                                signature.InputWho = row.PatName;
                                signature.InputWhy = row.ChartDisplayName;
                                signature.Licence = "XXXXXX";
                                DynamicCaptureResult result = new DynamicCaptureResult();
                                //Check signature capture result
                                result = sign.Capture(signature);
                                if (result == DynamicCaptureResult.DynCaptOK)
                                {
                                    SigObj sigObj = new SigObj();
                                    sigObj = (SigObj)signature.Signature;
                                    PXC_Rect rect = field.Widget[0].get_Rect();
                                    sigObj.RenderBitmap(localPath + "sig.png", (int)rect.left*2, (int)rect.bottom*2, "image/png", 1, 0x000000, 0xffffff, 10.0f, 10.0f, RBFlags.RenderOutputFilename | RBFlags.RenderColor32BPP | RBFlags.RenderEncodeData | RBFlags.RenderBackgroundTransparent);
                                    try
                                    {
                                        IPXC_Annotation annot = field.Widget[0];
                                        IPXC_Image image = doc.AddImageFromFile(localPath + "sig.png", 0 /*annot.Page.Number*/);
                                        PXC_Matrix matrix = new PXC_Matrix();
                                        matrix.a = rect.right - rect.left;
                                        matrix.d = rect.bottom - rect.top;
                                        matrix.e = rect.left;
                                        matrix.f = rect.bottom;
                                        IPXC_ContentCreator cc = doc.CreateContentCreator();
                                        cc.ConcatCS(matrix);
                                        cc.PlaceImage(image);
                                        
                                        doc.AcroForm.DeleteField(field.FullName);
                                    }
                                    catch (Exception z)
                                    {
                                        File.AppendAllText(logPath, "\n" + aUX_Inst.FormatHRESULT(z.HResult));
                                    }
                                    File.Delete(localPath + "sig.png");
                                    signatures.Add(signature.InputSignature);
                                }
                                else if (result == DynamicCaptureResult.DynCaptCancel)
                                {
                                    documentViewer.Navigate(localPath + "EISIGN_" + row.FileName);
                                }
                                else if (result == DynamicCaptureResult.DynCaptPadError)
                                {
                                    documentViewer.Navigate(localPath + "EISIGN_" + row.FileName);
                                    MessageBox.Show("WACOM Device Not Found!", "Error", MessageBoxButtons.OK);
                                }
                                else if (result == DynamicCaptureResult.DynCaptNotLicensed)
                                {
                                    documentViewer.Navigate(localPath + "EISIGN_" + row.FileName);
                                    MessageBox.Show("WACOM Interface Not Licensed!", "Error", MessageBoxButtons.OK);
                                }
                                else
                                {
                                    documentViewer.Navigate(localPath + "EISIGN_" + row.FileName);
                                    MessageBox.Show("There Was An Error!", "Error", MessageBoxButtons.OK);
                                }
                            }
                            //COMMIT CHANGES TO FILE IN LOCAL CACHE
                            doc.WriteToFile(localPath + "sigDoc" + row.FileName);
                            doc.Close();
There is a mix of SDKs being used here, but the important parts for your SDK seem to be there from the best I can tell. My new file is being written, and it has the watermarks on it, since I'm not using a license key currently, but the "Signature1" field hasn't been deleted, and the signature from "sig.png" isn't being appended to the PDF.

User avatar
Tracker Supp-Stefan
Site Admin
Posts: 13428
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Not Getting Changes to Write to File

Post by Tracker Supp-Stefan » Thu Mar 07, 2019 3:20 pm

Hello ptoon,

I've asked a colleague from the dev team to take a look here and advise!
We will post further instructions here shortly!

Regards,
Stefan

ptoon@einformatics.com
User
Posts: 6
Joined: Fri Mar 01, 2019 7:29 pm

Re: Not Getting Changes to Write to File

Post by ptoon@einformatics.com » Thu Mar 07, 2019 4:44 pm

Thank you for the reply. I've actually figured out why the image wasn't being added to the document. I'll post the missing line of code here in case anyone else comes across the same issue:

Code: Select all

doc.Pages[annot[l].Page.Number].PlaceContent(cc.Detach());
I was missing the PlaceContent method call.

The issue with not being able to delete the field still remains. I'll post my updated code here since it's changed some since the original post was made:

Code: Select all

List<string> signatures = new List<string>();
                    doc = inst.OpenDocumentFromFile(localPath + "EISIGN_" + row.FileName,null,null,0,0);
                    uint pages = doc.Pages.Count;
                    int rotation = doc.Pages[0].Rotation;
                    if (doc.HasAcroForm)
                    {
                        for (uint i = 0; i < doc.Pages.Count; i++)
                        {
                            var annot = doc.Pages[i].GetAnnotsList();
                            for (uint l = 0;  l < annot.Count;  l++)
                            {
                                if (annot[l].Field?.FullName == "Signature1")
                                {
                                    var field = annot[l].Field;
                                    //Generate signature data/Capture Signature
                                    DynamicCapture sign = new DynamicCapture();
                                    SigCtl signature = new SigCtl();
                                    signature.InputWho = row.PatName;
                                    signature.InputWhy = row.ChartDisplayName;
                                    signature.Licence = "AgAfADeB5ivVARNXYWNvbSBTaWduYXR1cmUgU0RLAAECgQMBAmQA";
                                    DynamicCaptureResult result = new DynamicCaptureResult();
                                    //Check signature capture result
                                    result = sign.Capture(signature);
                                    if (result == DynamicCaptureResult.DynCaptOK)
                                    {
                                        SigObj sigObj = new SigObj();
                                        sigObj = (SigObj)signature.Signature;
                                        PXC_Rect rect = field.Widget[0].get_Rect();
                                        sigObj.RenderBitmap(localPath + "sig.png", (int)Math.Abs((rect.right - rect.left)) * 2, (int)Math.Abs((rect.top - rect.bottom)) * 2, "image/png", 1.5f, 0x000000, 0xffffff, 1.0f, 1.0f, RBFlags.RenderOutputFilename | RBFlags.RenderColor32BPP | RBFlags.RenderEncodeData | RBFlags.RenderBackgroundTransparent);
                                        try
                                        {
                                            IPXC_Image image = doc.AddImageFromFile(localPath + "sig.png", annot[l].Page.Number);
                                            PXC_Matrix matrix = new PXC_Matrix();
                                            matrix.a = Math.Abs(rect.right - rect.left);
                                            matrix.d = Math.Abs(rect.bottom - rect.top);
                                            matrix.e = rect.left;
                                            matrix.f = Math.Min(rect.top, rect.bottom);
                                            IPXC_ContentCreator cc = doc.CreateContentCreator();
                                            cc.ConcatCS(matrix);
                                            cc.PlaceImage(image);
                                            doc.Pages[0].PlaceContent(cc.Detach());
                                            doc.AcroForm.FlattenField(field.FullName);
                                            //doc.AcroForm.DeleteField(field.FullName);
                                        }
                                        catch (Exception z)
                                        {
                                            File.AppendAllText(logPath, "\n" + aUX_Inst.FormatHRESULT(z.HResult));
                                        }
                                        File.Delete(localPath + "sig.png");
                                        signatures.Add(signature.InputSignature);
                                    }
                                    else if (result == DynamicCaptureResult.DynCaptCancel)
                                    {
                                        documentViewer.Navigate(localPath + "EISIGN_" + row.FileName);
                                    }
                                    else if (result == DynamicCaptureResult.DynCaptPadError)
                                    {
                                        documentViewer.Navigate(localPath + "EISIGN_" + row.FileName);
                                        MessageBox.Show(" Device Not Found!", "Error", MessageBoxButtons.OK);
                                    }
                                    else if (result == DynamicCaptureResult.DynCaptNotLicensed)
                                    {
                                        documentViewer.Navigate(localPath + "EISIGN_" + row.FileName);
                                        MessageBox.Show("Interface Not Licensed!", "Error", MessageBoxButtons.OK);
                                    }
                                    else
                                    {
                                        documentViewer.Navigate(localPath + "EISIGN_" + row.FileName);
                                        MessageBox.Show("There Was An Error!", "Error", MessageBoxButtons.OK);
                                    }
                                }
                            }
                        }
                        //COMMIT CHANGES TO FILE IN LOCAL CACHE
                        doc.WriteToFile(localPath + "sigDoc" + row.FileName);
                        doc.Close();
After this code runs, the "Signature1" field still remains on the form, and as you can see from the commented out section of code, I've tried both FlattenField and DeleteField.

Ivan - Tracker Software
Site Admin
Posts: 3607
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: Not Getting Changes to Write to File

Post by Ivan - Tracker Software » Fri Mar 08, 2019 11:06 pm

Can you send us a sample document where the problem is reproducible?
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.

ptoon@einformatics.com
User
Posts: 6
Joined: Fri Mar 01, 2019 7:29 pm

Re: Not Getting Changes to Write to File

Post by ptoon@einformatics.com » Mon Mar 11, 2019 11:43 am

I've attached the form I've been using to test with in a 7zip file.
Admission Form English.7z
Example PDF
(155.14 KiB) Downloaded 33 times

Ivan - Tracker Software
Site Admin
Posts: 3607
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: Not Getting Changes to Write to File

Post by Ivan - Tracker Software » Mon Mar 11, 2019 9:06 pm

Thanks for the file.
I found where the issue is, and made a fix which will be available in the next build.

As a workaround, for now, you can add

Code: Select all

int nIndex = field.Widget[0].get_IndexOnPage();
as the problem was with the late resolving page for widgets, and, as a result, the field was not properly removed.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.

ptoon@einformatics.com
User
Posts: 6
Joined: Fri Mar 01, 2019 7:29 pm

Re: Not Getting Changes to Write to File

Post by ptoon@einformatics.com » Tue Mar 12, 2019 9:02 pm

Can you enlighten me as to where exactly this line goes in the code I've provided? I've tried figuring out where it would be inserted and so far I'm coming up blank.

The FlattenField and DeleteField both accept a string which I'm assuming is the name of the field to delete/flatten. I can't find a similar method that accepts an int or an index of some sort.

I tried a round about way of using "MoveAnnot" to move the annotation to a page that didn't exist, but that didn't appear to do anything at all.

I also tried using the name of the annotation instead of the name of the field, but that didn't work either.

ptoon@einformatics.com
User
Posts: 6
Joined: Fri Mar 01, 2019 7:29 pm

Re: Not Getting Changes to Write to File

Post by ptoon@einformatics.com » Tue Apr 09, 2019 5:27 pm

Hello, I was hoping I could get an update on my question above? I've still been unable to figure out where in the code I posted the line you referenced would go.

Ivan - Tracker Software
Site Admin
Posts: 3607
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: Not Getting Changes to Write to File

Post by Ivan - Tracker Software » Tue Apr 09, 2019 11:22 pm

Sorry, I missed your post.

You can insert that line before this line in your original code:

Code: Select all

PXC_Rect rect = field.Widget[0].get_Rect();
Or, you can use the current version 8.0.330.0 where the issue is fixed and your code should work as expected without my line.
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.

ptoon@einformatics.com
User
Posts: 6
Joined: Fri Mar 01, 2019 7:29 pm

Re: Not Getting Changes to Write to File

Post by ptoon@einformatics.com » Mon Apr 22, 2019 11:29 am

I was able to update and use the DeleteField method to successfully remove the target field from the form. I've run into another issue though and I'm not sure if you'd like me to start a new thread or just post it in this one since it's somewhat related, but while the field is getting deleted from the form, it seems to be orphaning an empty annotation on the form. After saving the PDF, reopening it, and scanning it for a specific field again, an exception will be thrown when iterating through all the annotations on the form because the annotation which housed the field I just deleted will now have a null reference when looking for it's field. I can't seem to find a way to get the annotation to be deleted as well. I've tried the Remove method on an annotation list, but after executing that and saving the PDF, the annotation remains on the form.

I was able to work around this easily enough by just testing to see if the field within the annotation is null or not while iterating through the form, and skipping that annotation if the field is null, but I'm just wondering if I'm missing the needed method for removing the annotation from the PDF or not, because while I now know to check for the null field when iterating through, someone who handles the PDF down stream may not.

Post Reply