I have tried to insert an image into a word document with byte array but it does not let me insert it and gives an error “illegal argument exception: image format unsupported”.I also tried to convert the bytes array to buffered image but that also did not work.I am not using mail merge for creating the word document.
All I am trying to do is I have a column in my sql server database with datatype image.I want to use the image stored in the database and insert in my word document.
Any help or any pointers would be appreciated!
Please find my code below
public String imageit(final String question) throws IOException, SQLException
{
License();
Document doc = null;
try
{
doc = new Document("C:/graph.docx");
}
catch (final Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
final DocumentBuilder builder = new DocumentBuilder(doc);
builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
builder.getPageSetup().setLeftMargin(30);
builder.getPageSetup().setRightMargin(30);
builder.getPageSetup().setTopMargin(30);
builder.getPageSetup().setBottomMargin(40);
builder.writeln("Hi");
try
{
builder.insertImage("C:/ab-pn.png", 100, 100);
}
catch (final Exception e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
final String st = "abcd";
System.out.println("Inside method");
final DataSource project = DataSourceFactory.createDataSource();
project.addTable("afm_docvers");
project.addField("key_value");
project.addField("file_contents");
project.addQuery("SELECT * FROM afm_docvers WHERE key_value ='" + st + "' ");
new ByteArrayOutputStream();
byte[] bytes = null;
BufferedImage bImageFromConvert = null;
for (final DataRecord record1: project.getAllRecords())
{
// final Blob image = (Blob) record1.getValue("afm_docvers.file_contents"); // Blob
final Object object = record1.getValue("afm_docvers.file_contents"); // Blob image
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(object);
bytes = out.toByteArray();
System.out.println("Bytessss" + bytes.toString());
InputStream in = new ByteArrayInputStream(bytes);
bImageFromConvert = ImageIO.read(in);
}
try
{
// ImageIO.write(bImageFromConvert, "jpg", new File("c:/data/new-darksouls.jpg"));
// if (bImageFromConvert.equals(null)) {
// System.out.println("Still getting null");
// } else {
//// builder.insertImage(bytes);
builder.insertImage(bImageFromConvert);
// }
}
catch (final Exception e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
try
{
doc.save("C:/classification5.docx");
}
catch (final Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "aa";
}
Hi there,
Thanks for your inquiry. Could you please attach your input Word document and image here for testing? I will investigate the issue on my side and provide you more information.
Please use the ResultSet.getbytes
method in your code to read the image data. I suggest you please read the overloaded insertImage methods from here:
https://reference.aspose.com/words/java/com.aspose.words/documentbuilder/
Hope this helps you.
Thanks for your reply.Please find the image file attached.Since we have a different set of connectivity procedures I cannot use “resultset” and getbytes is not available with it.
I have tried printing the object.toString and it gives a hexadecimal number.I have seen that when I convert it into bytes I get the bytes(as in the bytes variable is not null it fetches value)But when I try to pass it to builder.insertImage(bytes,100,100) it gives me that error of unsupported image.
Hi there,
Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.2.0) from here and let us know how it goes on your side.
I have tested the scenario using latest version of Aspose.Words for Java 14.2.0 and have not found the shared issue. Please make sure that you are using the correct array of byte for image file. You can check the image byte array by saving it to disk as shown in highlighted code below.
// Open the stream.
InputStream stream = new FileInputStream(MyDir + "in.jpg");
byte[] bytes = IOUtils.toByteArray(stream);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertImage(bytes, 100, 100);
doc.save(MyDir + "Out.docx");
FileOutputStream fileOuputStream = new FileOutputStream(MyDir + "out.jpg");
fileOuputStream.write(bytes);
fileOuputStream.close();
Hi Tahir,
Thanks a lot for you help.It worked!!
To resolve the issue I had to accept the image as a blob since the column data type was ‘image’ and get it with resultset.I have put down the code below so that it may help others.
Full Method:
public String testit3(final String question) throws IOException, SQLException, Exception
{
License();
final String proj = "some key";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
final Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=dbname", "sa", "pass");
final Statement st = con.createStatement();
final ResultSet rs = st.executeQuery("SELECT * FROM docvs WHERE pkey_value ='" + proj + "' ");
Blob blob = null;
while (rs.next())
{
blob = rs.getBlob("file_contents");
}
final BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
final byte[] bytes = IOUtils.toByteArray(in);
Document doc = null;
try
{
doc = new Document("C:/Tomcat 6.0/webapps/Survey Files/SurveyDocs/graph.docx");
}
catch (final Exception e1)
{
e1.printStackTrace();
}
final DocumentBuilder builder = new DocumentBuilder(doc);
try
{
builder.insertImage(bytes);
}
catch (final Exception e1)
{
e1.printStackTrace();
}
try
{
builder.insertImage("C:/Tomcat 6.0/webapps/graphics/abcd.png",100, 100);
}
catch (final Exception e2)
{
e2.printStackTrace();
}
try
{
doc.save("C:/Tomcat 6.0/webapps/ Survey Files/SurveyDocs/myimg.docx");
}
catch (final Exception e)
{
e.printStackTrace();
}
con.close();
return "aa";
}
Hi there,
Thanks for your feedback. It is nice to hear from you that your problem has been solved.
We always appreciate positive feedback from our customers. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.