Hi all,
I'm posting here to ask for a huge help from you..
I need to accomplish a task and I'm having some problems..
I need to implement a method to merge fields in a PDF with forms created in Adobe Life Cycle.
I'll receive the template PDF and a XML to populate the PDF and need to return the new filled file.
The xml is something like this:
Name of the document
So, I have something like the following:
private MemoryStream GeneratePDF(string m_FormName, XmlDocument oData)
{
PdfReader pdfTemplate;
PdfStamper stamper;
PdfReader tempPDF;
Document doc;
MemoryStream msTemp;
PdfWriter pCopy;
MemoryStream msOutput = new MemoryStream();
pdfTemplate = new PdfReader(m_FormName);
doc = new Document();
pCopy = new PdfCopy(doc, msOutput);
pCopy.AddViewerPreference(PdfName.PICKTRAYBYPDFSIZE, new PdfBoolean(true));
pCopy.AddViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
doc.Open();
for (int i = 1; i < pdfTemplate.NumberOfPages + 1; i++)
{
msTemp = new MemoryStream();
pdfTemplate = new PdfReader(m_FormName);
stamper = new PdfStamper(pdfTemplate, msTemp);
// map xml values to pdf form controls (element name = control name)
foreach (XmlElement oElem in oData.SelectNodes("/form1/*"))
{
stamper.AcroFields.SetField(oElem.Name, oElem.InnerText);
}
stamper.FormFlattening = true;
stamper.Close();
tempPDF = new PdfReader(msTemp.ToArray());
((PdfCopy)pCopy).AddPage(pCopy.GetImportedPage(tempPDF, i));
pCopy.FreeReader(tempPDF);
}
doc.Close();
return msOutput;
}
{
PdfReader pdfTemplate;
PdfStamper stamper;
PdfReader tempPDF;
Document doc;
MemoryStream msTemp;
PdfWriter pCopy;
MemoryStream msOutput = new MemoryStream();
pdfTemplate = new PdfReader(m_FormName);
doc = new Document();
pCopy = new PdfCopy(doc, msOutput);
pCopy.AddViewerPreference(PdfName.PICKTRAYBYPDFSIZE, new PdfBoolean(true));
pCopy.AddViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
doc.Open();
for (int i = 1; i < pdfTemplate.NumberOfPages + 1; i++)
{
msTemp = new MemoryStream();
pdfTemplate = new PdfReader(m_FormName);
stamper = new PdfStamper(pdfTemplate, msTemp);
// map xml values to pdf form controls (element name = control name)
foreach (XmlElement oElem in oData.SelectNodes("/form1/*"))
{
stamper.AcroFields.SetField(oElem.Name, oElem.InnerText);
}
stamper.FormFlattening = true;
stamper.Close();
tempPDF = new PdfReader(msTemp.ToArray());
((PdfCopy)pCopy).AddPage(pCopy.GetImportedPage(tempPDF, i));
pCopy.FreeReader(tempPDF);
}
doc.Close();
return msOutput;
}
Hope someone can help me.
Kind Regards,
GV
Jasna KozeljPosted Mar 10, 2014, 3:18 PM
Toolkit.
GoncaloPosted Jan 2, 2014, 3:50 AM
I tried to pass html and it worked with some tags.
However I need to be able to use an image in one of the fields. How can I do this?
The last requirement is to be able to make the PDF (all fields) readonly.
I found a solution using javascript injection but I don't really like that approach.
Thank you in advance.
Regards,
GV
arron leePosted Jan 1, 2014, 9:37 PM
I have ever tried to merge PDF files with the help of the following code:
using YiiGo.Imaging.Basic;
using YiiGo.Imaging.Basic.Core;
using YiiGo.Imaging.Basic.Codec;
using YiiGo.Imaging.PDF;
namespace YG__Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string FolderName = "c:/";
private void button1_Click(object sender, EventArgs e)
{
string fileName1 = FolderName + "Sample1.pdf";
string fileName2 = FolderName + "Sample2.pdf";
string fileNameMerged = FolderName + "Merged.pdf";
YGDocument doc1 = YGFile.OpenDocumentFile(fileName1, new PDFDecoder());//use PDFDecoder open one pdf file
YGDocument doc2 = YGFile.OpenDocumentFile(fileName2, new PDFDecoder());//use PDFDecoder open another pdf file
BaseDocument docMerged = doc1.MergeDocument(doc2);//merge two pdf
YGFile.SaveDocumentFile((YGDocument)docMerged, fileNameMerged, new PDFEncoder());//save new pdf
}
}
}
Or you can also google it and select a PDF manipulator whose way of processing is
simple and fast to help you with the related converting work. It will be better if it is
totally manual and can be customized by users according to our own favors.
Remember to check its free trial package first if possible. I hope you success. Good luck.
Best regards,
Arron
Dhirendra MisraPosted Dec 27, 2013, 1:30 AM
Please refer the following link. It might be helpful to you.
Good Luck:
http://stackoverflow.com/questions/13819550/merging-pdf-fields-from-identical-forms-in-itextsharp
Thanks