abcd
abcd

Reputation: 21

iTextSharp - Printing Merged PDF Templates

I am using iTextSharp in asp.net (C#) in order to merge more than one PDf template. There is a functionality of print which prints only data of the template. While merging the templates the fields/Controls which are available in the template is renamed using RenameField Method of iTextSharp. This implementation has broken the Print functionality. As the Print functionality has been written according to the fields. For merging Templates, I am using a PDFCopy.

Document document = new Document();
bool flag = true;
using (FileStream fileStream = File.Create(newFile))
{
    PdfSmartCopy copy = new PdfSmartCopy(document, fileStream);
    PdfReader reader;
    MemoryStream baos;
    for (int i = 0; i < loopCount; i++)
    {
        reader = new PdfReader(pdfTemplate);
        baos = new MemoryStream();
        stamper = new PdfStamper(reader, baos);
        AcroFields pdfDoc = stamper.AcroFields;
        BuildData(datarow,pdfDoc, obj)
        renameFields(reader);
         stamper.FormFlattening = false;
         stamper.Close();
        reader = new PdfReader(baos.ToArray());
        copy.AddPage(copy.GetImportedPage(reader, 1));
    }
    document.Close();
    strFileName = newFile;
}

private static void renameFields(PdfReader pdfReader)
{
    string prepend = String.Format("_{0}", counter++);
    foreach (KeyValuePair<string, AcroFields.Item> de in pdfReader.AcroFields.Fields)
    {
         pdfReader.AcroFields.RenameField(de.Key.ToString(), prepend + de.Key.ToString());
    }
 }

Edit1: This is the solution i found in itextsharp doc,BUT it's not working "Using PdfCopy with documents that have named destinations is one of these exceptions. All annotations, such as link annotations, are kept with PdfCopy, but they no longer work for links to local named destinations. There is a workaround for this problem."

PdfReader[] readers = {
new PdfReader(LinkActions.RESULT2),
new PdfReader(LinkActions.RESULT1) };
Document document = new Document();
PdfCopy copy =
new PdfCopy(document, new FileOutputStream(RESULT1));
document.open();
int n;
for (int i = 0; i < readers.length; i++) {
readers[i].consolidateNamedDestinations();
n = readers[i].getNumberOfPages();
for (int page = 0; page < n; ) {
copy.addPage(copy.getImportedPage(readers[i], ++page));
}
} 

Upvotes: 2

Views: 932

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

The forms no longer work because you have forgotten to add a single line: copy.setMergeFields();

See the MergeForms2 example:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
    copy.setMergeFields();
    document.open();
    List<PdfReader> readers = new ArrayList<PdfReader>();
    for (int i = 0; i < 3; ) {
        PdfReader reader = new PdfReader(renameFields(src, ++i));
        readers.add(reader);
        copy.addDocument(reader);
    }
    document.close();
    for (PdfReader reader : readers) {
        reader.close();
    }
}

public byte[] renameFields(String src, int i) throws IOException, DocumentException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, baos);
    AcroFields form = stamper.getAcroFields();
    Set<String> keys = new HashSet<String>(form.getFields().keySet());
    for (String key : keys) {
        form.renameField(key, String.format("%s_%d", key, i));
    }
    stamper.close();
    reader.close();
    return baos.toByteArray();
}

It seems that you're also making the assumption that your template consists of only one page:

copy.addPage(copy.getImportedPage(reader, 1));

It is safer to add the document all at once:

copy.addDocument(reader);

Important:

My examples are written in Java. You are working with iTextSharp in C#. You will have to adapt the methods by changing the Java-specific methods into C#-specific properties or methods.

Upvotes: 0

Related Questions