1

I am new to c# and itextsharp and have a problem merging 2 or more pdfs using itextsharp. I have 2 methods that fill a pdf template each and saves them to a memorystream and a downloadAsPDF methods to return the combined pdfs to the broswer. the downloadAsPDF works when i add only one of the memorystream but when i add both MemoryStreams to the list I receive the message...If this message is not eventually replaced by the proper contents of the document, your PDF viewer may not be able to display this type of document. Any help would be much appreciated. Thanks

public byte[] fillTemplateA()
{
    String NfoodAllergyTemplate = "~/pdfTemplateA.pdf";

    //setting readers and stamper and memorystream
    PdfReader ReadPdf = new PdfReader(NfoodAllergyTemplate);
    MemoryStream msNfoodAllergyTemplate = new MemoryStream();
    PdfStamper StampPdf = new PdfStamper(ReadPdf, msNfoodAllergyTemplate,'\0',true);
    AcroFields NFoodAlF = StampPdf.AcroFields;

    NFoodAlF.SetField("stdFullNameNFAAP", testObject);

    StampPdf.FormFlattening = false;
    StampPdf.Close();
    ReadPdf.Close();


    byte[] ms1 = msNfoodAllergyTemplate.ToArray();
    return ms1;
}

Here is my second methos that fills the 2nd pdf

public byte[] fillTemplateB()
{
    String NfoodAllergyTemplate = "~/pdfTemplateB.pdf";


    //setting readers and stamper and memorystream
    PdfReader ReadPdf = new PdfReader(NfoodAllergyTemplate);
    MemoryStream msNfoodAllergyTemplate = new MemoryStream();
    PdfStamper StampPdf = new PdfStamper(ReadPdf, msNfoodAllergyTemplate,'\0',true);
    AcroFields NFoodAlF = StampPdf.AcroFields;

    NFoodAlF.SetField("stdFullNameNFAAP", testObject);

    StampPdf.FormFlattening = false;
    StampPdf.Close();
    ReadPdf.Close();


    byte[] ms2 = msNfoodAllergyTemplate.ToArray();
    return ms2;
}

merge pdf method

public MemoryStream MergePDFs(List<byte[]> pdfFiles) 
{
    if(pdfFiles.Count >1)
    {
        PdfReader finalPdf;
        Document pdfContainer;
        PdfWriter pdfCopy;
        MemoryStream msFinalPdf = new MemoryStream();

        finalPdf = new PdfReader(pdfFiles[0]);
        pdfContainer = new Document();
        pdfCopy = new PdfSmartCopy(pdfContainer, msFinalPdf);

        pdfContainer.Open();

        for (int k = 0; k < pdfFiles.Count; k++)
        {
            finalPdf = new PdfReader(pdfFiles[k]);
            for (int i = 1; i < finalPdf.NumberOfPages + 1; i++)
            {
                ((PdfSmartCopy)pdfCopy).AddPage(pdfCopy.GetImportedPage(finalPdf, i));
            }
            pdfCopy.FreeReader(finalPdf);

        }
        finalPdf.Close();
        pdfCopy.Close();
        pdfContainer.Close();

        return msFinalPdf;
    }
    else if(pdfFiles.Count==1)
    {
        return new MemoryStream(pdfFiles[0]);
    }
    return null;
}
                       }

in this last method I merge the pdfs in memorystream from FillTemplate A and B

public void finalPdf()
{
    //List to collect all pdfs in memorystream that have been assinged to a byte array

    System.Collections.Generic.List<byte[]> collectAllForms = new List<byte[]>();
    //when i add only one of the line below it works...having both give error message...Please wait...
    collectAllForms.Add(fillTemplateA());
    collectAllForms.Add(fillTemplateB());

    //Call the downloadAsPDF method 
    DownloadAsPDF(MergePDFs(collectAllForms), "QVSD_Packet");
}

Here is the downloadAsPDF method

public void DownloadAsPDF(MemoryStream msFinal,String theFileName)
{
    theFileName = "attachment; filename =" + theFileName + ".pdf";

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.AddHeader("Content-Disposition", theFileName);
    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpContext.Current.Response.BinaryWrite(msFinal.ToArray());
    HttpContext.Current.Response.OutputStream.Flush();
    HttpContext.Current.Response.OutputStream.Close();
    HttpContext.Current.Response.End();
    msFinal.Close();
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.