Issues in table layouts Using Aspose 21.1.0 while converting RTF to PDF

Issues in table layouts Using Aspose 21.1.0 while converting RTF to PDF

where in RTF entire table is showing properly but not in PDF.

@Haranath,

Please upgrade to the latest (21.9) version of Aspose.Words for .NET and see how it goes on your end? In case the problem still remains, then please ZIP and upload your input RTF Word document and Aspose.Words generated PDF file showing the undesired behavior here for testing. We will then investigate the issue on our end and provide you more information.

Thanks , Hafeez and i will upgrade and verify , if any issues i will zip and will send the required files.

@Haranath,

Sure, we will wait for your further input on this topic. Also, please provide a comparison screenshot highlighting (encircle) the problematic area(s) in Aspose.Words generated PDF.

Sample_RTF_Converted_PDF.docx (151.8 KB)

sample attached for your reference

@Haranath,

You had attached a DOCX file containing the screenshots only. Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A simplified source Word RTF document you are actually getting this problem with
  • Aspose.Words generated PDF file showing the undesired output
  • Your expected PDF file showing the desired output. You can create this file manually by using Save As to PDF command of MS Word.

As soon as you get these pieces of information ready, we will then start further investigation into your particular scenario and provide you more information.

RTF_To_PDF.zip (18.5 KB)

Uploaded RTF and converted PDF,

The last column in PDF is not properly visible, is there a way to set the table width wihout crossing the page margin while converting PDF

When we view the RTF in MS Word app, web layout all columns are visible, we want exact output in PDF as well.

@Haranath,

