Print Page | Close Window

MergeDocument cause some fields not to work

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: General Discussion
Forum Description: Discussion board for Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=3891
Printed Date: 19 May 24 at 12:42PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: MergeDocument cause some fields not to work
Posted By: fletchsod
Subject: MergeDocument cause some fields not to work
Date Posted: 09 Feb 21 at 6:04PM
Here's one issue I'm looking at and wonder why it doesn't work in batch PDFs but seem to work fine if we use single PDF document.

DropBox url is at https://www.dropbox.com/sh/uhoi3821eg1kcdk/AAC_x8zbI-HXBIu15-5_izqIa?dl=0

If you see the code we used "CreatePdfTemplate()", it works okay for 1 PDF file.   So, with batch PDFs, we called "CreatePdfTemplate()" multiple times, so we know there's no bug in "CreatePdfTemplates()" because the 1 PDF file works.   So, it seem there's some issue with the way the Quick-PDF's "MergeDocuemnt".

So anyone know what the problem is?   

Thanks.

Code we use is 

        private int CreatePdfTemplate(PDFLibrary pdfApp, FormWriter formWriter, bool isDocuSign)
        {
            var formDescription = formWriter.Forms.Description;
            var pdfFilename = formDescription.ToLower().EndsWith(".pdf") ? $"{formDescription}" : $"{formDescription}.pdf";
            var azureKeyName = $"{formWriter.Company.AcctNo}/PDF/{pdfFilename}";
            var pdfBytes = new byte[] { };

            formWriter.PdfTemplateLocation = pdfFilename;

            using (var pdfFileStream = _amazonManagerService.GetPdfTemplate(azureKeyName))
            using (var ms = new MemoryStream())
            {
                pdfFileStream.CopyTo(ms);
                pdfBytes = ms.ToArray();
            }

            pdfApp.LoadFromString(pdfBytes, null);
            pdfApp.SetNeedAppearances(-1);

            for (int fieldIX = 0; fieldIX < pdfApp.FormFieldCount(); fieldIX++)
            {
                var loopCounter = fieldIX + 1;
                var fieldName = pdfApp.GetFormFieldTitle(loopCounter);  // FoxIt QuickPDF start with 1, not 0.

                if (string.IsNullOrEmpty(fieldName))
                {
                    continue;
                }
                else if (fieldName.EndsWith("."))  // This indicate we have AcroForm field-name duplications.  (FoxIt Quick PDF append the "." to it for unknown reasons).
                {
                    fieldName = fieldName.Substring(0, (fieldName.Length - 1));
                }

                bool isString = true;  // Do we really need this?  (It is unused here & can be moved inside this "SendDpValuesToPdf" method instead).
                var fieldResult = _formService.SendDpValuesToPdf(fieldName, formWriter, ref isString);
                if (fieldResult.Value != null)
                {
                    var fieldValue = fieldResult.Value.ToSafeString().Trim();

                    if (string.IsNullOrEmpty(fieldValue))  
                    {
                         continue;
                    }

                    pdfApp.SetFormFieldValueByTitle(fieldName, fieldValue) ;
                }
            }

            return pdfApp.SelectedDocument();
        }


            var pdfBytes = new byte[] { };
            var pdfApp = _pdfEditorService.PdfInitialize();
            var mainDocumentId = -1;

            foreach (var (formWriter, index) in formWriters.WithLoopIndex())
            {
                    var tmpDocumentId = CreatePdfTemplate(pdfApp, formWriter, isDocuSign);
                    if (index == 0)
                    {
                        mainDocumentId = tmpDocumentId;
                    }
                    else
                    {
                        pdfApp.SelectDocument(mainDocumentId);
                        pdfApp.MergeDocument(tmpDocumentId);
                    }
            }

            pdfBytes = pdfApp.SaveToString();

            return pdfBytes;
 



Replies:
Posted By: Ingo
Date Posted: 09 Feb 21 at 7:12PM
Hi Fletch,

if "some fields don't work" after merge i think you wanna merge files with identical formfieldnames?
This won't work in a proper way with QuickPDF.
So - for example - don't try to merge many invoices with the same structure to a catalogue.



-------------
Cheers,
Ingo



Posted By: fletchsod
Date Posted: 09 Feb 21 at 7:19PM
Ok, I get it now.  I compared your answer to other posts issue someone else had & I now understand what you're trying to say.

Do you know of an API method that would properly rename the field title name?  That include the field name duplication.   We cannot change the field name on 500+ PDF, by the way.

Thanks


Posted By: Ingo
Date Posted: 10 Feb 21 at 8:24PM
In this function group you'll find all you need for renaming:
https://www.debenu.com/docs/pdf_library_reference/FormFields.php
First you have to get all form field names in a loop.
Second you can rename all names with new ones.
FormFieldCount to get the number of fields in a pdf.
GetFormFieldTitle in a loop gets all field names.
FindFormFieldByTitle to get the special form field.
SetFormFieldTitle to set a new name for the form field.

Cheers and good luck,
Ingo


-------------
Cheers,
Ingo




Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk