Differences between tables printed to .doc vs .tif

I’m trying to write out a document as an image and as a Word document. If you compare the multi-page tif with the single page Word document attached, you’ll see the differences. It looks like the image printing (using SaveToImage) is corrupting the table.

Any ideas to work around this problem at the moment, or what might be causing the layout issues? Tips on getting the Word document to display more clearly also appreciated.

Hello

Thanks for your request. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is fixed. As a workaround you can try calling UpdateTableLayout() before converting to image or open/save your document using MS Word.

Best regards,

Thanks,

I tried calling UpdateTableLayout() before saving to an image but this had no effect. Did it work for you when reproducing the problem?

Hello

Thank you for additional information. Yes it works fine on my side. I use the following code for testing:

Document doc = new Document(@"Test\Sample.doc");
doc.UpdateTableLayout();
// Save output document.
doc.SaveToImage(0, doc.PageCount, @"Test\out.tif", null);

I send the output TIF file to your e-mail.

Also I use the latest version of Aspose.Words 9.1.0.

Best regards,

Thanks, this gave me a clue for a workaround.

Our code is actually more complex than the example I posted above, as the document is initially generated from a Word template, which we manipulate before writing to an image file stream. Simply calling UpdateTableLayout against our generated document had no effect. However, writing to a doc file (the one I attached above) into a memory stream, then opening that in a fresh Aspose.Words document instance, then calling UpdateTableLayout against that before writing out the images, seems to get around the issue.

Here is roughly the code I am using instead of my original call to document.SaveToImage:

using (var ms = new MemoryStream())
{
    document.Save(ms, SaveFormat.Doc);
    ms.Seek(0, SeekOrigin.Begin);

    var dummyDoc = new Document(ms);
    dummyDoc.UpdateTableLayout();
    dummyDoc.SaveToImage(0, dummyDoc.PageCount, stream, ImageFormat.Tiff, imageOptions);
}

This appears to render more reliably.

Could you recommend a way for me to export the initial Aspose document in a format which will preserve the corruption, and possibly help you to resolve the issue fully? Or do you have a good idea about what would cause this problem already, which could be fixed in a later version of Aspose.Words?

Thanks again

Calling UpdateTableLayout caused some layout problems in a second template with an image inside a table (the image got moved to the left cut off, as if the table had been sized too narrow to accommodate the image) so I experimented some more with the problem.

I have found that with this code, the table layout corruption can be reproduced:

using (var ms = new MemoryStream())
{
    document.Save(ms, SaveFormat.Doc);
    ms.Seek(0, SeekOrigin.Begin);

    var dummyDoc = new Document(ms);
    dummyDoc.UpdatePageLayout(); // new line, breaks layout
    dummyDoc.UpdateTableLayout();
    dummyDoc.SaveToImage(0, dummyDoc.PageCount, stream, ImageFormat.Tiff, imageOptions);
}

Adding the call to UpdatePageLayout seems to stop UpdateTableLayout from doing anything. Is there anything I have to do before the call to UpdateTableLayout to make sure that images are respected?

Thanks again

Hello

Thank you for additional information. Could you please create simple application, which will allow me to reproduce the problem on my side? I will check it and provide you more information.

Best regards,

Hi Andrey,

Here is an example application.

It takes two input files, Letter.doc and Table.doc, from the InputDocuments folder, and writes out four files to OutputDocuments. I’ve included the output that I get in the MyOutput folder.

Table.doc was originally written out by Aspose, Letter.doc was just output by Word after loading the template. In our application, we would populate the merge field before writing the final document to .doc and .tiff.

In the output, the multi-page Table.tif demonstrates the table layout problem I’m having, and Table-UpdateTableLayout.tif demonstrates the table layout being fixed using your suggestion to call UpdateTableLayout. It isn’t perfect, too many columns, not enough spacing, but that is more the fault of our template than anything to do with Aspose :slight_smile:

So, fixing the layout of that table is great. Unfortunately, Letter.tif demonstrates the problem I have with some other templates when I call UpdateTableLayout. Letter.tif is fine, but if you compare to Letter-UpdateTableLayout.tif, you’ll see that the logo in the top right gets cut off. It looks like that cell has been resized to fit the text, ignoring the image size.

So, where I am at the moment, is that I’m using a hack to decide whether to call UpdateTableLayout or not. However, it isn’t a good hack, since the templates are end-user configurable, so it would be perfectly possible for a user to set up a template with a header like Letter.doc, but a table like Table.doc, so I’m looking for a solution which works in both cases.

Thanks for looking into this.

Hi

Thank you for additional information. Actually calling UpdatePageLayout for each your documents it is not good idea. If you open/save Table.doc using MS Word and then generate image using Aspose.Words, the output image will look fine. You wrote that Table.doc was originally written out by Aspose.Words, so I think the original problem is inside generating code. Could you please provide me the code which you are using to generate this document?

Best regards,

The code which actually inserts the table is called in AsposeMergingImp.cs around line 122.

Let me know if you have any problems interpreting the code.

Will

Hi

Thanks for your request. The problem occurs because you specify width of each cell and simultaneously use AlowAutoFit option. In your case, this corrupts the table. If you remove the following few lines of code, the table will look correct in out DOC file as well as in output TIF:

if (cell.Width.HasValue)
{
    builder.CellFormat.Width = cell.Width.Value;
}

You can find this snippet of code in WriteComponent method.

Hope this helps. Please let me know if you need further assistance, I will be glad to help you.

Best regards.

Thanks Alexey,

Removing the lines as you suggested does fix the .TIF. However, we now have a couple of problems:

  1. The table now overflows the boundaries of the page on the right hand side.
  2. We would like to prevent the “Drug” column from wrapping in the middle of words.

The autofit algorithm appears to simply distribute the page width across the number of columns and not respect the actual content of them?

I have attached the new output.

Hi

Thank you for additional information. You can also avoid using AllowAutoFit and specify width of cells explicitly, but in this case, width of each cell should be correct.

You should note, that rows in Ms Word tables are completely independent, i.e. each row can contain any number of cells of any width. So if width of cells in each row will be different this can break the table formatting.

Best regards.