Home > database >  iText7: Adding a PDF inside a Cell (cutoff at end of page)
iText7: Adding a PDF inside a Cell (cutoff at end of page)

Time:02-16

We are creating a PDF, it contains a large table, with headers (bold), sub-headers (non bold), rows and cells. enter image description here

One of the requirements is to add an already existing PDF (can be multiple pages) inside the cell (under the sub-header). And this is where we are struggling. What we have tried so far:

Converting the PDF to a FormXObject, and adding that into the cell.

FontProvider fontProvider = new FontProvider();
ConverterProperties props = new ConverterProperties();
props.SetFontProvider(fontProvider);

var historyId = text.Replace("=Spectec_template", "");

var pdfTemplate = templateToPdfClient.GetPdf(historyId, "HI").Result;
PdfDocument srcDoc = new PdfDocument(new PdfReader(pdfTemplate.FileStream));
for (int i = 1; i < srcDoc.GetNumberOfPages()   1; i  )
{
    Cell newCell = new Cell(1, columnInfo.ColumnSpan).SetFont(PdfFontFactory.CreateFont(_fontName));
    PdfPage origPage = srcDoc.GetPage(i);

    Rectangle rect = origPage.GetPageSize();
    PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
    Image image = new Image(pageCopy);
    image.SetBorder(Border.NO_BORDER);
    image.SetMaxWidth(UnitValue.CreatePercentValue(100));
    image.SetMaxHeight(UnitValue.CreatePercentValue(50));

    Div div = new Div();
    div.Add(image.SetMaxWidth(UnitValue.CreatePercentValue(100)));

    newCell.Add(div);
    newCell = ApplyLayoutsToCell(newCell, cellStyles);
    table.AddCell(newCell);
}

But the end result is that we only see 1 page and it's completely overlapping at the end of the page. enter image description here

When we set image.SetAutoScale(true); the PDF is visible but it's extremely small. When adding the Image to a Paragraph instead of a DIV the PDF pages display next to each other.

Any ideas, suggestions?

Thank you

CodePudding user response:

Thanks to @mkl and @KJ I figured this out.

I ended up adding each PDF page in a new Cell, and setting the scaling of image to 0.6f.

for (int i = 1; i < srcDoc.GetNumberOfPages()   1; i  )
{
    newCell = new Cell(1, columnInfo.ColumnSpan).SetFont(PdfFontFactory.CreateFont(_fontName));

    PdfPage origPage = srcDoc.GetPage(i);

    PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
    Image image = new Image(pageCopy);
    image.SetBorder(Border.NO_BORDER);
    image.Scale(0.6f, 0.6f);

    newCell.Add(image);
    newCell = ApplyLayoutsToCell(newCell, cellStyles);

    table.AddCell(newCell);
}
  • Related