Replace Field with Table

Hello,
i’m trying to replace a field with a table.
I have a source PDF with fields, and i need to fullfill a field with a table.
I mark the field as hidden, creat a table, and set its top-left.
The X positioning (left) is fine. But the Y (top) is shifted down.
Am i missing something?

Here’s my code. I’ve also attached the “source” PDF.

Thank you.

public byte[] GeneraDocumentoPDF()
{
  byte[] result;

  using (Document documentoPdf = new Document(@"c:\temp\TemplateDocTest_Mid.pdf"))
  {
    var paginaCompila = documentoPdf.Pages.First();
    var fieldCompila = paginaCompila.FieldsInTabOrder.ToList().Find(campo => campo.FullName.Equals("CampoTable1"));
    fieldCompila.Flags = Aspose.Pdf.Annotations.AnnotationFlags.Hidden;

    Table tabellaTesto = new Table();
    paginaCompila.Paragraphs.Add(tabellaTesto);
    tabellaTesto.Left = Convert.ToSingle(Math.Min(fieldCompila.Rect.LLX, fieldCompila.Rect.URX));
    tabellaTesto.Top = Convert.ToSingle(paginaCompila.PageInfo.Height - Math.Max(fieldCompila.Rect.LLY, fieldCompila.Rect.URY));
    tabellaTesto.Rows.Add().Cells.Add("pippo");

    using (var myStream = new MemoryStream())
    {
      documentoPdf.Save(myStream);
      myStream.Position = 0;
      result = myStream.ToArray();
    }
  }
  return result;
}

TemplateDocTest_Mid.pdf (16.6 KB)

@liscio

Please try to honor the margin values as well in the calculations that you are performing for Left and Top coordinates. e.g.

tabellaTesto.Left = Convert.ToSingle(Math.Min(fieldCompila.Rect.LLX, fieldCompila.Rect.URX) - {LeftMargin});
tabellaTesto.Top = Convert.ToSingle(paginaCompila.PageInfo.Height - Math.Max(fieldCompila.Rect.LLY, fieldCompila.Rect.URY) - ({TopMargin}+{BottomMargin}));

In case you still notice any issues, please let us know.

Hello, thanks for the prompt reply.
You mean something like:

        tabellaTesto.Left = Convert.ToSingle(Math.Min(fieldCompila.Rect.LLX, fieldCompila.Rect.URX) - paginaCompila.PageInfo.Margin.Left);
        tabellaTesto.Top = Convert.ToSingle(paginaCompila.PageInfo.Height - Math.Max(fieldCompila.Rect.LLY, fieldCompila.Rect.URY) - (paginaCompila.PageInfo.Margin.Top + paginaCompila.PageInfo.Margin.Bottom));

Doing this way, the table goes completely wrong… (see attachment… the replaced field is the big one on the middle of the page).

provaPdf (20).pdf (7.3 KB)

@liscio

We are checking it. However, just for a clearer understanding, instead of filling form fields, why do you want to place a table on the fields? Can you please share a little bit more of your requirements?

Well… the source pdf i sent you is just a “test”.
In real case we have some pdf documents (with images, logos, rich text formatting, etc…).
We need to fill these pdfs with a set of variable fields, and a dataset containg a list of elements, just like a kind of master-detail “report”. In the list of elements we also need to render barcodes/QRs…
Dataset has always the same structure, but we need to change the source template, based on certain fields.

@liscio

Please use below code snippet in order to render the table at desired location:

using (Document documentoPdf = new Document(dataDir + @"TemplateDocTest_Mid.pdf"))
{
    var paginaCompila = documentoPdf.Pages.First();
    var fieldCompila = paginaCompila.FieldsInTabOrder.ToList().Find(campo => campo.FullName.Equals("CampoTable1"));
    //fieldCompila.Flags = Aspose.Pdf.Annotations.AnnotationFlags.Hidden;
    fieldCompila.Value = "TableHolder";

    Table tabellaTesto = new Table();
    paginaCompila.Paragraphs.Add(tabellaTesto);
    tabellaTesto.Left = Convert.ToSingle(fieldCompila.GetRectangle(true).LLX);
    tabellaTesto.Top = Convert.ToSingle(paginaCompila.GetPageRect(true).Height - fieldCompila.GetRectangle(true).URY);
    tabellaTesto.Rows.Add().Cells.Add("pippo");
    documentoPdf.Save(dataDir + "out.pdf");
}

out.pdf (7.1 KB)

Ok, with the GetPageRect(true) it goes on the correct position.
Thank you very much!

Andrea