C#
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;
}
Mohammad HussainRank #513 · 4.8K reputationPosted Aug 23, 2023, 7:29 AM EDT
public static MemoryStream MergePdfStreams(params MemoryStream[] pdfStreams) { MemoryStream mergedStream = new MemoryStream(); Document document = new Document(); MemoryStream stream = new MemoryStream(); // Initialize the PdfCopy object with the correct document and stream PdfCopy copy = new PdfCopy(document, stream); 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; }Rathrola Prem KumarRank #307 · 8.2K reputationPosted Aug 23, 2023, 3:58 AM EDT
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
neeraj kumar107 reputationPosted Aug 23, 2023, 4:51 AM EDT
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);
Cr BhargaviRank #420 · 5.9K reputationPosted Aug 23, 2023, 9:15 AM EDT
public static MemoryStream MergePdfStreams(params MemoryStream[] pdfStreams) { MemoryStream mergedStream = new MemoryStream(); Document document = new Document(); MemoryStream stream = new MemoryStream(); // Initialize the PdfCopy object with the correct document and stream PdfCopy copy = new PdfCopy(document, stream); 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; }Kip Hackman61 reputationPosted Nov 14, 2023, 5:38 PM EST
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:
// Setup a cache FileCache cache = new FileCache(); cache.CacheDirectory = @"c:\cache-dir"; byte[] bytes = File.ReadAllBytes(@"Image File Path"); MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; LEADDocument _orignal = DocumentFactory.LoadFromStream(ms, new LoadDocumentOptions { Cache = cache }); LEADDocument _orignalCopy = _orignal.Clone(cache, new CloneDocumentOptions()); Stream _stream = _orignalCopy.Stream; return _stream;