PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Oct 04, 2024 3:33 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Wed Jul 24, 2024 4:54 pm 
Offline

Joined: Wed Jul 24, 2024 4:42 pm
Posts: 8
System.InvalidOperationException: 'Failed to compare two elements in the array'

We recently updated from .net framework to .net core. In framework we used PDFsharp - Migradocs version 1.50.5147 but in .net core upgraded to 6.0.0 before we started testing. In framework our pdfs worked fine (which includes cloning). In .net core, we get the above error message any time we use cloning. If we remove cloning, everything works fine. We have the same issue in 3 separate documents that are created different. I have also tried upgrading to PDFsharp - migradocs 6.1.1 in .net core but we have the same results; the documents error out with this error if cloning exists, but the error goes away when cloning is removed. We only see the issue when tables with rows and cells are cloned. If we are only cloning fonts and document settings, we don't seem to get this error. Based on our testing results I feel like there is a bug in PDFsharp cloning code.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jul 24, 2024 9:47 pm 
Offline

Joined: Wed Jul 24, 2024 4:42 pm
Posts: 8
I should have said this earier; I get the error when I call this:

renderer.RenderDocument();


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 25, 2024 5:12 am 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 345
astallcup wrote:
Based on our testing results I feel like there is a bug in PDFsharp cloning code.
It won't be fixed if we cannot replicate it.
https://docs.pdfsharp.net/General/Issue-Reporting.html

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 25, 2024 1:07 pm 
Offline

Joined: Wed Jul 24, 2024 4:42 pm
Posts: 8
Thanks for the reply, I am new here and I didn't know what or how to submit more information. I have attached 2 screen shots, one is the older framework solution in debug, and the other is the .net core solution. I am executing both solutions with the same data set, and both hit the same code in the same manner. I will figure out how to do the additional stuff in the link above but for now, here is this.

My code is building a pdf that has a series of tables on it. It first tries to put a table on the page, and if it fits, then it adds it; if not it removes it and moves it to the next page. I have tried using .KeepWith() but the way the data is set up, the pdf is a bit more complicated than what .KeepWith() can provide.


