Add a Dynamic Table to a Editable PDF

Hi,

I have a PDF which has some static text and some dynamic text fields. I want to get the objects in a page and fill the data in the dynamic text fields where ever required. Apart from these dynamic text fields i want to add a table in the Aspose.PDF which has to be generated dynamically using .Net Datatable.This table may contain some n number of rows.Once the page limit is exceeded the rows should get displayed in the next page. Can anyone share the sample code snippet which suits the above scenario. Thanks in advance!

Hi Suchitra,


Thanks for your interest in our products.

I am pleased to share that Aspose.Pdf for .NET supports the feature to add table in existing PDF document. For further details, please visit Add Table in Existing PDF Document

In the event of any further query, please feel free to contact.

Thanks Nayyer for the reply. But the above code is not working. As i am using Aspose 7.3 version. Will this be possible with this version.

Hi,

Anyone know how to add dynamic pages to the existing PDF document. I have made many trials but i haven't seen the output yet. Currently am using 7.3 version Aspose dll. Below is the code snippet i have tried to add a page to the existing PDF. But this is not working. Am I missing anything in the below code. Please do the needful.

Aspose.Pdf.License license = new Aspose.Pdf.License();
////license.SetLicense(System.Web.HttpContext.Current.Server.MapPath("/AsposeTotal/Aspose.Total.lic"));
license.SetLicense(@"Aspose.Total.lic");
string strFilePath = @"Test.pdf";
Document pdfDocument = new Document(strFilePath);
//insert a empty page in a PDF
Aspose.Pdf.Page page = pdfDocument.Pages.Add();

// Add a 4th page in the existing PDF
pdfDocument.Pages.Insert(4, page);
//save output file
pdfDocument.Save("output.pdf");

Suchitra SS: But the above code is not working. As i am using Aspose 7.3 version. Will this be possible with this version.

Hi Suchitra,

The support for adding a table to an existing PDF document was added in Aspose.Pdf for .NET 7.1.0 release. However, in later versions, we have added some more features into this functionality and have added a few more methods/properties. Please try using the following code snippet as during my testing with the latest release of Aspose.Pdf for .NET 8.2.0, the table is properly being added to the PDF document.

// Instantiate the Document object by calling its empty constructor
Document doc = new Document("c:/pdftest/LineHeight_HTML2pdf.pdf");

// add table on first page
Page page = doc.Pages[1];

// Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();

// Add the table in paragraphs collection of the desired section
page.Paragraphs.Add(tab1);

// Set with column widths of the table
tab1.ColumnWidths = "50 50 50";

// Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);

// Set table border using another customized BorderInfo object
tab1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);

// Create MarginInfo object and set its left, bottom, right, and top margins
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
margin.Top = 5f;
margin.Left = 5f;
margin.Right = 5f;
margin.Bottom = 5f;

// Set the default cell padding to the MarginInfo object
tab1.DefaultCellPadding = margin;

// Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = tab1.Rows.Add();
row1.Cells.Add("col1");
row1.Cells.Add("col2");
row1.Cells.Add();

TextFragment mytext = new TextFragment("col3 with large text string");
row1.Cells[2].Paragraphs.Add(mytext);
row1.Cells[2].IsWordWrapped = false;

Aspose.Pdf.Row row2 = tab1.Rows.Add();
row2.Cells.Add("item1");
row2.Cells.Add("item2");
row2.Cells.Add("item3");

// Save the Pdf
doc.Save("c:/pdftest/33917.pdf");

P.S. We always recommend our customers to try using the latest release version, and now concerning the code snippet specified in the earlier link, I am working on updating it according to the latest release. We are really sorry for this inconvenience.

Suchitra SS:
The above code is not working. As i am using Aspose 7.3 version. Will this be possible with this version.
Hi Suchitra,

Thanks for your patience.

The contents/code on Add Table in Existing PDF Document link have been updated and during my testing with Aspose.Pdf for .NET 8.2.0, the table is properly being added to PDF file. In case you still face any issue or you have any further query, please feel free to contact.

Suchitra SS: Anyone know how to add dynamic pages to the existing PDF document. I have made many trials but I haven’t seen the output yet. Currently am using 7.3 version Aspose dll. Below is the code snippet I have tried to add a page to the existing PDF. But this is not working. Am I missing anything in the below code. Please do the needful.

Hi Suchitra,

In order to add a blank page to an existing PDF file, please try using the following code snippet. During my testing with the latest release of Aspose.Pdf for .NET 8.2.0, the code snippet is working fine. Since you have been using a quite old version, I would recommend you to please try using the latest release version. In case the problem still persists, please share the source PDF file. We are sorry for this inconvenience.

[C#]

// Load source PDF document
Aspose.Pdf.Document doc = new Aspose.Pdf.Document("input.pdf");

// add a blank page to PDF file
doc.Pages.Add();

// Save updated document containing table object
doc.Save("Blank_page_added.pdf");

Or you may consider using the following code snippet to accomplish a similar requirement.

[C#]

// Load source PDF document
Aspose.Pdf.Document doc = new Aspose.Pdf.Document("input.pdf");

// get current page count of document and add new page after last page
doc.Pages.Insert(doc.Pages.Count + 1);

// Save updated document containing table object
doc.Save("blank_page_added.pdf");

Hi Nayyer,

Sorry for the delay in replying. The above code is working fine to me.

Here i have a question. How can i add a TextFragment dynamically.

Ex: for(int i=0;i<datatable.rows.count; i++)

{

var textfragment = new TextFragment(datatable.Rows[i][0].ToString()) { Position = new Position(108, 400) };

textfragment .TextState.FontSize = 10;

textfragment .TextState.Font = FontRepository.FindFont("TimesNewRoman");

textBuilder.AppendText(textfragment);

}

When i implement the above logic am able to add only one fragment to the page.

Is there is any way to implement.

Thanks in advance.

Hi Nayyer,

Can you please tell me how to add RowHeight and Columnheight for a table. As i am using Aspose.PDf 7.3 version.

Here is my sample code.

Aspose.Pdf.Table table = new Aspose.Pdf.Table(Convert.ToInt32(7), 5, rect)
{
IsOpaque = true,
BorderColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)
RowHeights = 30;
};

Thanks in advance.

Hi there,


Thanks for your inquiry. Please check following code snippet to set column width and row height using DOM approach. Hopefully it will help you to fulfill your requirements. Please note Point is measuring unit of Aspose.Pdf and 72 Points= I inch.

Aspose.Pdf.Table table = new Aspose.Pdf.Table();
table.ColumnWidths = “25 25”;
Aspose.Pdf.Row row = table.Rows.Add();
row.FixedRowHeight = 14;
----
----


Please feel free to contact us for any further assistance.

Best Regards,