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

Free Support Forum - aspose.com

AutoFit a Tabel with Aspose.Words

Hi,

I generate Aspose documents over xslt and a data-file. The result is a wordml structue. I go this way, because I don't use Microsoft Word for rendering, printing and exporting. Now the problem:

This document contains a table. Over the xslt this table has normally the AutoSize (AutoFit) property. If you save the document as file (as doc-format) and open this file with Mircosoft Word everything is fine.

But if you directly export this file as pdf , the table is not very nice.

Now the question: Set the property AutoFit on the table-object only a property? A property, so that can Microsoft Word render the document correct? Or make I a mistake? At the moment I calculate the cell width dynamically. But this is sometimes inexactly.

thanks and regards

Jörg Michel

Hi
Jörg,


Thanks for your inquiry. Yes sure, you can use Table.AutoFit method to resize the table and cells according to the specified auto fit behavior before saving document to PDF format. This method mimics the commands available in the Auto Fit menu for a table in Microsoft Word.

Please let me know if I can be of any further assistance.

Best Regards,

Hi Awais,

I think that the feature isn't perfectly. Therefore I attach a sample. I have a sourcefile with a table. I load this file into a Aspose document object. I set the property on the table and save the file as pdf an doc. Than you can see the result.

Here is the sample code:

private void createPDF_Click(object sender, RoutedEventArgs e)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

License lic = new License();

lic.SetLicense(System.IO.Path.Combine("Libraries", "Aspose.Words.lic"));

Document doc = new Document(@"D:\Temp\TestTableSource.doc");

foreach (Table table in doc.GetChildNodes(NodeType.Table, true))

{

if (table.ParentNode.NodeType == NodeType.Body)

{

table.AutoFit(AutoFitBehavior.AutoFitToWindow);

}

}

doc.Save(@"D:\Temp\ResultTable.pdf", SaveFormat.Pdf);

doc.Save(@"D:\Temp\ResultTable.doc", SaveFormat.Doc);

}

Best Regards,

Jörg Michel

Hi Jörg,


Thanks for your inquiry. In this case, you should just call Document.UpdateTableLayout method before exporting to PDF (or any other fixed-page format), only in rare cases where you confirmed that tables appear incorrectly laid out in the output document. I hope, calling this method will help to correct the output. Here is how use should use it:

private void createPDF_Click(object sender, RoutedEventArgs e)
{
License lic = new License();
lic.SetLicense(System.IO.Path.Combine(“Libraries”, “Aspose.Words.lic”));

Document doc = new
Document(@“D:\Temp\TestTableSource.doc”);
foreach (Table
table
in doc.GetChildNodes(NodeType.Table, true))
{
if (table.ParentNode.NodeType == NodeType.Body)
{
table.AutoFit(AutoFitBehavior.AutoFitToWindow);
}
}

doc.Save(@“D:\Temp\ResultTable.doc”, SaveFormat.Doc);

doc.UpdateTableLayout();
doc.Save(@“D:\Temp\ResultTable.pdf”, SaveFormat.Pdf);
}

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi Awais,

now the sample works. Thank's! The AutoFit-calculation has now only problems with long values without a delimiter. Then the table is greater than the papersize. But Word has this problem too.

Best Regards,

Jörg Michel