Apache POI: Difference between revisions
Jump to navigation
Jump to search
Line 122: | Line 122: | ||
| valign="top" | | | valign="top" | | ||
* [https://stackoverflow.com/questions/37599003/ Extract a paragraph & table from word] | * [https://stackoverflow.com/questions/37599003/ Extract a paragraph & table from word] | ||
* [https://stackoverflow.com/questions/48322534/ Copy tables from one docx to another] | |||
* [https://stackoverflow.com/questions/5457771/ Get text from Textbox of MS word] | * [https://stackoverflow.com/questions/5457771/ Get text from Textbox of MS word] | ||
* [https://stackoverflow.com/questions/3022376/ Convert a Word document to PDF] | * [https://stackoverflow.com/questions/3022376/ Convert a Word document to PDF] | ||
|} | |} |
Revision as of 05:12, 5 January 2022
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.converter</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
|
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
|
| |
LocalDateTime startTime = LocalDateTime.now();
XWPFDocument doc = new XWPFDocument(new ClassPathResource("/META-INF/chorke/template.docx").getInputStream());
String pdfFilePath = String.format("%s/.chorke/academia/var/pdf/report.pdf", System.getProperty("user.home"));
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(pdfFilePath);
PdfConverter.getInstance().convert(doc, out, options);
out.close()
double duration = Duration.between(startTime, LocalDateTime.now()).toMillis()/1000.0;
LOG.info("Duration: {} seconds", String.format("%.2f", duration));
|
Font Provider
PdfOptions options = PdfOptions.create();
options.fontProvider((familyName, encoding, size, style, color) -> {
try {
BaseFont baseFont = BaseFont.createFont("fonts/LiberationSans-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
return new Font(baseFont, size, style, color);
} catch (Exception e) {
LOG.warn("Font not found!\n{}", e.getMessage());
}
});
Replacements
public static void replace(XWPFParagraph paragraph, Map<String, Object> replacements) {
List<XWPFRun> runs = paragraph.getRuns();
for (String find : replacements.keySet()) {
String replace = (String) replacements.get(find);
TextSegment found = paragraph.searchText(find, new PositionInParagraph());
if (!ObjectUtils.isEmpty(found)) {
if (found.getBeginRun() == found.getEndRun()) {
XWPFRun run = runs.get(found.getBeginRun());
String text = run.getText(run.getTextPosition());
String replaced = text.replace(find, replace);
run.setText(replaced, 0);
} else {
StringBuilder builder = new StringBuilder();
for (int position = found.getBeginRun(); position <= found.getEndRun(); position++) {
XWPFRun run = runs.get(position);
builder.append(run.getText(run.getTextPosition()));
}
String text = builder.toString();
String replaced = text.replace(find, replace);
XWPFRun partOne = runs.get(found.getBeginRun());
partOne.setText(replaced, 0);
for (int position = found.getBeginRun() + 1; position <= found.getEndRun(); position++) {
XWPFRun partNext = runs.get(position);
partNext.setText("", 0);
}
}
}
}
}