我使用XDocReport和Velocity生成DOCX文件。我目前正在使用图像,目前我已经能够应对它们了。
但现在,我正试图弄清楚如何实施以下内容:
我有一组端口。每个端口可能有1到N个目录。每个目录都有一个IImageProvider的HashMap(可能更改为List)。
我不知道是否可以实施或不实施。这里是示例类:
//The list of ports seems like:
List<Port> ports = ....
public class Port{
private String descriptionPort;
private String title;
private String detail;
private IImageProvider image1;
private IImageProvider image2;
private ArrayList<Catalogue> listCatalogue;
.......
}
public class Catalogue{
private String description;
private String detail;
private String duration;
/* Formerly I had this one (it worked) instead of logos:
* private IImageProvider logo;
*/
//new: multiple images per catalogue
private HashMap<String, IImageProvider> logos;
private Number logosCount;
..........
}
因此,在创建DOCX报告时:
InputStream in = fileTemp.getBinaryStream();
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
fieldsMetadata.load("ports", Port.class);
fieldsMetadata.addFieldAsImage("photoPort1", "p.Image1");
fieldsMetadata.addFieldAsImage("photoPort2", "p.Image2");
/*Formerly it worked this one when having a unique Image per Catalogue:
*fieldsMetadata.addFieldAsImage("photoCat", "cat.Logo.");
*/
report.setFieldsMetadata(fieldsMetadata);
IContext context = report.createContext();
context.put("ports", ports);
report.process(context, outputStream); //ignore outputStream
因此,我想知道如何从一组目录的哈希中添加图像。我的建议是由XDocReport支持的吗?
提前致谢。每一次帮助对我来说意义重大,我都非常感激:)。
答案 0 :(得分:1)
按照示例(DeveloperWithImage.java)中所述使用POJO
/* Load the photos as list in the metadata */
FieldsMetadata metadata = report.createFieldsMetadata();
metadata.load( "photos", Photo.class, true );
report.setFieldsMetadata(metadata);
List<Photo> photos = ...
context.put( "photos", photos );
Photo.java:
public class Photo {
private IImageProvider photo;
@FieldMetadata( images = { @ImageMetadata( name = "photo" ) }, description="Photo" )
public IImageProvider getPhoto() {
return photo;
}
public void setPhoto(IImageProvider photo) {
this.photo = photo;
}}
文章共享here