I am using ASPOSE java word api to convert an word document to image document. However, I facing issue with it post conversion. After conversion it does not preserve the source document apperance.
I am attaching an input.docx and output.docx file for more clarity.
Issue 1 - Input.docx document contains two pages however, Output document is in only one page.
Issue 2 - There is a small white space between these two blue boxes.
As you can see in INPUT.docx file here -
But in OUTPUT.docx these spaces disappeared.
Here is the below code which I have wrote to perform the action.
import java.io.File;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
public class TestAspose {
public static void main(String[] args) throws Exception {
Document docOut = new Document();
DocumentBuilder builder = new DocumentBuilder(docOut);
System.out.println("Process Started...");
Document doc = new Document("D:\\ACA33\\Aspose\\INPUT.docx");
ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.PNG);
for (int i = 0; i < doc.getPageCount(); i++) {
opt.setPageIndex(i);
opt.setPageCount(1);
opt.setResolution(150f);
doc.save("D:\\ACA33\\Aspose\\page_" + i + ".png", opt);
String imagePath = String.format("D:\\ACA33\\Aspose\\page_%d.png", i);
builder.insertImage(imagePath);
File imageFile = new File(imagePath);
if (imageFile.delete()) {
System.out.println("Deleted the file: " + imagePath);
} else {
System.out.println("Failed to delete the file: " + imagePath);
}
}
docOut.save("D:\\ACA33\\Aspose\\OUTPUT.docx");
System.out.printf("Hello and completed!!!");
}
}
Input document -
INPUT.docx (43.7 KB)
Output document -
OUTPUT.docx (192.0 KB)
@ketanpbh
It seems you’re experiencing issues with the conversion of a Word document to an image format using Aspose.Words for Java. Here’s a structured approach to address the problems you’ve encountered:
Cause:
- Multiple Pages Issue: The code may not be correctly handling the layout when converting a multi-page document, leading to images not being placed on separate pages.
- White Space Between Boxes: The white space issue could stem from how images are inserted, potentially due to incorrect layout options.
Solution:
To resolve these issues, you can modify your code as follows:
import java.io.File;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
public class TestAspose {
public static void main(String[] args) throws Exception {
Document docOut = new Document();
DocumentBuilder builder = new DocumentBuilder(docOut);
System.out.println("Process Started...");
Document doc = new Document("D:\\ACA33\\Aspose\\INPUT.docx");
ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.PNG);
for (int i = 0; i < doc.getPageCount(); i++) {
opt.setPageIndex(i);
opt.setPageCount(1);
opt.setResolution(150f);
String imagePath = "D:\\ACA33\\Aspose\\page_" + i + ".png";
doc.save(imagePath, opt);
builder.insertImage(imagePath);
builder.insertBreak(); // Add a page break after each image
File imageFile = new File(imagePath);
if (imageFile.delete()) {
System.out.println("Deleted the file: " + imagePath);
} else {
System.out.println("Failed to delete the file: " + imagePath);
}
}
docOut.save("D:\\ACA33\\Aspose\\OUTPUT.docx");
System.out.printf("Process completed successfully!");
}
}
Next Steps:
- Implement the suggested code modifications to ensure that each image is placed on a new page and that the layout settings are correct.
- Test the modified code with your document to verify that the issues are resolved.
- If problems persist, review the original Word document’s formatting for compatibility with the conversion process.
Please let me know if this solution works for you or if you have any further questions!
Above provided solution by bot does not work.
@alexey.noskov could you please check this issue. and let me know the cause of this issue?
@ketanpbh As I can see you are using a very old 18.11 version of Aspose.Words. The problem is not reproducible using the latest 25.7 version of Aspose.Words for Java. Here is the produced output:
out.docx (191.9 KB)
Please try using the latest version and let us know if the problem still persist on your side.