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