Skip to content

Commit

Permalink
Try adding docx -> pdf conversion using the xwpf-converter from opens…
Browse files Browse the repository at this point in the history
…agres
  • Loading branch information
centic9 committed Nov 20, 2023
1 parent cd3ddbc commit e908d00
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions poishadow/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"));

Expand Down Expand Up @@ -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);
}
}
}
}
}

0 comments on commit e908d00

Please sign in to comment.