I'm trying to use openpdf
for dynamic field.
Maven:
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.8</version>
</dependency>
Find fields: (dynamic find field names)
Type type = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) type;
Class<V> entity = (Class<V>) pt.getActualTypeArguments()[0];
// create Header
List<String> columnNames = new ArrayList<>();
Field[] superFields = entity.getSuperclass().getDeclaredFields();
for (Field field : superFields) {
if ( ! field.getName().equals("serialVersionUID"))
columnNames.add( field.getName() );
}
Field[] fields = entity.getDeclaredFields();
for (Field field : fields) {
if ( ! field.getName().equals("serialVersionUID"))
columnNames.add( field.getName() );
}
for (String columnName : columnNames) {
cell.setPhrase( new Phrase(columnName));
table.addCell(cell);
}
document.add(table);
document.close();
How to change dynamic this? (I use this code for only User
object)
List<User> users = repository.findAll();
for (User user : Users) {
cell.setPhrase( new Phrase( user.getId() ));
cell.setPhrase( new Phrase( user.getFirstName() ));
cell.setPhrase( new Phrase( user.getLastName() ));
}
Is there a better library for PDF export?
Thanks
I write some code for that problem:
first I can get list of name methods:
and second convert to
List<Object>
:and the end :
now I can call the method with dynamic Object and field