Can someone give me some idea to read a PDF file to make it editable and then save it again as PDF in Java? Is this possible in a reasonably simple way and without using libraries like itext? It is for a Liferay 6.2 project.
Thank you very much.
Can someone give me some idea to read a PDF file to make it editable and then save it again as PDF in Java? Is this possible in a reasonably simple way and without using libraries like itext? It is for a Liferay 6.2 project.
Thank you very much.
A very convenient way how to do doc conversion in Liferay is to use the open office integration.
DocumentConversionUtil.convert(
String id, InputStream inputStream, String sourceExtension,
String targetExtension)
Just be aware that this may break the PDF UI depending on the character of. If it's just text you should be fine.
I was able to recover the text of a PDF file with Apache PDFBox. In maven project pom.xml, we must add dependence
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.8</version>
</dependency>
The code:
try {
DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(folder.getGroupId(), folder.getFolderId(), fileName);
File file = DLFileEntryLocalServiceUtil.getFile(themeDisplay.getUserId(), fileEntry.getFileEntryId(), fileEntry.getVersion(), true);
PDDocument pddDocument=PDDocument.load(file);
PDFTextStripper textStripper = new PDFTextStripper();
String text = textStripper.getText(pddDocument);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
To create a PDF, see the documentation:
https://pdfbox.apache.org/1.8/cookbook/documentcreation.html