Table not rendered correctly when saved as image

We were using Aspose for Java 10.5.0 and trying to render a word document contaning a table as image (png format). The table header was not rendered correctly (the cells in the header rows were not merged horizontally right). Is there a way to fix this issue? Thanks.

Hi Trung,

Thank you for inquiry. It would be great if you share your code snippet along with input files for testing purpose.

Hi Imran,
Below is the code snippet. We queried data from database and dynamically built the table with the data and then saved the document as an ByteArrayOutStream. The table was rendered correctly in the word file, but not correctly when saved as an image. I do have pictures of the table in both format (word file and image), but the forum does not have option to attach image. If you give me the email address, I can send it directly to you so you can see the difference.

try
{
    List list = new ArrayList();
    Document doc = _render(wordFile, placeholders);
    // save every page as PNG
    for (int i = 0; i <doc.getPageCount(); i++)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageSaveOptions op = new ImageSaveOptions(SaveFormat.PNG);
        op.setPageIndex(i);
        doc.save(bos, op);
        list.add(bos.toByteArray());
        bos.close();
    }
    return list;
}
catch (Exception e)
{
    e.printStackTrace();
    throw e;
}

Hi
Trung,

Thanks for the additional information. Unfortunately, it is difficult to say what the problem is without the Word document. I need this document to reproduce the problem on my side. You can attach files by clicking “Replay” button and on the bottom of page you will see File attachment “Add/Update” button. Also, you can send the files to my e-mail as described here:
https://forum.aspose.com/t/aspose-words-faq/2711

Best Regards,

Hi Awais,
I have attached a document containing the output table when saved as image and word. As you can see, when saving as image, the headers were not rendered correctly. Please let me know if you have a fix for this or any workaround. Thanks.
Trung

Hi Trung,

Thanks for the additional information. While using latest Aspose.Words for Java, i am unable to reproduce it on my side. Please try latest version. I have attached input/output files.

In case of any ambiguity, please let me know.

Hi Imran,
I downloaded the latest version of Aspose and still got the problem. I have created a simple test program for you to reproduce the problem. There are two methods saveAsImage() and saveAsDoc(). They are basically the same except for one saves the document containing the table as an image and one saves as .doc file. I have also attached the ouput in both format. As you can see, the image is still not rendered correctly.
Below is the code:

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.aspose.words.Cell;
import com.aspose.words.CellMerge;
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.Paragraph;
import com.aspose.words.ParagraphAlignment;
import com.aspose.words.Row;
import com.aspose.words.Run;
import com.aspose.words.SaveFormat;
import com.aspose.words.Table;

