Free Support Forum - aspose.com

Add Table in Existing PDF Document using Aspose.PDF for .NET - problem with table breaking and word wrapping

Hi,

I’m trying to add table (Aspose.Pdf.Table) in the existing PDF document, and I faced with several issues:
  • I try to enable word wrapping of a Cell by setting IsWordWrapped = true, but it doesn’t take effect. Please, can anybody helps me to get it work?
  • It is possible to configure the auto-breaking of a table, if it takes more than one pages?

I’m using Aspose.Pdf 7.7.0.

Thanks,

Max

Hi Max,


Thanks for using our products.

Can you please share the code snippet so that we can test the scenario at our end. We are sorry for this inconvenience.

Thanks for reply,

This is my test code:


Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(“Template.pdf”);

int tabHeight = 18 + 18 + 18; // Sum of row heights

Aspose.Pdf.Table table = new Aspose.Pdf.Table(new double[3] { 60, 270, 40 }, new double[3] { 18, 18, 18 }, new Aspose.Pdf.Point(65, pdfDocument.Pages[1].Rect.Height - (tabHeight + 330)));

// Specify the transparency flag of the table background.
table.IsOpaque = true;

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

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

//Set default cell padding using MarginInfo object
table.DefaultCellPadding = new Aspose.Pdf.MarginInfo() { Top = 5f, Left = 5f, Bottom = 5f, Right = 5f };

//Formatting table header
Aspose.Pdf.Text.TextFragment tf1 = new Aspose.Pdf.Text.TextFragment(“Item #”);
tf1.TextState.ForegroundColor = System.Drawing.Color.White;
tf1.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
table.Rows[0].Cells[0].Paragraphs.Add(tf1);
table.Rows[0].Cells[0].BackgroundColor = System.Drawing.Color.Blue;

Aspose.Pdf.Text.TextFragment tf2 = new Aspose.Pdf.Text.TextFragment(“Description”);
tf2.TextState.ForegroundColor = System.Drawing.Color.White;
tf2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
table.Rows[0].Cells[1].Paragraphs.Add(tf2);
table.Rows[0].Cells[1].BackgroundColor = System.Drawing.Color.Blue;

Aspose.Pdf.Text.TextFragment tf3 = new Aspose.Pdf.Text.TextFragment(“Value”);
tf3.TextState.ForegroundColor = System.Drawing.Color.White;
tf3.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
table.Rows[0].Cells[2].Paragraphs.Add(tf3);
table.Rows[0].Cells[2].BackgroundColor = System.Drawing.Color.Blue;

//Set Rows
table.Rows[1].Cells[0].Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“0001”));
table.Rows[1].Cells[1].Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“This it the first item”));
table.Rows[1].Cells[2].Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“AAAA”));

table.Rows[2].Cells[0].Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“0002”));
table.Rows[2].Cells[1].Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“This it the second item / This it the second item / This it the second item / This it the second item / This it the second item”));
table.Rows[2].Cells[1].IsWordWrapped = true;
table.Rows[2].Cells[2].Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(“BBBB”));

pdfDocument.Pages[1].AddTable(table);

pdfDocument.Save(“Result.pdf”);

P.S.: I noticed that IsWordWrap property of cell works properly!
But because of the rows have predefined heights, text will not fit in the cell.

Now my questions are:
- Is it possible to set the LEFT UPPER point of a table?
- Are there any approaches to configure table that will be broken on pages automatically and the cells will be resized depends on content?

Thanks,

Max

nikiforov.maksim:
I noticed that IsWordWrap property of cell works properly!
But because of the rows have predefined heights, text will not fit in the cell.
Hi Max,

Thanks for sharing the details.

I have tested the scenario and have observed that when updating/specifying appropriate row height, the contents inside table cell start appearing. I am glad to hear that you have already figured out this solution.

nikiforov.maksim:
- Is it possible to set the LEFT UPPER point of a table?
When calling overload constructor of Table(…), the third argument Point can be used to set the Upper-Left origin of the table object.
nikiforov.maksim:
- Are there any approaches to configure table that will be broken on pages automatically and the cells will be resized depends on content?
Do you mean that if on subsequent page, if the table cells contains less contents as compare to the size (width,Height) of table cell, the table cells should re-size ? If so is the case, then I am afraid this feature is currently not supported. All the table cells can have appropriate width+height. We are sorry for this inconvenience.
Nayyer,

codewarior:
When calling overload constructor of Table(..), the third argument Point can be used to set the Upper-Left origin of the table object.

I have only the constructor with the position of LEFT LOWER corner of the table as the third parameter... (see attachment).

