neeraj kumar

neeraj kumar

  • 1.6k
  • 144
  • 3.3k

getting error in PdfCopy copy = new PdfCopy(document, stream);

Aug 23 2023 7:32 AM
MemoryStream ms1 = HtmlToPDFConversion.GenerateGratuityPDF(InnovID, Convert.ToInt32(Session["MappingID"]));
MemoryStream ms2 = HtmlToPDFConversion.GeneratePFPDF(InnovID, Convert.ToInt32(Session["MappingID"]));
MemoryStream ms3 = HtmlToPDFConversion.GeneratePF11FormPDF(InnovID, Convert.ToInt32(Session["MappingID"]));
MemoryStream ms4 = HtmlToPDFConversion.GenerateESICJoiningKitPDF(InnovID, Convert.ToInt32(Session["MappingID"]));

MemoryStream mergedStreams = MergePdfStreams(ms1, ms2, ms3, ms4);

string fileName = $"ComplianceJoiningKit_{InnovID}.pdf";
return base.File(mergedStreams, "application/pdf", fileName); 

public static MemoryStream MergePdfStreams(params MemoryStream[] pdfStreams)
{
    MemoryStream mergedStream = new MemoryStream();
    Document document = new Document();
    MemoryStream stream = new MemoryStream();
    PdfCopy copy = new PdfCopy(document, stream); //System.NullReferenceException: 'Object reference not set to an instance of an object.
    document.Open();

    foreach (MemoryStream pdfStream in pdfStreams)
    {
        PdfReader reader = new PdfReader(pdfStream.ToArray());
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            PdfImportedPage page = copy.GetImportedPage(reader, i);
            copy.AddPage(page);
        }
        reader.Close();
    }

    document.Close();
    return stream;
}

 


Answers (5)