Giter Site home page Giter Site logo

asimmon / mupdf-for-xamarin-android Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 9.0 49.9 MB

A port of the MuPDF Android library for Xamarin Android. With this library, you will be able to read, write, modify PDF files as well as convert pages to images.

License: GNU Affero General Public License v3.0

C# 100.00%

mupdf-for-xamarin-android's Introduction

MuPDF for Xamarin Android

A port of the MuPDF Android library for Xamarin Android. With this library, you will be able to read, write, modify PDF files as well as convert pages to images.

The latest release has been created from the 1.18.0 tag of MuPDF source code. It supports the following Android ABIs: armeabi-v7a, arm64-v8a, x86 and x86_64. Support for armeabi and MIPS has been removed.

Features

  • Create or modify existing PDF files
  • Render PDF files as images (jpeg, png, etc.)
  • Anything else coming out of the box from MuPDF library

Quote from mupdf.com:

The renderer in MuPDF is tailored for high quality anti-aliased graphics. It renders text with metrics and spacing accurate to within fractions of a pixel for the highest fidelity in reproducing the look of a printed page on screen.

MuPDF is also small, fast, and yet complete. It supports PDF 1.7 with transparency, encryption, hyperlinks, annotations, searching and more. It also reads XPS and OpenXPS documents. MuPDF is written modularly, so features can be added on by integrators if they so desire.

Since the 1.2 release of MuPDF, we have optional support for interactive features such as form filling, javascript and transitions.

Get started

Install the nuget package in the Package Manager Console:

Install-Package Askaiser.Android.MuPDF

Exemple 1: rendering a PDF's first page as an image from its bytes

using Artifex.MuPdf;

// [...]
var pdfBytes = // ... 

using var doc = Document.OpenDocument(pdfBytes, string.Empty);
using var firstPage = doc.LoadPage(0);

using var pageBitmap = AndroidDrawDevice.DrawPage(firstPage, dpi: 72);
using var jpgStream = new MemoryStream();
await pageBitmap.CompressAsync(Bitmap.CompressFormat.Jpeg, quality: 90, jpgStream);

jpgStream.Seek(0, SeekOrigin.Begin);
var jpgBytes = jpgStream.ToArray();

var downloadsDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
var jpgFilePath = System.IO.Path.Combine(downloadsDir.AbsolutePath, "page-0.jpg");

using var fileStream = File.OpenWrite(jpgFilePath);
fileStream.Write(jpgBytes);

Exemple 2: Reading a PDF from a seekable stream

In order to use input seekable streams, you will need to include the following implementation of the Java input stream to make it compatible with C#.

using Artifex.MuPdf;

// [...]
using var inputStream = File.OpenRead("...");
using var inputStreamWrapper = new SeekableInputStreamWrapper(inputStream);
using var doc = Document.OpenDocument(inputStreamWrapper, string.Empty);

// [...]
private class SeekableInputStreamWrapper : global::Java.Lang.Object, ISeekableInputStream
{
    private readonly Stream _innerStream;

    public SeekableInputStreamWrapper(Stream innerStream)
    {
        this._innerStream = innerStream;
    }

    public long Position() => this._innerStream.Position;

    public long Seek(long offset, int origin) => this._innerStream.Seek(offset, origin switch
    {
        SeekableStream.SeekCur => SeekOrigin.Current,
        SeekableStream.SeekEnd => SeekOrigin.End,
        SeekableStream.SeekSet => SeekOrigin.Begin,
        _ => SeekOrigin.Begin
    });

    public int Read(byte[] buffer) => this._innerStream.Read(buffer);

    public new void Dispose()
    {
        base.Dispose();
        this.Dispose(true);
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        this._innerStream.Dispose();
    }

    // These properties and methods are required by the ISeekableInputStream interface
    public JniManagedPeerStates JniManagedPeerState { get; }

    public void SetJniIdentityHashCode(int value) { }
    public void SetPeerReference(JniObjectReference reference) { }
    public void SetJniManagedPeerState(JniManagedPeerStates value) { }
    public void DisposeUnlessReferenced() { }
    public void Disposed() { }
    public void Finalized() { }
}

Licensing

MuPDF is GNU Affero General Public License v3.0 or later licensed.

MuPDF is Copyright 2006-2020 Artifex Software, Inc.

I am not associated with either Artifex Software, Inc or Xamarin Inc. All rights belong to their respective owners.

mupdf-for-xamarin-android's People

Contributors

asimmon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mupdf-for-xamarin-android's Issues

Reflow mode showing blank page with '100%' text

If I use the library in my app with the following code:

MuPDFCore core = new MuPDFCore(this, path);
var reflowView = new MuPDFReflowView(this, core, new Point(parent.Width, parent.Height));
reflowView.SetPage(3, new PointF());
parent.AddView(reflowView);

It only shows a blank page with the text '100%' on the upper left. The same applies if I use MuPDFReaderView:

MuPDFCore core = new MuPDFCore(this, $"{directory}{apiBook.Epub}");
MuPDFReaderView readerView = new MuPDFReaderView(this);
readerView.Adapter = new MuPDFReflowAdapter(this, core);
readerView.SetLayerType(LayerType.Hardware, null);
parent.AddView(readerView);

how to build the library

hello my friend,

i want to use mupdf library inside xamarin android project, your binding working very well, thanks a lot for this, but I want to change something inside it, so what I did is I download the source code and change something.

my question is how you build the library and get the .so files?
I want to do that then to make binding for xamarin android

any hint or steps to do it?

thank you

Using MuPDFPageView shows a blank page

I get a blank page when using the library with the following code:

MuPDFCore core = new MuPDFCore(this, path);
Bitmap mSharedHqBm = Bitmap.CreateBitmap(parent.Width, parent.Height, Bitmap.Config.Argb8888);

var pageView = new MuPDFPageView(this, null, core, new Point(parent.Width, parent.Height), mSharedHqBm);
pageView.SetPage(0, new PointF(parent.Width, parent.Height));
parent.AddView(pageView);

One More :(

/Documents/gitProject/xamarin/MuPDF-for-Xamarin-Android/src/Askaiser.Android.MuPDF/PartialClasses.cs(45,45): Error CS0115: `Artifex.MuPdf.ReaderView.RawAdapter' is marked as an override but no suitable property found to override (CS0115) (Askaiser.Android.MuPDF)

Add arm64 version of libmupdf_java.so to libs folder

Can't use the library on my Pixel C:

dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.mypackge-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mypackge-1/lib/arm64, /system/fake-libs64, /data/app/com.mypackge-1/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]] couldn't find "libmupdf_java.so"

Thanks!

Password protected pdf

Hi,
I've been looking for an android pdf viewer for more than a year, finally some hope! I was able to load a normal pdf in MuPDFActivity, but when I tried to open a password protected file, the keyboard show up, than the app crash :( So close... The behavior let me think that only a little part is missing. Did I need to configure something or this part haven't been ported yet?

Thanks for your work!
LP

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.