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 Nov 03, 2020 12:57 am

Hey,

When generating a document from scratch (i.e. not editing an existing document) I'm seeing some unexpected behaviour.

Pages.Insert(1)
This method call will hang forever, requiring the program to be terminated.

Pages.Insert(1, PdfPageSize.A4)
This will succeed and the new page will be added at the correct location.
However, after calling SaveToFile(...), the saved PDF will have a duplicate page inserted at the end. I.e. the new page will be inserted at both index 1 and as the last page.

Neither of these issues happen when I'm loading an existing PDF document from file and calling Insert() on it, only when calling it on a new document - so one work around is to save the doc to file, then reload it and call insert, but I would prefer not to do that.
I also noticed that the Pages.ReArrange() method breaks with an index out of bounds exception when called on a new document, but succeeds on an existing one that was loaded.

Is there some method that needs to be called on the pages before these sorts of methods can be called?

I'm using:

.NET 4.5
Spire.PDF 6.5.15

Code Sample:

Code: Select all
var doc = new PdfDocument
{​​​
    PageSettings =
    {​​​
        Size = PdfPageSize.A4,
        Margins = new PdfMargins
        {​​​
            Top = 40,
            Bottom = 20,
            Left = 40,
            Right = 40
        }​​​
    }​​​
}​​​;

var font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f, FontStyle.Bold));
var format = new PdfStringFormat(PdfTextAlignment.Left);

var page1 = doc.Pages.Add();
page1.Canvas.DrawString("Page 1", font, PdfBrushes.Black, 0, 0, format);

var page2 = doc.Pages.Add();
page2.Canvas.DrawString("Page 2", font, PdfBrushes.Black, 0, 0, format);

var page3 = doc.Pages.Add();
page3.Canvas.DrawString("Page 3", font, PdfBrushes.Black, 0, 0, format);

var pageX1 = doc.Pages.Insert(1); // This will hang forever
pageX1.Canvas.DrawString("Page X1", font, PdfBrushes.Black, 0, 0, format);

var pageX2 = doc.Pages.Insert(1, PdfPageSize.A4); // This succeeds, but when saving to file a duplicate Page X2 is added at the end
pageX2.Canvas.DrawString("Page X2", font, PdfBrushes.Black, 0, 0, format);

// Note that at this point before calling SaveToFile(), the Pages collection has the correct number of pages.

doc.SaveToFile("C:\\temp\\Spire\\Test.pdf");
doc.Close();

// After the SaveToFile() has been called, the saved PDF will have a duplicate page added at the end

alexsandilands
 
Posts: 2
Joined: Tue Nov 03, 2020 12:33 am

Tue Nov 03, 2020 10:19 am

Hello,

Thanks for your inquiry.
I did reproduce your issue when using the “Pages.Insert(1)” and “Pages.Insert(1, PdfPageSize.A4)” methods. As for the Pages.ReArrange method, I tested the following code and got the error“System.NullReferenceException”. I have logged these issues in our bug tracking system with the ticket SPIREPDF-3742 for further investigation. We will keep you informed if there is any progress. Sorry for the inconvenience caused.

Code: Select all
            var doc = new PdfDocument();
 
            var font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f, FontStyle.Bold));
            var format = new PdfStringFormat(PdfTextAlignment.Left);

            var page1 = doc.Pages.Add();
            page1.Canvas.DrawString("Page 1", font, PdfBrushes.Black, 0, 0, format);

            var page2 = doc.Pages.Add();
            page2.Canvas.DrawString("Page 2", font, PdfBrushes.Black, 0, 0, format);

            var page3 = doc.Pages.Add();
            page3.Canvas.DrawString("Page 3", font, PdfBrushes.Black, 0, 0, format);


            var pageX2 = doc.Pages.Insert(1, PdfPageSize.A4);
            pageX2.Canvas.DrawString("Page X2", font, PdfBrushes.Black, 0, 0, format);

            int[] range = new int[] { 0 };
            doc.Pages.ReArrange(range);//System.NullReferenceException

            doc.SaveToFile("Test.pdf");
            doc.Close();


Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Wed Nov 04, 2020 8:02 pm

Great, good to know, thanks for the response!

Note, the workaround I'm currently using is to save the Document to a MemoryStream, then reloading it as a new document with the LoadFromStream method.
This allows all 3 of those functions to work as expected.

Cheers

alexsandilands
 
Posts: 2
Joined: Tue Nov 03, 2020 12:33 am

Thu Nov 05, 2020 1:46 am

Hello,

Thanks for your feedback.
Please use the workaround you mentioned for now. Once there is any progress regarding the issue SPIREPDF-3742, we will notify you immediately.

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Return to Spire.PDF

cron