C# code to fill a PDF form

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, 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.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to fill a PDF form

Post by Sasha - Tracker Dev Team »

Hello Nisha,

Chrome also opens it as Acrobat does. We have recreated the behavior that you mentioned - that's the Edge problem - please write to it's support.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Nisha
User
Posts: 43
Joined: Tue May 22, 2018 9:31 am

Re: C# code to fill a PDF form

Post by Nisha »

Hi,

Thanks for your support.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: C# code to fill a PDF form

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
SMan
User
Posts: 23
Joined: Tue May 15, 2018 10:18 am

Re: C# code to fill a PDF form

Post by SMan »

Sasha - Tracker Dev Team wrote: Thu Jun 07, 2018 7:34 am I've made a sample and indeed, in the case of the non-UI usage (there is no IPXV_Document) the logic is somewhat different and does not work as it is intended to.
For now, you can use this as a workaround - in your case this should be the same:

Code: Select all

ff.SetValueText("New Text");
Hello all,

I currently have a need to programatically fill in a PDF form which contains both text fields and checkboxes (programming in Visual Baisc). This should ideally happen without having to load the document in the UI.

Updating the text fields with the method cited above as in Alex's sample from here:
Sasha - Tracker Dev Team wrote: Thu Jun 07, 2018 8:58 am Here's my sample project that works:
FieldsModifySample_Simplified.zip
works perfectly.

However, attemptling to check or uncheck the checkboxes is giving me grief. The checkboxes in the PDF form are neither locked nor write protected, checking them manually through the UI in the PDF form when loaded in the Editor works fine, and their export value is "On". Still, using ff.SetValueText("On") or ff.SetValueText("Off") on the checkboxes has no effect, i.e. does not change their values and does not result in a previously unchecked textbox being checked in the saved result, or vice versa. What am I doing wrong?

Here is a simplified code snippet stripped of user-specific data:

Code: Select all

Public Sub FillPdfTemplateAndSave()

        Dim strInFile, strOutFile As String
        Dim m_Inst As IPXV_Inst = New PXV_Inst()
        m_Inst.Init()

        Dim dicFormTextValues As Dictionary(Of String, String) = New Dictionary(Of String, String)
        dicFormTextValues.Add("#text1", "12345/67")
        dicFormTextValues.Add("#text2", "Foo")
        dicFormTextValues.Add("#text3", "Bar")

        Dim dicFormCheckValues As Dictionary(Of String, Boolean) = New Dictionary(Of String, Boolean)
        dicFormCheckValues.Add("#check1", True)
        dicFormCheckValues.Add("#check2", False)
        dicFormCheckValues.Add("#check3", True)

        Try
            strInFile = Path.Combine("C:\temp", "my_pdf_form.pdf")
            strOutFile = Path.Combine("C:\temp", "my_filled_out_pdf.pdf")

            Dim strDummy As String

            Dim pxcInst As IPXC_Inst = CType(m_Inst.GetExtension("PXC"), IPXC_Inst)
            Dim Doc As IPXC_Document = pxcInst.OpenDocumentFromFile(strInFile, Nothing)
            Dim ff As PDFXEdit.IPXC_FormField
            For Each kvp As KeyValuePair(Of String, String) In dicFormTextValues
                ff = Doc.AcroForm.GetFieldByName(kvp.Key)
                ff.SetValueText(kvp.Value)   '' // this works just fine!

                strDummy = ff.GetValueText()
            Next

            For Each kvp As KeyValuePair(Of String, Boolean) In dicFormCheckValues
                ff = Doc.AcroForm.GetFieldByName(kvp.Key)

                strDummy = ff.GetValueText()  '' // check the status quo ante

                If kvp.Value Then
                    ff.SetValueText("On")
                Else
                    ff.SetValueText("Off")
                End If

                strDummy = ff.GetValueText()  '' // still identical to the status quo ante in all cases, regardless of kvp.Value!
            Next
            If File.Exists(strOutFile) Then
                File.Delete(strOutFile)
            End If
            Doc.WriteToFile(strOutFile)
            Doc.Close()

        Catch ex As Exception
            myLogger.Write2Log(ex)
        End Try
    End Sub
Here is the same code converted to C#:

Code: Select all

