1

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.

  • 2
    There is no built-in way to edit a PDF in Java, if that's what you're asking. You'll have to use a library such as [iText](https://itextpdf.com/) or [Apache PDFBox](https://pdfbox.apache.org/). [This](https://stackoverflow.com/questions/4131031/editing-pdf-text-using-java) question might help you. – ack Jan 20 '18 at 18:36

2 Answers2

1

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.

Miroslav Ligas
  • 1,287
  • 8
  • 22
1

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