Hello Aspose,
We are considering to use your pdf library for exporting our grid data. Can you help to find a way how to trim text inside table cell to end it with ellipsys (…) in case of overflow
Please clarify which technique you use - HTML-to-PDF or rendering via Table class.
If possible, please provide your sample code.
I am using Table class
private static Cell CreateCell(string text) {
var cell = new Cell { Margin = new MarginInfo(5, 5, 5, 5), Alignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center };
cell.Paragraphs.Add(new TextFragment(text) { TextState = { FormattingOptions = new TextFormattingOptions { WrapMode = TextFormattingOptions.WordWrapMode.NoWrap } } });
return cell;
}
@sik We don’t have a final solution for your question right now. I want to propose a few workarounds to you.
Here is my snippet for creating a demo table. Please notice that we need to define column size.
static void Main()
{
var licensePath = @"Aspose.PDF.NET.lic";
new Aspose.Pdf.License().SetLicense(licensePath);
// Load the PDF file
Document document = new();
// Initializes a new instance of the Table
Page page = document.Pages.Add();
Aspose.Pdf.Table table = new()
{
// Set the table border color as LightGray
Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)),
// Set the border for table cells
DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)),
ColumnWidths = "90 100 90"
};
// Create a loop to add 10 rows
for (int row_count = 1; row_count < 10; row_count++)
{
// Add row to table
Aspose.Pdf.Row row = table.Rows.Add();
// Add table cells
row.Cells.Add("Column (" + row_count + ", 1)");
row.Cells.Add("Column (" + row_count + ", 2)");
row.Cells.Add(CreateCell("Column long text Column long text (" + row_count + ", 3)"));
}
// Add table object to first page of the input document
page.Paragraphs.Add(table);
// Save the updated document
document.Save("document_with_table_out.pdf");
//Process.Start("document_with_table_out.pdf");
}
The first idea is to use HtmlFragment instead of TextFragment. In this case you can use text-overflow: clip
CSS property. But now we don’t support ellipsis
style.
private static Cell CreateCell_1(string text)
{
var cell = new Cell
{
Margin = new MarginInfo(5, 5, 5, 5),
Alignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center
};
var content = $"<p style=\"height: 36px; white-space: normal; overflow: hidden; text-overflow: clip;\">{text}</p>";
var textFragment = new HtmlFragment(content);
cell.Paragraphs.Add(textFragment);
return cell;
}
Yet another idea is to calculate the length of the string and transform the string manually by trimming and adding an ellipsis.
private static Cell CreateCell(string text)
{
var cell = new Cell
{
Margin = new MarginInfo(5, 5, 5, 5),
Alignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center
};
var textFragment = new TextFragment(text);
for (int i = 0; i < text.Length; i++)
{
if (textFragment.TextState.MeasureString(textFragment.Text) < (90-5-5))
{
break;
}
textFragment.Text = $"{text[..^i]}…";
}
cell.Paragraphs.Add(textFragment);
return cell;
}
If these methods are not suitable for you, please let me know. I will create a task for the dev team.
The first proposal looks promissing but I have an exception Cannot find table ‘OS/2’ in the font file, also I trully would like to have ellipsis instead of clip.
The second proposal looks low performant, I have a huge dataset, I think doing this for each cell would be very slow
@sik
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFNET-58083
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
I am not setting any particular font, it just crashes with default settings
I got you, but this error is related to font, and we need to know which font has been used.
So, please add more details: OS, .NET version, Aspose.PDF version. Do you use Aspose.PDF or Aspose.PDF.Drawing library?
Also, if possible, please add font-family: sans-serif;
to the style in the example above to check if the error still exists.
Can you tell me how to do this, I have this
image.png (21.6 KB)
If I set “Verdana” to font family, then font is found and I have the previous exception
Please check the installed font in your system and see if you are specifying its name in the code correctly? Looks like the font may be present in the system with different name.
It is not, font is fine and working good until using HtmlFragment. The thing is when using known system font it crashes with this System.InvalidOperationException: ‘Cannot find table ‘OS/2’ in the font file.’
If possible, can you please share a code sample that we can use to test and replicate the issue in our environment. We will further proceed to assist you accordingly.
Here is whole source code
Attached is the output that we obtained in our environment using 24.9 version of the API and your code sample. We reduced the page count and rows count for testing purposes however.
TableOutput.pdf (105.3 KB)
We did not notice any exceptions in our environment.