Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Jul 28, 2015 4:49 am

I have list of pdf documents.
var documentList = new List<PdfDocument>();

I am trying to create merged pdf file reading memory stream and converting to pdf documents.
I have list of documents that has been added to documentList of PdfDocument type.

then error happening when i am trying to append page to list to pass back to handler.
following exception occurs

System.NullReferenceException: Object reference not set to an instance of an object.
at Spire.Pdf.Bookmarks.PdfBookmarkWidget.ᜀ()
at Spire.Pdf.PdfDocumentBase.᜝()
at Spire.Pdf.PdfDocumentBase.ImportPageRange(PdfDocumentBase
ldDoc, Int32 startIndex, Int32 endIndex)
at Spire.Pdf.PdfDocumentBase.ᜁ(PdfDocumentBase A_0)

Following is code
IEnumerable<docs> = <list of documents >;

if(docs.Any())
{
foreach (var doc in docs)
{
documentList.Add(new PdfDocument(doc));
}
}
Var first = true;
foreach (var item in documentList)
{
if (!first && item != null)
documentList.First().AppendPage(item); // throwing error on this line
first = false;
}

kulwinder
 
Posts: 6
Joined: Tue Feb 03, 2015 9:14 pm

Tue Jul 28, 2015 9:21 am

Dear kulwinder,

Thanks for your inquiry.
I have tested your code and don’t reproduce your issue. So please provide us your sample documents which are added into documentList to help us reproduce this issue. And then we will update to you.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Wed Jul 29, 2015 1:30 am

I should mention before i am trying to insert a photo of each person to pdf document and then adding list of pdfDocument type to merge in one big pdf file.
Following is code i am using to create PdfDocument list.(images could be png,jpg, jpeg, giff formats)

Code: Select all
var docs = GetDocumentsList();
var documentList = new List<PdfDocument>();

if (docs.Any())
            {
                foreach (var doc in docs)
                {
                        var person = q_Person.SingleOrDefault(x => x.ID == doc.PersonID);
                        if (person != null)
                        {
                           // add photo as first document, load from
                            AddPersonPhoto(documentList, person);
                        }
                 using (var docStream = new MemoryStream(doc.GetFileContent()))
                    {
                        var extension = Path.GetExtension(doc.filetype);
                        if (extension != null && extension.ToLower() != ".pdf")
                        {
                            using (var dstMemory = new MemoryStream())
                            {
                                var ndoc = new Document(docStream);
                                ndoc.Save(dstMemory, SaveFormat.Pdf);
                                dstMemory.Position = 0;
                                documentList.Add(new PdfDocument(dstMemory));
                            }
                        }
                        else
                        {
                            docStream.Position = 0;
                            documentList.Add(new PdfDocument(docStream));
                        }
                    }
            }
            var first = true;
            foreach (var item in documentList)
            {
                if (!first)
                    documentList.First().AppendPage(item);
                first = false;
            }
            if (documentList.Count > 0)
            {
                var firstOrDefault = documentList.FirstOrDefault();
                firstOrDefault?.SaveToFile(pdfFullPath);
            }
}


        private static void AddPersonPhoto(List<PdfDocument> documentList, q_Person person)
        {
            var imageUrl = person.GetProfileImagePath();
            if (imageUrl == null) return;
            var doc = new PdfDocument();
            var page = doc.Pages.Add();

            var image = PdfImage.FromFile(HttpContext.Current.Server.MapPath(imageUrl));
            var width = image.Width * 0.75f;
            var height = image.Height * 0.75f;
            var x = (page.Canvas.ClientSize.Width - width) / 2;
            page.Canvas.DrawImage(image, x, 60, width, height);
            documentList.Add(doc);[code][/code]
        }
}




[NullReferenceException: Object reference not set to an instance of an object.]
Spire.Pdf.PdfDocumentBase.ImportPageRange(PdfDocumentBase ldDoc, Int32 startIndex, Int32 endIndex) +2711
Spire.Pdf.PdfDocumentBase.ᜁ(PdfDocumentBase A_0) +536

kulwinder
 
Posts: 6
Joined: Tue Feb 03, 2015 9:14 pm

Wed Jul 29, 2015 10:02 am

Dear kulwinder,

Thanks for your information. We will further investigate this issue, and then update to you.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Return to Spire.PDF

cron