Reputation: 93
I need to insert various images in a doc or odt document using java, the images are generated on runtime. At the time I am tempted to use jodreports because it is very easy to generate the template of the final document, but I am stuck because I cannot find any kind of documentation telling me how to insert images. If you can answer my question please post a code snippet or tell me which other library I can use.
Thanks a lot for your help(and sorry for my bad English).
Upvotes: 1
Views: 3559
Reputation: 31
In my page https://xbrowser.altervista.org/informatica-portata/odt-converter/ you can find step by step how to implement your question.
Libraries are:
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import net.sf.jooreports.templates.DocumentTemplate;
import net.sf.jooreports.templates.DocumentTemplateException;
import net.sf.jooreports.templates.DocumentTemplateFactory;
import net.sf.jooreports.templates.image.FileImageSource;
This is the method
private Map<String, Object> data = new HashMap<String, Object>();;
private Logger log = Logger.getLogger(this.getClass().getName());
private void createOdt() {
DocumentTemplateFactory documentTemplateFactory = new DocumentTemplateFactory();
DocumentTemplate template = null;
File inputFile = new File("/path/template.odt"); //inserire il path relativo alla posizione del template .odt
try {
template = documentTemplateFactory.getTemplate(inputFile);
log.debug("input file ok -> " + inputFile);
} catch (IOException e) {
log.error(e);
}
try {
tmpFile = File.createTempFile("odt_", ".odt");
data.put("qrcode", new FileImageSource(new File("/img/src/qrcode.jpg"))); //qui è possibile generare per esempio un qrcode
template.createDocument(data, new FileOutputStream(tmpFile));
log.debug("output file temporaneo creato ("+ tmpFile.getAbsolutePath() + ")..");
} catch (FileNotFoundException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
} catch (DocumentTemplateException e) {
log.error(e);
}
}
At this point it is necessary that in the .odt template an image is inserted with the following modality:
As you can see the name of the image shows qrcode which is exactly the name of the hashmap key in our code(data.put("qrcode")
).
In this way it is possible to show dynamically generated images from the java code in a template defined in .odt.
I hope you have been helpful
Upvotes: 1
Reputation: 511
Just want to confirm that approach from @Jarabid works.
//visual signature
ImageSource signatureImage =
new RenderedImageSource(ImageIO.read(new File("resources/Signature.png")));
data.put("signature", signatureImage);
I'm using LibreOffice on the Mac and what I did was:
Worked perfectly first try. Happy :)
Upvotes: 0
Reputation: 11
Create a ImageSource
-object in your class.
ImageSource imagen =
new RenderedImageSource(ImageIO.read(new File(ruta_fisica_imagen)));
Add this object into Map object
In template define jooscript.image(name)
More information, the document is Spanish http://www.montesinos.org.es/2010/11/jodreports-mini-how-to.html
Upvotes: 1