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");
|