diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /android/source/src/java/org/libreoffice/PDFDocumentAdapter.java | |
parent | Initial commit. (diff) | |
download | libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'android/source/src/java/org/libreoffice/PDFDocumentAdapter.java')
-rw-r--r-- | android/source/src/java/org/libreoffice/PDFDocumentAdapter.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/android/source/src/java/org/libreoffice/PDFDocumentAdapter.java b/android/source/src/java/org/libreoffice/PDFDocumentAdapter.java new file mode 100644 index 000000000..2ce167ce3 --- /dev/null +++ b/android/source/src/java/org/libreoffice/PDFDocumentAdapter.java @@ -0,0 +1,86 @@ +package org.libreoffice; + +import android.annotation.TargetApi; +import android.content.Context; +import android.os.Bundle; +import android.os.CancellationSignal; +import android.os.ParcelFileDescriptor; +import android.print.PageRange; +import android.print.PrintAttributes; +import android.print.PrintDocumentAdapter; +import android.print.PrintDocumentInfo; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +@TargetApi(19) +public class PDFDocumentAdapter extends PrintDocumentAdapter{ + Context mContext; + String pdfFile; + + public PDFDocumentAdapter(Context mContext, String pdfFile) { + this.mContext = mContext; + this.pdfFile = pdfFile; + } + + @Override + public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) { + if (cancellationSignal.isCanceled()) { + callback.onLayoutCancelled(); + } + else { + File f = new File(pdfFile); + PrintDocumentInfo.Builder builder= + new PrintDocumentInfo.Builder(f.getName()); + builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT) + .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN) + .build(); + callback.onLayoutFinished(builder.build(), + !newAttributes.equals(oldAttributes)); + } + } + + @Override + public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) { + InputStream in=null; + OutputStream out=null; + try { + File file = new File(pdfFile); + in = new FileInputStream(file); + out=new FileOutputStream(destination.getFileDescriptor()); + + byte[] buf=new byte[in.available()]; + int size; + + while ((size=in.read(buf)) >= 0 + && !cancellationSignal.isCanceled()) { + out.write(buf, 0, size); + } + + if (cancellationSignal.isCanceled()) { + callback.onWriteCancelled(); + } + else { + callback.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES }); + } + } + catch (Exception e) { + callback.onWriteFailed(e.getMessage()); + e.printStackTrace(); + } + finally { + try { + in.close(); + out.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + + } +}
\ No newline at end of file |