Populate an image field using XFA.SetFieldImage() in PDF using Aspose.PDF for .NET

Hello,


I am attempting to open a PDF, find all the image fields of a certain name, then populate each image field with the same image. For example, a single PDF may have any number of pages and each page may have multiple image fields that need to be populated with the same image. Unfortunately, nothing I have tried so far is working.

The layout of the PDF is as follows:

Page 1
LogoField1
LogoField2

Page 2
LogoField1
LogoField2

Page 3
LogoField1
LogoField2…

…and so on.

Here is the code I am using:

Document doc = new Document(“test.pdf”);
FileStream fs = new FileStream(“logo.png”, FileMode.Open, FileAccess.Read);

string[] fieldNames = (from x in doc.Form.XFA.FieldNames
where x.Contains(“LogoField”)
select x.ToString()
).ToArray();

foreach (var x in fieldNames)
{
doc.Form.XFA.SetFieldImage(x.ToString(), fs);
}

doc.Save(“test.pdf”);

I do not get an error, just no images. Any assistance you can offer would be extremely helpful.

Thank you.

Hi Jaden,


Thanks for your inquiry. We will appreciate it if you please share your sample PDF document here, so we will look into it and guide you accordingly.

Moreover, please check sample code snippet to fill Acro Form field and XFA form field as following.

…<o:p></o:p>

//to fill AcroForm field

TextBoxField textBoxField = pdfDocument.Form["BarcodeFinalReport"] as TextBoxField;

System.Drawing.Image img = System.Drawing.Image.FromFile(myDir + "barcode.jpg");

textBoxField.AddImage(img);

.....

//to fill XFAForm field

FileStream fs = new FileStream("image.jpg", FileMode.Open, FileAccess.Read);

doc.Form.XFA.SetFieldImage("form1[0].ImageField1[0]", fs);


Best Regards,

Thank you for your prompt reply.


I am familiar with the XFAForm code you mentioned and have actually used it in the past. However, my current situation is a little different in that the number of fields needing to be set is variable; so, I cannot hardcode a path to every possible image field. That is why I am attempting to get the entire collection of form fields that contain a specific naming convention, loop through that collection, and populate each of the fields in the collection with an image.

As requested, I have attached a PDF example.

Thank you!

Hi Jaden,


Thanks for sharing your source document. I am looking into it and will get back to you soon.

Best Regards,

Hi Jaden,


Sorry for the delayed response. We have managed to notice the issue with SetFeildImage() and logged a ticket PDFNEWNET-37926 in our issue tracking system for further investigation and resolution. We will keep you updated about the issue resolution progress within this forum thread.

We are sorry for the inconvenience.

Best Regards,