From e908d00c83c81676b7f1f23fb4505c65413acf59 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Mon, 20 Nov 2023 07:43:52 +0100 Subject: [PATCH] Try adding docx -> pdf conversion using the xwpf-converter from opensagres --- poishadow/build.gradle | 1 + .../poiandroidtest/poitest/MainActivity.java | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/poishadow/build.gradle b/poishadow/build.gradle index 79f6f3f6..ac3b3c7a 100644 --- a/poishadow/build.gradle +++ b/poishadow/build.gradle @@ -13,6 +13,7 @@ repositories { dependencies { implementation 'org.apache.poi:poi-ooxml:5.2.4' implementation 'org.apache.poi:poi-scratchpad:5.2.4' + implementation 'fr.opensagres.xdocreport:fr.opensagres.poi.xwpf.converter.pdf:2.0.4' implementation 'com.fasterxml:aalto-xml:1.3.0' implementation 'stax:stax-api:1.0.1' diff --git a/poitest/src/main/java/org/dstadler/poiandroidtest/poitest/MainActivity.java b/poitest/src/main/java/org/dstadler/poiandroidtest/poitest/MainActivity.java index ca82e0bb..55d5a3b2 100644 --- a/poitest/src/main/java/org/dstadler/poiandroidtest/poitest/MainActivity.java +++ b/poitest/src/main/java/org/dstadler/poiandroidtest/poitest/MainActivity.java @@ -49,9 +49,13 @@ import java.util.List; import java.util.Map; +import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter; +import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions; + public class MainActivity extends Activity { // Request code for creating a PDF document. private static final int CREATE_DOCX_FILE = 2; + private static final int CREATE_PDF_FILE = 3; private int idCount = 0; @@ -209,6 +213,23 @@ private void setupContent() throws IOException { return "Writing to selected file"; })); + DummyContent.addItem(new DummyItemWithCode("c" + (idCount++), + "DOCX to PDF", + () -> { + Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); + intent.addCategory(Intent.CATEGORY_OPENABLE); + intent.setType("application/pdf"); + intent.putExtra(Intent.EXTRA_TITLE, "DocWithImage.pdf"); + + // Optionally, specify a URI for the directory that should be opened in + // the system file picker when your app creates the document. + //intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, "DocWithImage.docx"); + + startActivityForResult(intent, CREATE_PDF_FILE); + + return "Writing to selected file"; + })); + DummyContent.addItem(new DummyItemWithCode("c" + (idCount++), "Test Callable", () -> "This is the result from the callable")); @@ -345,6 +366,38 @@ public void onActivityResult(int requestCode, int resultCode, Intent resultData) throw new IllegalStateException(e); } } + } else if (requestCode == CREATE_PDF_FILE + && resultCode == Activity.RESULT_OK) { + // The result data contains a URI for the document or directory that + // the user selected. + if (resultData != null) { + Uri uri = resultData.getData(); + // Perform operations on the document using its URI. + + try (InputStream docFile = getResources().openRawResource(R.raw.lorem_ipsum)) { + XWPFDocument doc = new XWPFDocument(docFile); + + try (InputStream pictureStream = getResources().openRawResource(R.raw.logo)) { + XWPFParagraph p = doc.createParagraph(); + + XWPFRun r = p.createRun(); + r.setText("logo.jpg"); + r.addBreak(); + r.addPicture(pictureStream, Document.PICTURE_TYPE_JPEG, + "logo.jpg", toEMU(200), + toEMU(200)); // 200x200 pixels + r.addBreak(BreakType.PAGE); + } + + try (ParcelFileDescriptor pdf = getContentResolver().openFileDescriptor(uri, "w")) { + try (OutputStream outputStream = new FileOutputStream(pdf.getFileDescriptor())) { + new PdfConverter().convert(doc, outputStream, PdfOptions.create()); + } + } + } catch (Exception e) { + throw new IllegalStateException(e); + } + } } } }