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

Free Support Forum - aspose.com

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:
http://www.aspose.com/corporate/purchase/faqs/send-license-to-aspose-staff.aspx

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);

<font color="RED"><b>for</b></font> <font color="BLUE"><b>(</b></font><font color="RED"><b>int</b></font> c <font color="BLUE">=</font> <font color="BROWN">0</font><font color="BLUE"><b>;</b></font> c <font color="BLUE"><</font> <font color="BROWN">15</font><font color="BLUE"><b>;</b></font> c<font color="BLUE"><font color="BLUE">+</font><font color="BLUE">+</font></font><font color="BLUE"><b>)</b></font>
<font color="BLUE"><b>{</b></font>
    Cell cell <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Cell<font color="BLUE"><b>(</b></font>doc<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    row<font color="BLUE"><b>.</b></font>appendChild<font color="BLUE"><b>(</b></font>cell<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    cell<font color="BLUE"><b>.</b></font>appendChild<font color="BLUE"><b>(</b></font><font color="RED"><b>new</b></font> Paragraph<font color="BLUE"><b>(</b></font>doc<font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    cell<font color="BLUE"><b>.</b></font>getFirstParagraph<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>appendChild<font color="BLUE"><b>(</b></font><font color="RED"><b>new</b></font> Run<font color="BLUE"><b>(</b></font>doc<font color="BLUE"><b>,</b></font> data<font color="BLUE"><b>[</b></font>r<font color="BLUE"><b>]</b></font><font color="BLUE"><b>[</b></font>c<font color="BLUE"><b>]</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    cell<font color="BLUE"><b>.</b></font>getFirstParagraph<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getRuns<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>get<font color="BLUE"><b>(</b></font><font color="BROWN">0</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getFont<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>setSize<font color="BLUE"><b>(</b></font><font color="BROWN">8</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    cell<font color="BLUE"><b>.</b></font>getFirstParagraph<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getParagraphFormat<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>setAlignment<font color="BLUE"><b>(</b></font>ParagraphAlignment<font color="BLUE"><b>.</b></font>CENTER<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>        
<font color="BLUE"><b>}</b></font>

}

// 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<font color="BLUE"><b>.</b></font>save<font color="BLUE"><b>(</b></font>bos<font color="BLUE"><b>,</b></font> op<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

FileOutputStream fos <font color="BLUE">=</font> <font color="RED"><b>new</b></font> FileOutputStream<font color="BLUE"><b>(</b></font><font color="PURPLE">"C:\\test\\test"</font> <font color="BLUE">+</font> i <font color="BLUE">+</font> <font color="PURPLE">".png"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
fos<font color="BLUE"><b>.</b></font>write<font color="BLUE"><b>(</b></font>bos<font color="BLUE"><b>.</b></font>toByteArray<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
fos<font color="BLUE"><b>.</b></font>close<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
bos<font color="BLUE"><b>.</b></font>close<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

}

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.