Вот упрощенная версия моего кода:
Код: Выделить всё
using (var templateReader = new PdfReader(_env.ContentRootPath + "\\templates\\" + single.Tipo + ".pdf"))
using (var templateDoc = new PdfDocument(templateReader))
{
PdfPage templatePage = templateDoc.GetPage(1);
PdfFormXObject templateXObject = templatePage.CopyAsFormXObject(pdfDoc);
canvas.BeginText();
canvas.MoveText(120, 664);
canvas.ShowText($"{single.Numero} / {single.Data.Year.ToString()}");
canvas.EndText();
canvas.BeginText();
canvas.MoveText(40, 756);
canvas.ShowText(single.Data.ToString("dd/MM/yyyy"));
canvas.EndText();
canvas.BeginText();
canvas.MoveText(500, 800);
canvas.ShowText("ID#" + single.Id.ToString());
canvas.EndText();
Table table1 = new Table(1).SetWidth(526);
var righe = single.Righe.ToList();
AddPreventivoTable(table1, righe);
float rectHeight = 595;
var altezzaRighe = CalculateRowHeights(table1);
var table1Height = altezzaRighe.Sum();
int pageCount = (int)Math.Ceiling(table1Height / rectHeight);
for (int i = 0; i < pageCount; i++)
{
if (i > 0)
{
pdfDoc.AddNewPage();
var page = pdfDoc.GetPage(i + 1);
canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
var bf = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
canvas.SetFontAndSize(bf, 10);
canvas.SetColor(ColorConstants.BLACK, true);
canvas.AddXObject(templateXObject);
AddIntestazione(canvas, ana);
canvas.BeginText();
canvas.SetTextMatrix(120, 664);
canvas.ShowText($"{single.Numero} / {single.Data.Year.ToString()}");
canvas.EndText();
canvas.BeginText();
canvas.SetTextMatrix(40, 756);
canvas.ShowText(single.Data.ToString("dd/MM/yyyy"));
canvas.EndText();
canvas.BeginText();
canvas.SetTextMatrix(500, 800);
canvas.ShowText("ID#" + single.Id.ToString());
canvas.EndText();
}
Rectangle rect1 = new iText.Kernel.Geom.Rectangle(34, 60, 526, rectHeight);
var canvasRect1 = new Canvas(canvas, rect1);
Table subTable = new Table(1).SetWidth(526);
float currentHeight = 0;
int currentRow = 0;
while (currentRow < table1.GetNumberOfRows())
{
float cellHeight = altezzaRighe[currentRow];
if (currentHeight + cellHeight > rectHeight)
{
break;
}
var cell = table1.GetCell(currentRow, 0).Clone(true);
subTable.AddCell(cell);
currentHeight += cellHeight;
currentRow++;
}
canvasRect1.Add(subTable);
canvasRect1.SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 0.5f));
canvas.Stroke();
canvasRect1.Close();
}
}`
Как правильно скопировать содержимое исходных ячеек и добавить их в новую таблицу, не встретив этой ошибки? Любая помощь или предложения будут очень признательны!
Подробнее здесь: https://stackoverflow.com/questions/792 ... text7-in-c