Hello, I am trying to make text NOT wrap within a table cell, after being converted to a PDF from a Word document.
I am using:
builder.CellFormat.WrapText = false;
The Word Doc is created from scratdch, and it looks correct in the Word Document, but when converted to a PDF the text within the table wraps.
I think the issue happens when I use:
table.AutoFit(AutoFitBehavior.AutoFitToContents);
I want this table to resize based on the contents. This works in Word, but again when converted to PDF it wraps all the cells and makes the table width as small as possible.
I played around with the following properties, but no variation of these seem to change the issue with the PDF wrapping text.
table.AllowAutoFit = true;
table.PreferredWidth = PreferredWidth.Auto;
builder.CellFormat.PreferredWidth = PreferredWidth.Auto;
Is there a way to do this with keeping the AutoFitToContents setting on the table?
Thank You.
@martincoty77
To prevent text from wrapping within a table cell when converting a Word document to PDF using Aspose.Words, you can follow these steps:
-
Disable Text Wrapping: Ensure that you set the WrapText
property of the cell format to false
. This should be done for each cell where you want to prevent text wrapping.
builder.CellFormat.WrapText = false;
-
AutoFit Behavior: If you are using table.AutoFit(AutoFitBehavior.AutoFitToContents)
, it may cause the table to resize based on the content, which can lead to unexpected wrapping behavior. Instead, consider using AutoFitBehavior.AutoFitToWindow
or AutoFitBehavior.FixedColumnWidths
if you want to maintain a specific width without wrapping.
-
Cell Formatting: You can also set the PreferredWidth
of the cells to ensure they maintain a specific width. For example:
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(100); // Set to desired width
-
Allow AutoFit: Make sure that AllowAutoFit
is set to false
if you want to prevent the table from automatically adjusting its size based on the content.
table.AllowAutoFit = false;
-
Finalizing the Document: After making these adjustments, save the document to PDF format. This should help in maintaining the desired layout without text wrapping.
Here is a sample code snippet that incorporates these suggestions:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a table
Table table = builder.StartTable();
builder.InsertCell();
builder.CellFormat.WrapText = false; // Disable text wrapping
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(100); // Set preferred width
builder.Write("This is a long text that should not wrap.");
builder.InsertCell();
builder.Write("Another cell.");
builder.EndRow();
builder.EndTable();
// Set table properties
table.AllowAutoFit = false; // Prevent auto-fit
table.AutoFit(AutoFitBehavior.FixedColumnWidths); // Use fixed widths
// Save to PDF
doc.Save("output.pdf");
By following these steps, you should be able to control the text wrapping behavior in your table cells when converting to PDF. If you continue to experience issues, consider checking the specific content and formatting of your Word document, as certain styles or settings may also influence the final output.
@martincoty77 Could you please provide runnable code that will allow us to reproduce the problem and attach your DOCX and problematic PDF output documents? We will check the issue and provide you more information.
Thanks for the info. My end goal is to not use fixed width, but a table that just fits to the content. I am a little confused if all or which of these properties are required.
table.AutoFit(AutoFitBehavior.AutoFitToContents);
table.AllowAutoFit = true;
table.PreferredWidth = PreferredWidth.Auto;
builder.CellFormat.PreferredWidth = PreferredWidth.Auto;
Hello Alexey,
I put together a console app and I could not replicate the issue until I copied the exact text i was inserting in the cell. I tried to upload but it says it was too large. So I just uploaded the 2 cs files used with the code.
WordWrapIssue.zip (2.1 KB)
And here is an example PDF:
NewFileName.pdf (27.7 KB)
I figured out the issue though. The text I am inserting is “Initial Contract:” and I thought it was something with the colon, but it might actually be with the number of characters. But testing other characters with the same number worked. So could be the cumulative length of the characters? I don’t know it is really odd. With only changing the text of the cell being inserted, here were the results:
THIS TEXT WRAPS:
“Initial Contract:”
“Initial ContractThis:”
“Initial Contracts”
THIS TEXT DOES NOT WRAP:
“Initial Contract”
“Initial Contract Test No Wrap”
“Initial Contract: Test”
“First ContractThis:”
“1234567 123456789”
@martincoty77 Thank you for additional information. Unfortunately, I cannot reproduce the problem on my side using the latest 25.4 version of Aspose.Words. Here are output documents produced on my side:
out.docx (7.6 KB)
out.pdf (27.7 KB)
In addition, I see you are using a deprecated Document.UpdateTableLayout
method. UpdateTableLayout
method was the first attempt of table grid calculation algorithm implementation. Currently another implementation is used internally by Aspose.Words, so normally there is no need to call additional methods to calculate table layout.
Thanks for testing. I am using version 23.4.0 currently, I’ll test with latest.
I have used the UpdateTableLayout method before (I think it was commented out in the files I gave you). But I had tried to use it for this issue and sometimes it actually fixes an issue, but I really try not to use it since it is tagged as deprecated.
Thanks Alexey,
I can confirm also this issue does not exist on the latest version. I took a look at our lic and see we were behind a few versions and was able to update to version 24.5. It is fixed in this version as well, so I’ll see if I can get our versions updated within the project. Appreciate the help! Thanks!
1 Like