public class TestWordReport
{
    private static String[][] data = new String[][]
            {
                    {"Subject", "C", "C1", "C1", "C1", "C1", "C1", "C1", "C2", "C2", "C2", "C2", "C2", "C2", "C2"},
                    {"Subject", "C", "M+H", "M+H", "M+H", "Data2", "% of Dose", "% of Dose", "M+H", "M+H", "M+H", "Data2", "% of Dose", "% of Dose", "% of Dose"},
                    {"Subject", "C", "I1", "Dog", "Monkey", "Monkey", "Dog", "Monkey", "I1", "Dog", "Monkey", "I1", "I1", "Dog", "Monkey"},
                    {"1", "1", "", "", "", "", "", "", "", "", "", "", "", "", ""},
                    {"2", "1", "", "", "", "", "", "", "", "", "", "", "", "", ""},
                    {"2", "2", "", "", "", "", "", "", "", "", "", "", "", "", ""},
                    {"3", "1", "", "", "", "", "", "", "", "", "", "", "", "", ""},
                    {"3", "2", "", "", "", "", "", "", "", "", "", "", "", "", ""},
            };
    
    
    public static void main(String[] args)
    {
        try
        {
            com.aspose.words.License lic = new com.aspose.words.License();
            lic.setLicense(new FileInputStream("Aspose.Words.lic"));
            
            saveAsImage();
            saveAsDoc();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    private static void saveAsImage()
    {
        try
        {
            Document doc = new Document();
            Table table = new Table(doc);
            
            for (int r = 0; r <8; r++)
            {
                Row row = new Row(doc);
                table.appendChild(row);
                
                for (int c = 0; c <15; c++)
                {
                    Cell cell = new Cell(doc);
                    row.appendChild(cell);
                    cell.appendChild(new Paragraph(doc));
                    cell.getFirstParagraph().appendChild(new Run(doc, data[r][c]));
                    cell.getFirstParagraph().getRuns().get(0).getFont().setSize(8);
                    cell.getFirstParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
                    
                    if (r <3)
                    {
                        if (c <2)
                        {
                            if (r>= 1 && data[r - 1][c].equals(data[r][c]))
                                cell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
                            else
                                cell.getCellFormat().setVerticalMerge(CellMerge.FIRST);
                        }
                        else
                        {
                            if (r>= 1 && data[r][c - 1].equals(data[r][c]) && data[r - 1][c - 1].equals(data[r - 1][c]))
                                cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
                            else if (r == 0 && data[r][c - 1].equals(data[r][c]))
                                cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
                            else
                                cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
                        }
                    }
                    else
                    {
                        if ((c == 1) && data[r - 1][c].equals(data[r][c]) && data[r - 1][c - 1].equals(data[r][c - 1]))
                            cell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
                        else if (c == 0 && data[r - 1][c].equals(data[r][c]))
                            cell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
                        else
                            cell.getCellFormat().setVerticalMerge(CellMerge.FIRST);
                    }
                }
            }
            
            table.setAllowAutoFit(true);
            doc.getFirstSection().getBody().insertAfter(table, doc.getFirstSection().getBody().getFirstChild());
            
            // save every page as image as png
            for (int i = 0; i <doc.getPageCount(); i++)
            {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ImageSaveOptions op = new ImageSaveOptions(SaveFormat.PNG);
                op.setPageIndex(i);
                doc.save(bos, op);
                FileOutputStream fos = new FileOutputStream("test" + i + ".png");
                fos.write(bos.toByteArray());
                fos.close();
                bos.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    private static void saveAsDoc()
    {
        try
        {
            Document doc = new Document();
            Table table = new Table(doc);
            
            for (int r = 0; r <8; r++)
            {
                Row row = new Row(doc);
                table.appendChild(row);
                
                for (int c = 0; c <15; c++)
                {
                    Cell cell = new Cell(doc);
                    row.appendChild(cell);
                    cell.appendChild(new Paragraph(doc));
                    cell.getFirstParagraph().appendChild(new Run(doc, data[r][c]));
                    cell.getFirstParagraph().getRuns().get(0).getFont().setSize(8);
                    cell.getFirstParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
                    
                    if (r <3)
                    {
                        if (c <2)
                        {
                            if (r>= 1 && data[r - 1][c].equals(data[r][c]))
                                cell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
                            else
                                cell.getCellFormat().setVerticalMerge(CellMerge.FIRST);
                        }
                        else
                        {
                            if (r>= 1 && data[r][c - 1].equals(data[r][c]) && data[r - 1][c - 1].equals(data[r - 1][c]))
                                cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
                            else if (r == 0 && data[r][c - 1].equals(data[r][c]))
                                cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
                            else
                                cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
                        }
                    }
                    else
                    {
                        if ((c == 1) && data[r - 1][c].equals(data[r][c]) && data[r - 1][c - 1].equals(data[r][c - 1]))
                            cell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
                        else if (c == 0 && data[r - 1][c].equals(data[r][c]))
                            cell.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
                        else
                            cell.getCellFormat().setVerticalMerge(CellMerge.FIRST);
                    }
                }
            }
            
            table.setAllowAutoFit(true);
            doc.getFirstSection().getBody().insertAfter(table, doc.getFirstSection().getBody().getFirstChild());
            
            // save as .doc file
            doc.save("test.doc");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Hi Trung,

Thanks for the additional information. While using latest Aspose.Words 11.0.0 for java. I managed to reproduce this problem on my side. I have logged your issue into our bug tracking system. Your request has also been linked to the appropriate issue. Once we sort it out, we will let you know. Sorry for inconvenience.

Hi Trung,

Thanks for your inquiry. Please try using the following code snippet in your saveAsImage method that worked for me:

Table table = new Table(doc);
for (int r = 0; r < 8; r++)
{
    Row row = new Row(doc);
    table.appendChild(row);
    for (int c = 0; c < 15; c++)
    {
        Cell cell = new Cell(doc);
        row.appendChild(cell);
        cell.appendChild(new Paragraph(doc));
        cell.getFirstParagraph().appendChild(new Run(doc, data[r][c]));
        cell.getFirstParagraph().getRuns().get(0).getFont().setSize(8);
        cell.getFirstParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
    }
}

// save every page as image as png*
for (int i = 0; i < doc.getPageCount(); i++)
{
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageSaveOptions op = new ImageSaveOptions(SaveFormat.PNG);
    op.setPageIndex(i);
    doc.save(bos, op);
    FileOutputStream fos = new FileOutputStream("C:\\test\\test" + i + ".png");
    fos.write(bos.toByteArray());
    fos.close();
    bos.close();
}

I hope, this will help.

Best Regards,

Thanks for your input, but we do need the section of code to merge the cells in the example I provided in order to to have the desired headers. When the document is save as word, the table looked correct. But the same document when saving as image looked different.
We really do need to resolve this issue asap as we have to deliver the product to our client at the end of this week. Can you provide us with a patch?

Hi
Trung,

Thanks for your inquiry. You can ask issue status here. Your issue is pending for analysis. Once our developers analyze these issues, we will be able to provide you an estimate. You will be notify as soon as it is fixed.

Hi Trung,

The bug should be fixed in .Net baseline first. Sorry for inconvenience. As a temporary workaround you can save the document to DOC format first and just after this to image or pdf – merged cells will be calculated correctly.

Regards,

Hi,
We applied the workaround and it fixed the column issue. However, we still saw another issue. When the table was too big, it was truncated when saved as image or .doc file even though the setAllowAutoFit was set to true. This happened when the cells contained long text without spacing. Is there a way to fix this? Attached are both the image and the .doc files.
Also we would like to know when the real fix will be done for the Aspose.Words for Java. Currently we still use the workaound.
Thanks,
-Trung

Do you have a fix for this? We really need this resovled asap. Thanks.

Hi Trung,

Thanks for your inquiry. Unfortunately, your issues are pending for analysis. Once our developers analyze these issues, we will be able to provide you an estimate. You will be notify as soon as it is fixed. Sorry for inconvenience.

Hi, any status on this?

Hi

Thanks for your request. Unfortunately, the issue is unresolved yet. We will keep you informed and immediately let you know once it is fixed.

Best regards,

The issues you have found earlier (filed as WORDSJAVA-508) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The issues you have found earlier (filed as WORDSNET-5882) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.