public void FillPdfTemplateAndSave()
{
    string strInFile, strOutFile;
    IPXV_Inst m_Inst = new PXV_Inst();
    m_Inst.Init();

    Dictionary<string, string> dicFormTextValues = new Dictionary<string, string>();
    dicFormTextValues.Add("#text1", "12345/67");
    dicFormTextValues.Add("#text2", "Foo");
    dicFormTextValues.Add("#text3", "Bar");

    Dictionary<string, bool> dicFormCheckValues = new Dictionary<string, bool>();
    dicFormCheckValues.Add("#check1", true);
    dicFormCheckValues.Add("#check2", false);
    dicFormCheckValues.Add("#check3", true);

    try
    {
        strInFile = Path.Combine(@"C:\temp", "my_pdf_form.pdf");
        strOutFile = Path.Combine(@"C:\temp", "my_filled_out_pdf.pdf");

        string strDummy;

        IPXC_Inst pxcInst = (IPXC_Inst)m_Inst.GetExtension("PXC");
        IPXC_Document Doc = pxcInst.OpenDocumentFromFile(strInFile, null);
        PDFXEdit.IPXC_FormField ff;
        foreach (KeyValuePair<string, string> kvp in dicFormTextValues)
        {
            ff = Doc.AcroForm.GetFieldByName(kvp.Key);
            ff.SetValueText(kvp.Value);   // this works just fine!

            strDummy = ff.GetValueText();
        }

        foreach (KeyValuePair<string, bool> kvp in dicFormCheckValues)
        {
            ff = Doc.AcroForm.GetFieldByName(kvp.Key);

            strDummy = ff.GetValueText();  // check the status quo ante

            if (kvp.Value)
                ff.SetValueText("On");
            else
                ff.SetValueText("Off");

            strDummy = ff.GetValueText();  // still identical to the status quo ante in all cases, regardless of kvp.Value!
        }
        if (File.Exists(strOutFile))
            File.Delete(strOutFile);
        Doc.WriteToFile(strOutFile);
        Doc.Close();
    }
    catch (Exception ex)
    {
        myLogger.Write2Log(ex);
    }
}
The "strDummy" variable is used only for debugging purposes so that I can make sure I have correctly addressed the various form fields and that I can inspect the values of the text fields and checkboxes before and after modifying, or attempting to modify, their values when I debug the program step by step.

Also, a Happy New Year to everyone! We can all use it...

Best regards,
Sven
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3549
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: C# code to fill a PDF form

Post by Ivan - Tracker Software »

For radio buttons and check boxes you have to use CheckWidget method instead of SetValueText.

For checkboxes, the first parameter should be 0. For the radio buttons - the index of the radio button on the group.
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.
SMan
User
Posts: 23
Joined: Tue May 15, 2018 10:18 am

Re: C# code to fill a PDF form

Post by SMan »

Hello Ivan,

thank you, this does the trick.

Unfortunately, I've run into another issue. While the PDF form is filled out correctly, the software from which the Editor SDK is called crashes within seconds afterwards with Code -1073740791 (0xc0000409).

I've narrowed down the trigger to the call of Doc.AcroForm.GetFieldByName(field_name). If I comment out that line and all subsequent ones that depend on it, the program doesn't crash (but of course doesn't fill out the form either). If I restore just that one line of code, the program crashes (even without the code making the slightest change to the content of the form).

The program (a Windows Forms application) contains an instance of the PDF Xchange Editor, but the PDF form that I want to fill out shouldn't be loaded in the UI. In comparison to my code snippet above, I have replaced the creation of a New PXV_Inst() object with a reference to the Inst of the Editor in my application's main window, which however makes no difference.

The version of the SDK that we're using (version 8.0.340.0) is not the latest one, might the issue be fixed in a newer one? If yes, can I download it anywhere?

Thanks for any assistance.
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8439
Joined: Wed Jan 03, 2018 6:52 pm

Re: C# code to fill a PDF form

Post by TrackerSupp-Daniel »

Hello, SMan

You can certainly try this in the latest release:
https://www.pdf-xchange.com/PDFXEd ... leSDK9.zip
If that package does not resolve the issue, please let us know, and our Dev team will get back to you for some further analysis.

Kind regards,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
SMan
User
Posts: 23
Joined: Tue May 15, 2018 10:18 am

Re: C# code to fill a PDF form

Post by SMan »

Hi Daniel,

Thank you for your reply. Apologies for taking so long to respond, we've been busy with other features of our software.

The product that we had purchased and that we use is the full PDF-XChange Editor SDK, not the Simple SDK. Is there any way to download or otherwise receive the last released version of that? Unfortunately there is no download link on the website anymore, at least not at https://www.pdf-xchange.com/product/downloads/discontinued .

Best regards,
Sven (SMan)
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8439
Joined: Wed Jan 03, 2018 6:52 pm

Re: C# code to fill a PDF form

Post by TrackerSupp-Daniel »

Hello, SMan

You can always download the correct version for use with your license key from your account page directly (signing into your account will also allow the discontinued product pages to show any builds available for download that you have a license covering).

Kind regards,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
Post Reply