Home / java templ4docx docx apache-poi / Templ4docx 2.0.0 - Image Variables

Templ4docx 2.0.0 - Image Variables

19 August 2015 on java, templ4docx, docx, and apache-poi 2 minutes

In the previous post, I’ve written about simple text variable. Now, I would like to spend some time on Image Variables. In the previous example, we created an invitation. Now I’m going to change a little our example, and we will create a business card. The standard card has information about name, last name and phone number. I would like to add image to my business card as well. So in my business card will be 4 variables: ${firstname}, ${lastname}, ${phone}, ${photo}. The first three are simple text variables. The last one is variable of image type, so we need to create appropriate ImageVariable object. In my case, I want to insert photo 75px x 75px to business card template: new ImageVariable("${photo}", "E:\\photo.jpeg", ImageType. JPEG, 75, 75)

If you want to insert image from the Internet, you can use sweetener (pl.jsolve.sweetener) library:

File logo = Resources.asFile(new URL("https://media.licdn.com/mpr/mpr/shrink_100_100/p/2/000/189/0b4/11ba0d4.jpg" ), File.createTempFile("tmpPhoto", ".tmp" ));
ImageVariable imageVariable = new ImageVariable("${photo}" , logo, ImageType.JPEG, 75, 75);

The rest of the code is identical to the example of the previous post. Below is the full code example:

Docx docx = new Docx("E:\\businessCard.docx" );

Variables var = new Variables();
var. addTextVariable(new TextVariable( "${firstname}", "Lukasz" ));
var.addTextVariable( new TextVariable("${lastname}" , "Stypka" ));
var.addTextVariable( new TextVariable("${phone}" , "123456789" ));

File logo = Resources.asFile(new URL("https://media.licdn.com/mpr/mpr/shrink_100_100/p/2/000/189/0b4/11ba0d4.jpg" ), File.createTempFile("tmpPhoto", ".tmp" ));
ImageVariable imageVariable = new ImageVariable("${photo}" , logo, ImageType.JPEG, 75, 75);
var.addImageVariable(imageVariable);
docx.fillTemplate(var);
docx.save( "E:\\businessCard2.docx");

Previous

Templ4docx 2.0.0 - Text Variables

The most common situation is when we want to replace simple text variable. Imagine that you have an invitation template and you want to prepare filled invitation with firstname and lastname. So the only thing you need is to find variables in the template and then replace them with the...

Next

Spring Boot - Security in distributed system

Spring Security is indispensable part of every enterprise Spring based application. Everything looks pretty easy, when you want to secure single web-application. Unfortunately, everything looks completely different when your system consists of many components. In that cases the expected behavior is single login for whole system, not separate login for...