Please first make sure that the following font files are installed on your machine:

  • Calibri Light
  • Arial Unicode MS (you may get it from https://www.download-free-fonts.com/details/88978/arial-unicode-ms)
  • Times New Roman

Do you then see the same problem when converting source RTF Word document to PDF by using MS Word on your end? Please also attach the MS Word’s generated PDF file showing the desired behavior here for our reference.

I have also converted your “06.rtf" file to PDF format by using the licensed latest (21.10) version of Aspose.Words for .NET and by using the Save AS command of MS Word and attached the PDF files here for your reference:

Contents in both these PDF files look identical on my end.

I agree with Word print layout both are similar, but our clients were able to see the total table properly in Web layout option in MS word, our clients looking for same functionality in PDF, users don’t wont to loose any last column data, please suggest here

@Haranath,

One way is to increase Page width such that the table gets fit within it:

Document doc = new Document(@"C:\Temp\RTF_To_PDF\\06.rtf");

PageSetup ps = doc.FirstSection.PageSetup;

// Maximum allowed page width/height you can specify using MS Word 2016 is 22 inches
ps.PageWidth = 22 * 72;

Table table = doc.FirstSection.Body.Tables[0];
double tableWidth = GetTableWidth(table);

// Adjust PageWidth/Margins accordingly
if (tableWidth <= ps.PageWidth)
{
    ps.PageWidth = tableWidth;
    ps.LeftMargin = ps.LeftMargin / 2;
}

doc.UpdatePageLayout();

doc.Save(@"C:\Temp\\RTF_To_PDF\\21.10.pdf");

public static double GetTableWidth(Table table)
{
    LayoutCollector collector = new LayoutCollector((Document)table.Document);
    LayoutEnumerator enumerator = new LayoutEnumerator((Document)table.Document);

    double maxWidth = 0;
    foreach (Row row in table.Rows)
    {
        enumerator.Current = collector.GetEntity(row.LastCell.FirstParagraph);

        while (enumerator.MoveParent())
            if (enumerator.Type == LayoutEntityType.Row)
                break;

        // Get the width of Row
        double rowWidth = enumerator.Rectangle.Right;

        if ((rowWidth > maxWidth))
            maxWidth = rowWidth;
    }
    return maxWidth;
}

The mentioned solutioin will only work for first table , now i attached whole document , please help me, there are tons of documents having similar problems. I want generic solution, i applied below solution worked for 1484475260.zip (1.4 MB)
90% documents , but i want 100% quality of the docs

                         //code start
                   document = new Aspose.Words.Document(file);
                    DocumentBuilder builder = new DocumentBuilder(document);

                    NodeCollection tables = document.GetChildNodes(NodeType.Table, true);
                    foreach (var table in tables.OfType<Table>())
                    {
                        table.StyleOptions = TableStyleOptions.ColumnBands| TableStyleOptions.LastColumn;
                        table.AutoFit(AutoFitBehavior.AutoFitToWindow);
                        
                        foreach (Row row in table.Rows)
                        {
                            foreach (Cell c in row.Cells)
                            {
                                if (c.IsLastCell)
                                {
                                    //can we do any thing here , like estimate the length from the edge of the page and change the table width accordingly
                                }
                            }
                        }
                    }

                                 
                    document.UpdateTableLayout();
 PdfSaveOptions pdfSaveOptions = new PdfSaveOptions
                    {
                        ColorMode = ColorMode.Normal                       
                        
                    };

                    pdfSaveOptions.SaveFormat = SaveFormat.Pdf;
  document.Save(@"C:\Temp\PDF\" + Path.GetFileNameWithoutExtension(file) + ".pdf", pdfSaveOptions);

@Haranath,

You can build logic on the following code to get the desired output:

Document doc = new Document(@"C:\Temp\1484475260\\1484475260.rtf");

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

// Get Tables which are wider than their parent Sections
Dictionary<Table, double> keyValuePairs = new Dictionary<Table, double>();
foreach (Section section in doc.Sections)
{
    foreach (Table table in section.Body.Tables)
    {
        double tableWidth = GetTableWidth(collector, enumerator, table);

        if (section.PageSetup.PageWidth < tableWidth)
            keyValuePairs.Add(table, tableWidth);
    }
}

ArrayList uniqueSections = new ArrayList();
foreach (KeyValuePair<Table, double> kvp in keyValuePairs)
{
    Section section = (Section)kvp.Key.GetAncestor(NodeType.Section);
    if (!uniqueSections.Contains(section))
        uniqueSections.Add(section);
}

// for each sections, get the Tables with maximum widths
Dictionary<Table, double> tablesWithMaxWidths = new Dictionary<Table, double>();
foreach (Section section in uniqueSections)
{
    double maxWidth = 0;
    Table theTable = null;
    foreach (KeyValuePair<Table, double> kvp in keyValuePairs)
    {
        if (kvp.Key.GetAncestor(NodeType.Section) == section)
        {
            if (kvp.Value > maxWidth)
            {
                maxWidth = kvp.Value;
                theTable = kvp.Key;
            }
        }
    }

    tablesWithMaxWidths.Add(theTable, maxWidth);
}

// Increase widths of Sections
foreach (KeyValuePair<Table, double> kvp in tablesWithMaxWidths)
{
    Section section = (Section)kvp.Key.GetAncestor(NodeType.Section);
    section.PageSetup.PageWidth = kvp.Value;
}

doc.UpdatePageLayout();
doc.Save(@"C:\Temp\\1484475260\\21.10.pdf");

public static double GetTableWidth(LayoutCollector collector, LayoutEnumerator enumerator, Table table)
{
    double maxWidth = 0;
    foreach (Row row in table.Rows)
    {
        enumerator.Current = collector.GetEntity(row.LastCell.FirstParagraph);

        while (enumerator.MoveParent())
            if (enumerator.Type == LayoutEntityType.Row)
                break;

        // Get the width of Row
        double rowWidth = enumerator.Rectangle.Right;

        if ((rowWidth > maxWidth))
            maxWidth = rowWidth;
    }
    return maxWidth;
}

Hi HafeeZ,

Sorry for delay in response, thanks for your quick solution, i just applied , we will monitor for few days and will update soon.

@Haranath,

Sure, please let us know if you may have any troubles and we will be glad to look into this further for you.

1496980572.zip (87.4 KB)

i applied your solution it is working fine for most of the documents , but we have still some issues like,
not able to convert attached file from RTF to PDF using above code, could you please help me to resolve this issue, provide as a generic solution not specific to this doc

@Haranath,

I have converted the RTF file to PDF format by using the C# code from my previous post and attached the PDF file here for your reference (see 21.10.pdf (107.1 KB)). Do you still observe problems in this output PDF? If yes, then please also provide a comparison screenshot highlighting (encircle) the problematic areas in this Aspose.Words 21.10 generated output PDF.

getting null reference exception from GetTableWidth(), method at this line --> enumerator.Current = collector.GetEntity(row.LastCell.FirstParagraph);

used same code mentioned above. ASPOSE:Version :21.1.0.0

@Haranath

We have tested the scenario using the latest version of Aspose.Words for .NET 21.10 with this document and have not faced the shared issue. So, please use Aspose.Words for .NET 21.10.