Hi,
i want to search a word and then replace it with another in a XFA Form PDF.
So at the moment I convert the PDF into a XML file. Then I search the word and replace it. But now I have the problem that I can´t convert the XML file back to PDF.
Can anyone help me to bring it back to PDF or is there another way to search and replace in a XFA Form?
Can you please share some more details like sample PDF and the code snippet that you have used so far to convert PDF into XML? We will test the scenario in our environment and address it accordingly.
For reasons of secrecy I can´t give you a sample PDF. But here is the code i used.
That is the code to convert the PDF into XML
Document doc = new Document(oldpath+fileName+".pdf");
String[] fieldNames = doc.Form.XFA.FieldNames;
XmlDocument xmlDoc = new XmlDocument();
if (fieldNames.Length > 0)
{
xmlDoc = doc.Form.XFA.GetFieldTemplate(fieldNames[0]).OwnerDocument;
}
else
Debug.WriteLine("Error");
xmlDoc.Save(newpath+fileName+".xml");
That is the code to convert the XML into a String
String xmlString;
using (var stringWriter =new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
xmlDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
xmlString=stringWriter.GetStringBuilder().ToString();
}
Then I edit it and convert it back to XML with this code
var stream = new MemoryStream();
var writer =new StreamWriter(stream);
writer.Write(newxmlString);
writer.Flush();
stream.Position = 0;
XmlDocument newxmlDoc = new XmlDocument();
newxmlDoc.Load(stream);
newxmlDoc.Save(newpath + fileName + "Updated.xml");
But now I don´t know how I get the XML back into a XFA Form PDF.
You need specific format in XML in order to import it again to the PDF. You can please check below topic as we hope these may help:
I created the XML file like in the example.
Aspose.Pdf.Facades.Form form = new Aspose.Pdf.Facades.Form();
form.BindPdf(oldpath + fileName + ".pdf");
System.IO.FileStream xmlOutputStream = new FileStream(newpath + fileName + ".xml", FileMode.Create);
form.ExportXml(xmlOutputStream);
xmlOutputStream.Close();
Then i edited it and then imported it back with this code
form.ImportXml(System.IO.File.OpenRead(newpath + fileName + ".xml"))
form.Save(newpath + fileName + ".pdf");
So on the first side of the PDF document the changes are applied but on the other pages aren´t applied.
Would you kindly share your sample source files with us so that we can test the scenario in our environment and address it accordingly.
Is there a way to send you the file as a private message? I don´t want the file to be publicly accessible.