Attachments:
Old framewrok solution.png
Old framewrok solution.png [ 195.89 KiB | Viewed 16378 times ]
new .net core solution.png
new .net core solution.png [ 220.68 KiB | Viewed 16378 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 25, 2024 1:32 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 989
Location: CCAA
Between version 6.0.0 and version 6.1.1, we resolved an issue with cloned tables.

But with screenshots of code snippets, we will not attempt to replicate the issue.
If you provide a solution file that allows to replicate the issue, then it'll be a different story.


System.InvalidOperationException: 'Failed to compare two elements in the array'
=> That error message comes from .NET, not from PDFsharp.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 25, 2024 1:44 pm 
Offline

Joined: Wed Jul 24, 2024 4:42 pm
Posts: 8
That is good to know that there was a clone issue. I am adding info to the post as I have it. I am in the process of striping my class down to the basics to make it easier to debug. I hope to have this done today sometime hopefully. Thanks for the quick reply.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 25, 2024 2:44 pm 
Offline

Joined: Wed Jul 24, 2024 4:42 pm
Posts: 8
Quote:
=> That error message comes from .NET, not from PDFsharp.


Correct, but a fellow coworker has decompiled the code and stepped through the pdf sharp code and found a line of code inside PDFsharp code where the comparison fails. I can get a screen shot after while.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 25, 2024 4:09 pm 
Offline

Joined: Wed Jul 24, 2024 4:42 pm
Posts: 8
Ok, I have simplified the code and have it to where if you run this code in .net core with 6.1.1, you will get the error I have having. There are no database looks, so everything you need to reproduce the error is here:

Color MuellerBlue = new(0, 44, 115);

Document PDFDoc = new();
Document TempDoc = new();
Section Page1Section = PDFDoc.AddSection();
Table MainTable = Page1Section.AddTable();

PageSetup DocumentSetup = PDFDoc.DefaultPageSetup.Clone();
DocumentSetup.Orientation = Orientation.Portrait;
DocumentSetup.PageHeight = Unit.FromInch(11);
DocumentSetup.PageWidth = Unit.FromInch(8.5);
DocumentSetup.TopMargin = Unit.FromInch(.75);
DocumentSetup.HeaderDistance = Unit.FromInch(.25);
DocumentSetup.LeftMargin = Unit.FromInch(.5);
DocumentSetup.RightMargin = Unit.FromInch(.5);
DocumentSetup.BottomMargin = Unit.FromInch(.75);
DocumentSetup.FooterDistance = Unit.FromInch(.25);
DocumentSetup.DifferentFirstPageHeaderFooter = true;

Paragraph PageNumberParagraph = new Paragraph();
HeaderFooter footer = Page1Section.Footers.Primary;

Style TitleStyle = new("TitleStyle", "Normal");
TitleStyle.Font.Name = "Georgia";
TitleStyle.Font.Bold = true;
TitleStyle.Font.Color = MuellerBlue;
TitleStyle.Font.Size = Unit.FromPoint(16);

PDFDoc.Add(TitleStyle);


int PageCount = 1;
int BlockCounter = 0;
///this is a simplified version of the code I am running.
///the comments to the right indicate the line number in the code to make it easier for me to refer too.
MainTable.AddColumn(Unit.FromInch(6));
Row myrow = MainTable.AddRow();//added to give myrow a definition; not in original code.
Paragraph paragraph = myrow.Cells[0].AddParagraph();
paragraph.AddFormattedText("Based on a random tank Total, Cost is Each", "TitleStyle"); // line 244


Table TitleTable = new();//line 328
TitleTable.AddColumn(Unit.FromInch(6));

Row TitleRow1 = TitleTable.AddRow();//line 448
Row TitleRow2 = TitleTable.AddRow();//line 455
TempDoc.Add(TitleStyle.Clone());// line 534
Section TempSection = Page1Section.Clone();//line 542
TempDoc.Add(TempSection);

//do we need to do this?
PDFDoc = new();//line 546
Table PreviousTable = null;

//start of line 556
TitleRow1 = TitleTable.Rows[0];
TitleRow1.Cells[0].Elements.Clear();
paragraph = TitleRow1.Cells[0].AddParagraph();
string TitleText = "MyCCAssembly.Title";

paragraph.AddFormattedText(TitleText, "TitleStyle");
paragraph.Format.OutlineLevel = OutlineLevel.Level1;
//paragraph.AddBookmark(MyCCAssembly.Title);

// make sure that the bottom border
//of the table is visible.
TitleRow2 = TitleTable.Rows[1];//line 567

TempSection.Elements.Add(TitleTable);//line 574

//start of cost code foreach loop
//start of quoteitemblock foreach loop row 578

MainTable = TempSection.AddTable();
MainTable.AddColumn(Unit.FromInch(6));

//this is the loop where I pull data from the database; there are 56 rows in my table, for testing I am using dummy data.

for (int i = 0; i < 56; i++)
{
myrow = MainTable.AddRow();// line 626
paragraph = myrow.Cells[0].AddParagraph();
paragraph.AddFormattedText("Line on page", "TitleStyle");//line 658
}

myrow = MainTable.AddRow();//line 837
paragraph = myrow.Cells[0].AddParagraph();
paragraph.AddFormattedText("quote item sub total line", "TitleStyle");//line 846

//Now check to see if another page has been added
TempDoc.BindToRenderer(null);
PdfDocumentRenderer PDFRenderer2 = new();
PDFRenderer2.Document = TempDoc;
PDFRenderer2.RenderDocument();
//if I render here and return the resulting pdf to the screen, it works fine, but it goes over to a second page. The code
//that is below attempts to split up the table so that only enough rows exist on the first page,and put these rows in a second table
//on the second page with new headers,
//MemoryStream mem1 = new();
//PDFRenderer2.Save(mem1, closeStream: false);
//return new FileStreamResult(mem1, "application/pdf");


if (PDFRenderer2.PageCount > 1)//line 1040
{
//Another page needs to be added to the document
PageCount++;

//Remove the TitleTable and MainTable from the section in the temporary document
int k = TempSection.Elements.IndexOf(TitleTable);//line 1048
TempSection.Elements.RemoveObjectAt(k);
k = TempSection.Elements.IndexOf(MainTable);
TempSection.Elements.RemoveObjectAt(k);

//The offending items have been removed from the section
//Now add a copy of the section to the final document
PDFDoc.Add(TempSection.Clone());//line 1083
TitleTable = TitleTable.Clone();//line 1085
MainTable = MainTable.Clone();//line 1086

//Create a new temporary document
//and add a copy of TitleTable and MainTable to it
///lines 1185 through 1217; some redundant code removed for simplification
TempDoc = new();//line 1185
TempDoc.Add(TitleStyle.Clone());//line 1186. there are more clones here, but they are all of styles, so I only put 1.
TempSection = TempDoc.AddSection();
TempSection.PageSetup = DocumentSetup.Clone();
TempSection.Headers.FirstPage.SetNull();
footer = TempSection.Footers.Primary;
footer.Format.Alignment = ParagraphAlignment.Center;
footer.Add(PageNumberParagraph.Clone());
TempSection.Elements.Add(TitleTable);

TempSection.Elements.Add(MainTable);//line 1207

TempDoc.BindToRenderer(null);//line 1214
PDFRenderer2 = new();
PDFRenderer2.Document = TempDoc;
PDFRenderer2.RenderDocument();//line 1217

//this is the start of the code that I did two screen shots of in a previous post.
if (PDFRenderer2.PageCount > 1)
{
PageCount++;
//find offending table on page
k = TempSection.Elements.IndexOf(MainTable);
//make a copy of the table
Table OldTable = MainTable.Clone();
//remove table from section
TempSection.Elements.RemoveObjectAt(k);
Document NewDoc = new();
Section thisSection = NewDoc.AddSection();
thisSection.PageSetup = DocumentSetup.Clone();
//Create the footer content for first page
HeaderFooter thisfooter = thisSection.Footers.Primary;
thisfooter.Format.Alignment = ParagraphAlignment.Center;
// Add paragraph to footer for first page.
thisfooter.Add(PageNumberParagraph.Clone());
//add title table
thisSection.Elements.Add(TitleTable.Clone());
//Now create new empty table
MainTable = thisSection.AddTable();
MainTable.AddColumn(Unit.FromInch(6));
//add rows to it until a new page is needed
for (k = 0; k <= OldTable.Rows.Count - 1; k++)
{
myrow = OldTable.Rows[k];
MainTable.Rows.Add(myrow.Clone());

NewDoc.BindToRenderer(null);
PdfDocumentRenderer myrenderer = new();
myrenderer.Document = NewDoc;
myrenderer.RenderDocument();//this is the line where the error happens.

if (myrenderer.PageCount > 1)
{
break;
}
}
}
}


//due to a ton of simplifying, I never tested this section. The error I have is coming from the code above so if we can fix that error
//I should be good to go.
//tempDoc.Sections.Add(tempSection);
//PDFDoc = new();
//PDFDoc.Sections.Add(tempSection.Clone());

PDFDoc.BindToRenderer(null);
PdfDocumentRenderer PDFRenderer = new();
MemoryStream mem1 = new();
PDFRenderer.Document = PDFDoc;
PDFRenderer.RenderDocument();
PDFRenderer.Save(mem1, closeStream: false);

return new FileStreamResult(mem1, "application/pdf");


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 26, 2024 2:05 pm 
Offline

Joined: Wed Jul 24, 2024 4:42 pm
Posts: 8
Any luck replicating the issue with the code provided?


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 29, 2024 1:15 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3110
Location: Cologne, Germany
I was able to replicate the issue.
Fix should be included with PDFsharp 6.2.0 Preview 1.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 29, 2024 1:26 pm 
Offline

Joined: Wed Jul 24, 2024 4:42 pm
Posts: 8
That is great news. Do you know when the version you mentioned will be available?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group