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.

Mon Feb 07, 2022 2:55 am

Hello,

I was trying to utilize the ReArrange method on the PdfPageCollection class in order to re-arrange the pages of my document, however it seems that no matter what I always receive a System.NullReferenceException from within that method. I am using Spire.Office v7.1.4 with an enterprise license.

I've tried uploading a small console application as a .zip file to this post that reproduces the error but unfortunately it does not appear to be working. Please use the code snippet below in a console application in order to reproduce the error:

Code: Select all
static void Main(string[] args)
        {
            var pdfDoc = new PdfDocument();

            for (int i = 0; i < 3; i++)
            {
                pdfDoc.Pages.Add();
            }

            pdfDoc.Pages.ReArrange(new int[] { 2, 1, 0 });

            Console.WriteLine("Successfully rearranged pages");
            Console.ReadLine();
        }


Please let me know when I can expect this method to start working as intended.

Thank you,
Zac

ZCliff92
 
Posts: 31
Joined: Thu Jan 20, 2022 1:58 am

Mon Feb 07, 2022 3:41 am

To add to the post above, through further testing, it appears that saving the document to the file system or a memory stream first, loading it back in and then re-arranging the pages works.

Code: Select all
static void Main(string[] args)
        {
            var pdfDoc = new PdfDocument();

            for (int i = 0; i < 3; i++)
            {
                pdfDoc.Pages.Add();
            }

            pdfDoc.SaveToFile("C:\\temp\\junk\\test.pdf");

            var newPdf = new PdfDocument();

            newPdf.LoadFromFile("C:\\temp\\junk\\test.pdf");

            newPdf.Pages.ReArrange(new int[] { 2, 1, 0 });

            Console.WriteLine("Successfully rearranged pages");
            Console.ReadLine();
        }


Alternatively:

Code: Select all
static void Main(string[] args)
        {
            var pdfDoc = new PdfDocument();

            for (int i = 0; i < 3; i++)
            {
                pdfDoc.Pages.Add();
            }

            using (var ms = new MemoryStream())
            {
                pdfDoc.SaveToStream(ms);

                var newPdf = new PdfDocument();

                newPdf.LoadFromStream(ms);

                newPdf.Pages.ReArrange(new int[] { 2, 1, 0 });
            }
               

            Console.WriteLine("Successfully rearranged pages");
            Console.ReadLine();
        }


While this provides a solution for this instance, having to save the file to disc or a stream before re-arranging the pages sort of defeats the purpose of what I'm trying to do with the document. Given that the document I am producing could potentially be GB's in size writing and reading from disc or a stream would be very cumbersome and has often resulted in exceptions being thrown by the PdfDocument constructor or LoadFromFile method.

Is there any way the ReArrange method can be used prior to the PdfDocument being finalised? Do you have a work around that might help me achieve this?

ZCliff92
 
Posts: 31
Joined: Thu Jan 20, 2022 1:58 am

Mon Feb 07, 2022 5:52 am

Hello,

Thanks for your inquiry and addition instructions.

I have reproduced your issue and logged it in our issue tracking system with the ticket SPIREPDF-4933 for further investigation.

We will let you know if there is any update. Sorry for the inconvenience caused.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Feb 07, 2022 6:35 am

Hi Marcia,

Thank you for responding and logging this issue.

Are you able to provide me an ETA on when this issue will be resolved?

Thank you,
Zac

ZCliff92
 
Posts: 31
Joined: Thu Jan 20, 2022 1:58 am

Mon Feb 07, 2022 6:41 am

Hello Zac,

You are welcome.

Sorry that I cannot provide an ETA time now because our dev team needs some time to analyze the issue. Anyway, I urged and requested them to share any possible ETA at their earliest, once there is any good news, we will notify you immediately.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Feb 11, 2022 8:21 am

Hello,

Thanks for your patience!

I get news from our dev team that the issue about ReArrange method need to adjust our underlying code, we need more time to analyze and investigate it.

As a temporary method, you can refer to the following code to sort the page. And once there is any progress regarding the issue SPIREPDF-4933, we will notify you immediately.
Code: Select all
            var pdfDoc = new PdfDocument();

            for (int i = 0; i < 3; i++)
            {
                pdfDoc.Pages.Add().Canvas.DrawRectangle(PdfBrushes.Red,i*100,i*100, (i+1) * 100, (i+1) * 100);
            }
            //pdfDoc.Pages.ReArrange(new int[] { 2, 1, 0 });
            var newpdf = ReArrange(pdfDoc, new int[] { 2, 1, 0 });
            newpdf.SaveToFile("res.pdf");

        private static PdfDocument ReArrange(PdfDocument pdf, int[] sort)
        {
            PdfDocument tempPdf = new PdfDocument();
            for(int i=0; i <sort.Length; i++)
            {
                tempPdf.InsertPage(pdf,sort[i],i);
            }
            return tempPdf;
        }


Thanks for your understanding.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.PDF