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;
}Loading
Kip HackmanPosted Nov 14, 2023, 10:38 PM
I know that you are using the iTextSharp Library to achieve this functionality, but I wanted to note that the LEADTOOLS SDK can help you acheive this as well. You can use LEADTOOLS Document SDK technology in your application.
https://www.leadtools.com/sdk/document
You can leverage the LEADDocument class to load, clone, and export document files. See the below code:
Cr BhargaviPosted Aug 23, 2023, 1:15 PM
Mohammad HussainPosted Aug 23, 2023, 11:29 AM
neeraj kumarPosted Aug 23, 2023, 8:51 AM
GETTING System.NullReferenceException: 'Object reference not set to an instance of an object.
of this line
PdfCopy copy = new PdfCopy(document, stream);
after adding this -
if (document != null) {
PdfCopy copy = new PdfCopy(document, stream);
}
then getting issue in - can't find copy valirable
PdfImportedPage page = copy.GetImportedPage(reader, i);
copy.AddPage(page);
Rathrola Prem KumarPosted Aug 23, 2023, 7:58 AM
What is the error message?
Update:
Sorry, I can see error message
Suspect:
I suspect, your document is null. to confirm this can you add if a condition like below and initialise pdfcopy
if (document != null) {
PdfCopy copy = new PdfCopy(document, stream);
}
If document is null, then that's the issue