We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Reduce Table-Width only when tables exceeds page width


I want to convert MSG files to PDF:
How can I reduce the table width to the document width - only when the table width exceeds the page width?


[.Net 3.5]

Hi Bea,

Thanks for your inquiry. The shared zip file in this forum thread has 0kb file size. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input MSG file.
  • Please attach the output Pdf that shows the undesired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

I attached the files

Hi Bea,

Thanks for sharing the detail. In your document, the width of table is 474.75 pt which is less than default page size. However, due to the large size image in your table, it is rendering outside the page.

In this case, we suggest you please calculate the table’s width using Aspose.Words.Layout APIs. Please use the following code example to get the desired output. Hope this helps you.

Aspose.Email.Mail.MailMessage msg = Aspose.Email.Mail.MailMessage.Load(MyDir + "EMail_2016-78.msg");
MemoryStream StreamMHT = new MemoryStream();
msg.Save(MyDir + "Out.mhtml");
StreamMHT.Position = 0;
Aspose.Words.LoadOptions lLoadOptions = new Aspose.Words.LoadOptions();
lLoadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;
Document doc = new Document(StreamMHT);
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
// Calculate the table's width
// Insert bookmark in the last cell of table's first row and 
// get its position using Aspose.Words.Layout APIs
Paragraph p = table.FirstRow.LastCell.LastParagraph;
p.AppendChild(new BookmarkStart(doc, "bookmark"));
p.AppendChild(new BookmarkEnd(doc, "bookmark"));
ParagraphAlignment palignment = p.ParagraphFormat.Alignment;
p.ParagraphFormat.Alignment = ParagraphAlignment.Right;
Bookmark bm = doc.Range.Bookmarks["bookmark"];
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
var renderObject = collector.GetEntity(bm.BookmarkStart);
layoutEnumerator.Current = renderObject;
RectangleF location1 = layoutEnumerator.Rectangle;
RectangleF location2 = layoutEnumerator.Rectangle;
double tablewidth = location1.X - location2.X;
if (tablewidth > doc.FirstSection.PageSetup.PageWidth)
    p.ParagraphFormat.Alignment = palignment;
    table.PreferredWidth = PreferredWidth.FromPercent(100);
doc.Save(MyDir + "Out v16.12.0.pdf");

Thanks for the reply.

How can I iterate through ALL tables in a document? And do I really need the Bookmarks and LayoutEnumerator?

Can’t I just get the table width and compare it to the page width?

Ok, I see…there is reallly really NO way to get the actual width of a table??

Hi Bea,

Thanks for your inquiry. You can get the table’s width using Aspose.Words. Please refer to the following article:
Specifying Table and Cell Widths

In your case, the table renders outside the page due to its size. Please open the output MHTML in MS Word and remove the big size image. You will then get the exact table width. In your case, you need to calculate the table’s width as shown in my previous reply.

Please let us know if you have any more queries.

I tried your code solution - I changed the code to a more generic approach, it works BUT only if I update the page layout of the document:


Hi Bea,

Thanks for your feedback. It is nice to hear from you that your problem has been solved. In your case, you need to call Document.UpdatePageLayout method before saving document to PDF.

Please let us know if you have any more queries.