My end goal is to add a table with dynamic content (number of rows varies depending on user data) to the existing PDF file.
But I want to add a table right at the end of the document and not to create a new page and place a table here so there will be no gaps.
As I understand - I should use Aspose.Pdf.Document and Aspose.Pdf.Table classes.
The issue that I’m dealing with is this: if I create Aspose.Pdf.Table and place a long text in some table cell - the cell doesn't increases its height - so some piece of the inserted text in the cell is missing in the result PDF file. There is no such issue with Aspose.Pdf.Generator.Table class but I cannot use it with while working with the existing PDF file.

Thanks,

Max

nikiforov.maksim:
I have only the constructor with the position of LEFT LOWER corner of the table as the third parameter… .

Hi Max,

Yes the third parameter of Table constructor supports the feature to specify the Lower-Left point of Table but we can get Upper-Left position by adding Row Height value to y parameter to Point object and it will give the Upper-Left position for table object. Though its a bit tricky way to get the Top-Left position, but its the current option that we can use to accomplish the requirement.

nikiforov.maksim:
My end goal is to add a table with dynamic content (number of rows varies depending on user data) to the existing PDF file.
But I want to add a table right at the end of the document and not to create a new page and place a table here so there will be no gaps.
Well in order to fulfill this requirement, to need to parse the contents of PDF page and get the location where last object (Text or Image) is placed and then start the table position after that point.
nikiforov.maksim:
As I understand - I should use Aspose.Pdf.Document and Aspose.Pdf.Table classes.
In order to add table to existing PDF document, you need to use these classes.
nikiforov.maksim:
The issue that I’m dealing with is this: if I create Aspose.Pdf.Table and place a long text in some table cell - the cell doesn’t increases its height - so some piece of the inserted text in the cell is missing in the result PDF file. There is no such issue with Aspose.Pdf.Generator.Table class but I cannot use it with while working with the existing PDF file.

I
have tested the scenario and I am able to reproduce the same problem. For the
sake of correction, I have logged it in our issue tracking system as PDFNEWNET-34934. We
will investigate this issue in details and will keep you updated on the status
of a correction. <o:p></o:p>

We apologize for your inconvenience.

Hi Max,


Thanks for your patience. We have further investigated the issue PDFNEWNET-34934 and have made some progress regarding its resolution. Please try using the following code snippet to resolve this problem.

[C#]

Document pdfDocument = new Document(“c:/pdftest/RichTexBox_out.pdf”);<o:p></o:p>

Aspose.Pdf.Table table = new Aspose.Pdf.Table();

table.Left = 100;

table.Top = 100;

table.ColumnWidths = "60 270 40";

//Set default cell border using BorderInfo object

table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);

//Set table border using another customized BorderInfo object

table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);

//Set default cell padding using MarginInfo object

Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();

margin.Top = 5f;

margin.Left = 5f;

margin.Bottom = 5f;

margin.Right = 5f;

table.DefaultCellPadding = margin;

for (int i = 0; i < 3; i++)

{

Aspose.Pdf.Row row = table.Rows.Add();

for (int j = 0; j < 3; j++)

{ Aspose.Pdf.Cell cell = row.Cells.Add(); cell.BackgroundColor = System.Drawing.Color.Blue; }

}

//Formatting table header

TextFragment tf1 = new TextFragment("Item #");

tf1.TextState.FontStyle = FontStyles.Bold;

table.Rows[0].Cells[0].Paragraphs.Add(tf1);

TextFragment tf2 = new TextFragment("Description");

tf2.TextState.FontStyle = FontStyles.Bold;

table.Rows[0].Cells[1].Paragraphs.Add(tf2);

TextFragment tf3 = new TextFragment("Value");

tf3.TextState.FontStyle = FontStyles.Bold;

table.Rows[0].Cells[2].Paragraphs.Add(tf3);

//Set Rows

table.Rows[1].Cells[0].Paragraphs.Add(new TextFragment("0001"));

table.Rows[1].Cells[1].Paragraphs.Add(new TextFragment("This it the first item"));

table.Rows[1].Cells[2].Paragraphs.Add(new TextFragment("AAAA"));

table.Rows[2].Cells[0].Paragraphs.Add(new TextFragment("0002"));

table.Rows[2].Cells[1].Paragraphs.Add(new TextFragment("This it the second item / This it the second item / This it the second item / This it the second item / This it the second item"));

table.Rows[2].Cells[1].IsWordWrapped = true;

table.Rows[2].Cells[2].Paragraphs.Add(new TextFragment("BBBB"));

pdfDocument.Pages[1].Paragraphs.Add(table);

pdfDocument.Save("c:/pdftest/UpdatedPDFfile.pdf");

The issues you have found earlier (filed as PDFNEWNET-34934) have been fixed in Aspose.Pdf for .NET 7.8.0update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.