using iText.Kernel.Pdf.Canvas;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Layout;
using iText.Layout.Properties;
static void Main(string[] args)
Console.WriteLine("PRUEBA 2");
using (var memoryStream = new MemoryStream())
var pdfWriter = new PdfWriter(memoryStream);
var pdfDoc = new PdfDocument(pdfWriter);
var pageSize = PageSize.LEGAL;
var document = new Document(pdfDoc, pageSize);
var loremIpsum = new Paragraph("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi " +
"ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " +
"in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " +
"officia deserunt mollit anim id est laborum.");
var content_table_folio = new Table(1);
for (int i = 0; i < 20; i++)
content_table_folio.AddCell(loremIpsum);
var result = content_table_folio.CreateRendererSubTree().SetParent(document.GetRenderer()).Layout(new LayoutContext(new LayoutArea(0, new Rectangle(0, 0, 400, 10000.0f))));
var tablaAltura = result.GetOccupiedArea().GetBBox().GetHeight();
var paginaAltura = pageSize.GetHeight();
Console.WriteLine($"Altura PAGINA: {tablaAltura}");
Console.WriteLine($"Altura TABLA: {paginaAltura}");
if (tablaAltura > paginaAltura)
int numPaginasNecesarias = (int)Math.Ceiling(tablaAltura / paginaAltura);
Console.WriteLine($"Cantidad de páginas necesarias: {numPaginasNecesarias}");
for (int i = 0; i < numPaginasNecesarias; i++)
var startIndex = i * (int)Math.Floor((double)(content_table_folio.GetNumberOfRows() / numPaginasNecesarias));
var endIndex = (i + 1) * (int)Math.Floor((double)(content_table_folio.GetNumberOfRows() / numPaginasNecesarias)) - 1;
Console.WriteLine($"Para está se comienza en el índice: {startIndex} y finaliza en el índice: {endIndex}");
if (i == numPaginasNecesarias - 1)
endIndex = content_table_folio.GetNumberOfRows() - 1;
var partialContentTable = new Table(1);
for (int j = startIndex; j <= endIndex; j++)
partialContentTable.AddCell(content_table_folio.GetCell(j,0));
var page = pdfDoc.AddNewPage();
dibujarHoja(pdfDoc, page)
.Add(partialContentTable);
var page = pdfDoc.AddNewPage();
dibujarHoja(pdfDoc, page)
.Add(content_table_folio);
byte[] pdfBytes = memoryStream.ToArray();
var base64String = Convert.ToBase64String(pdfBytes);
Console.WriteLine(Environment.NewLine);
Console.WriteLine(base64String);
protected static Canvas dibujarHoja(PdfDocument pdfDocument, PdfPage page)
var TamanioPagina = page.GetPageSize();
var pdfcanvas = new PdfCanvas(page);
var rectangulo = TamanioPagina == PageSize.LEGAL ? new Rectangle(179, 39, 375, 898) : new Rectangle(179, 60, 360, 725);
var canvas = new Canvas(page, rectangulo);
var datosActa = new Table(3)
.SetFixedPosition((float)Math.Round(width_a * 10.15 / 100), (float)Math.Round(page.GetPageSize().GetHeight() * 642 / 100 - 312), 112)
.SetTextAlignment(TextAlignment.CENTER)
.SetBorder(new SolidBorder(SolidBorder.SOLID))
.AddCell(new Paragraph($"1 A").SetPaddings(8, 0, 8, 0))
.AddCell(new Paragraph("123456").SetPaddings(8, 0, 8, 0))
.AddCell(new Paragraph(DateTime.Now.Year.ToString()).SetPaddings(8, 0, 8, 0))
.AddCell(new Paragraph("Tomo").SetFontSize(8).SetPadding(4).SetCharacterSpacing(1))
.AddCell(new Paragraph("Acta").SetFontSize(8).SetPadding(4).SetCharacterSpacing(1))
.AddCell(new Paragraph("Año").SetFontSize(8).SetPadding(4).SetCharacterSpacing(1));
pdfcanvas.MoveTo(Math.Round(page.GetPageSize().GetWidth() * 9.48 / 100), Math.Round(page.GetPageSize().GetHeight() * height_b / 100))
.LineTo(Math.Round(page.GetPageSize().GetWidth() * 90.52 / 100), Math.Round(page.GetPageSize().GetHeight() * height_b / 100))
.LineTo(Math.Round(page.GetPageSize().GetWidth() * 90.52 / 100), Math.Round(page.GetPageSize().GetHeight() * height_a / 100))
.LineTo(Math.Round(page.GetPageSize().GetWidth() * 9.48 / 100), Math.Round(page.GetPageSize().GetHeight() * height_a / 100))
.LineTo(Math.Round(page.GetPageSize().GetWidth() * 9.48 / 100), Math.Round(page.GetPageSize().GetHeight() * height_b / 100))
.MoveTo(Math.Round(width_a * 29.25 / 100), Math.Round(page.GetPageSize().GetHeight() * height_b / 100))
.LineTo(Math.Round(width_a * 29.25 / 100), Math.Round(page.GetPageSize().GetHeight() * height_a / 100))
.MoveTo(Math.Round(page.GetPageSize().GetWidth() * 9.48 / 100), Math.Round(page.GetPageSize().GetHeight() * height_a / 100 - 45))
.LineTo(Math.Round(width_a * 29.25 / 100), Math.Round(page.GetPageSize().GetHeight() * height_a / 100 - 45))
.MoveTo(Math.Round(page.GetPageSize().GetWidth() * 9.48 / 100), Math.Round(page.GetPageSize().GetHeight() * height_c / 100))
.LineTo(Math.Round(width_a * 29.25 / 100), Math.Round(page.GetPageSize().GetHeight() * height_c / 100))