Free Support Forum - aspose.com

Attaching a file from XML to a XFA PDF in C#

I am fairly new to coding with Aspose.PDF. Any help would be appreciated. We have successfully taken an XML string and populated it into an XFA PDF. However, the XML has a file attachment that does not get populated into the PDF. I have tried using the Aspose.PDF.Form.XFA.FieldNames to attach it separately. I used a for loop to spit out the values afterwards and I can see they were assigned successfully, but when I open the resulting file, I do not see the attached file. Also, I do not see any of the other values that were assigned using the Aspose.PDF.Facades.Form.SetXfaData().

Input Files.zip (985.5 KB)

My C# Console Code:

using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.InteractiveFeatures.Forms;
using Form = Aspose.Pdf.Facades.Form;

namespace ImportXml
{
    class Program
    {
        static void Main(string[] args) 
        {
            string xmlFile = @"C:\Temp\ExportTest\XmlFile.txt";
            string inputFileFolder = @"C:\Temp\ExportTest\RR_Budget_1_4-V1.4.pdf";
            string tempFilePath = @"C:\Temp\ExportTest\RR_Budget_1_4-V1.4_result.pdf";
            string outputFile = @"C:\Temp\ExportTest\Output.txt";

            string xmlString = File.ReadAllText(xmlFile);

            new License().SetLicense("Aspose.Total.lic");
            using (var xmlStream = GenerateStreamFromString(xmlString))
            {
                 File.Delete("RR_Budget_1_4-V1.4_result.pdf");
                 File.Copy(inputFileFolder, tempFilePath, true);

                 using (var pdfStream = File.Open(tempFilePath, FileMode.Open, FileAccess.ReadWrite))
                 {
                     var form = new Form(pdfStream);
                     form.SetXfaData(xmlStream);
                     form.Save(pdfStream);
                 }

                 using (FileStream fs = new FileStream(tempFilePath, FileMode.Open, FileAccess.ReadWrite))
                 {
                      Document pdfDoc = new Document(fs);
                      string[] names = pdfDoc.Form.XFA.FieldNames;
                      
                      pdfDoc.Form.XFA["RR_SubawardBudget30_1_4[0].RR_Budget_1_4_Main[0].RR_Budget_1_4_P4[0].Attachments0[0].AttachedFile[0].FileName[0]"] = "SF424_Test3.pdf";
                      pdfDoc.Form.XFA["RR_SubawardBudget30_1_4[0].RR_Budget_1_4_Main[0].RR_Budget_1_4_P4[0].Attachments0[0].AttachedFile[0].MimeType[0]"] = "application/pdf";
                      pdfDoc.Form.XFA["RR_SubawardBudget30_1_4[0].RR_Budget_1_4_Main[0].RR_Budget_1_4_P4[0].Attachments0[0].AttachedFile[0].FileLocation[0].href[0]"] = "SF424_Test3_O25DITLMQQSK18S9L8MGR975B1.pdf";
                      pdfDoc.Form.XFA["RR_SubawardBudget30_1_4[0].RR_Budget_1_4_Main[0].RR_Budget_1_4_P4[0].Attachments0[0].AttachedFile[0].HashValue[0].hashAlgorithm[0]"] = "SHA-1";
                      pdfDoc.Form.XFA["RR_SubawardBudget30_1_4[0].RR_Budget_1_4_Main[0].RR_Budget_1_4_P4[0].Attachments0[0].AttachedFile[0].HashValue[0].HashValue_data[0]"] = "j1yKzJ9TVa7PnLAqBloCOIrhhL8";
                      pdfDoc.Save();

                      File.Delete(outputFile);
                      using (StreamWriter sw = new StreamWriter(outputFile))
                      {
                          for (int i = 0; i < names.Length; i++)
                          {
                              //if (names[i].ToLower().Contains("attach"))
                              //{
                                  sw.WriteLine(names[i]);
                                  sw.WriteLine(pdfDoc.Form.XFA[names[i]]);
                              //}
                          }
                      }
                 }
            }
        }

        public static Stream GenerateStreamFromString(string s)
        {
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);
            writer.Write(s);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }
    }
}

@mandres,

The source XML is not being validated from W3 XML validator. Please review and then send us a valid XML document after corrections. Your response is awaited.

My apologies. Here you go.

XmlFile.zip (1.4 KB)

Regards,
Matt

@mandres,

We have tested your scenario with the latest version 17.12 of Aspose.Pdf for .NET API and can set XFA fields. You can retrieve the same values from XFA fields with Aspose.Pdf API. This is the output PDF: RR_Budget_1_4-V1.4_result.pdf (1001.5 KB). In order to attach files, please refer to this help topic: Working with Attachments

Thank you for the response. However, the result pdf file you posted does not show the Budget Attachment file in it (L. Budget Attachment).

I am trying to find a way to get the attachment to show up in that field.

Regards,
Matt

@mandres,

Kindly share a sample of the expected output PDF document. We will investigate and share our findings with you. Your response is awaited.

Desired Result.zip (1.0 MB)

Here you go. Thank you so much!

@mandres,

We have logged an investigation under the ticket ID PDFNET-43943 in our issue tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

I am checking in to see if there has been any update on this?

Regards,
Matt

@mandres,

The linked ticket ID PDFNET-43943 is pending for the analysis and not resolved yet. Our product team will investigate as per their development schedules. We will let you know once it is fixed.

I am checking in again to see if there has been any update on this issue.

Regards,
Matt

@mandres,

The linked ticket ID PDFNET-43943 is not resolved yet and will take time because there are other high priority tickets in the queue. Besides this, we recommend our clients to post their critical issues (or ticket IDs) in the paid support forum. Please refer to this helping link: Aspose support options