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