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;
}
}
}