Didstopia / PDFSharp

A .NET Standard 2.0 library for reading, writing and editing PDF files.
MIT License
52 stars 18 forks source link

NullRefecenceException while creating instance of XImage #29

Closed willsam100 closed 6 years ago

willsam100 commented 6 years ago

Description: While trying to draw an image onto the PDF, an instance of XImage can not be created and throws an exception.

Error:

While calling `XImage.FromStream`` the following exception is thrown.

Object reference not set to an instance of an object.
   at Didstopia.PDFSharp.Drawing.XImage..ctor(Func`1 stream, Boolean isJepg)
   at Didstopia.PDFSharp.Drawing.XImage.FromStream(Func`1 stream, Boolean isJepg)
   at SqueakyApp.Applic.generatePdf@292-4.Invoke(Unit unitVar) in /Users/sam.williams/projects/SqueakyApp/SqueakyApp/SqueakyApp.fs:line 293
   at Microsoft.FSharp.Control.AsyncPrimitives.CallThenInvoke[T,TResult](AsyncActivation`1 ctxt, TResult result1, FSharpFunc`2 part2)
   at Microsoft.FSharp.Control.AsyncPrimitives.unitAsync@607.Invoke(AsyncActivation`1 ctxt)
   at Microsoft.FSharp.Control.AsyncPrimitives.unitAsync@607.Invoke(AsyncActivation`1 ctxt)
   at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction)

This also occurs if a file path is used.

Expected: To back an instance of XImage that can then be drawn onto the PDF using XGraphics instance.

Environment: The main code is a .NETStandard 2.0 project. The runtimes i have tried included .netcore, mono, and mono-android. I would like to use this on both mono-android and mono-ios

Didstopia.PDFSharp 1.0.0-beta7 nuget package was used.

Full environment details:

=== Visual Studio Community 2017 for Mac ===

Version 7.5.3 (build 7)
Installation UUID: 8d3712bd-ee56-4387-80b1-d5ddc495dce3
Runtime:
    Mono 5.10.1.57 (2017-12/ea8a24b1bbf) (64-bit)
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac 4.4.1.178 (master / eeaeb7e6)

    Package version: 510010057

=== NuGet ===

Version: 4.3.1.4445

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
    2.0.5
    2.0.0
    2.0.0-preview2-25407-01
    1.1.2
    1.1.1
    1.0.5
    1.0.4
SDK: /usr/local/share/dotnet/sdk/2.1.4/Sdks
SDK Versions:
    2.1.4
    2.0.0
    2.0.0-preview2-006497
    1.0.4
    1.0.1
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.10.1/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.6.2
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: 8.3.3.2 (Visual Studio Community)
Android SDK: /Users/sam.williams/Library/Developer/Xamarin/android-sdk-macosx
    Supported Android versions:
        6.0 (API level 23)
        7.0 (API level 24)
        7.1 (API level 25)
        8.0 (API level 26)
        8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 26.0.2
SDK Build Tools Version: 26.0.3

Java SDK: /usr
Unable to find a $JAVA_HOME at "/usr", continuing with system-provided Java...
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Apple Developer Tools ===

Xcode 9.4.1 (14161)
Build 9F2000

=== Xamarin.Mac ===

Version: 4.4.1.193 (Visual Studio Community)

=== Xamarin.iOS ===

Version: 11.12.0.4 (Visual Studio Community)
Hash: 64fece5f
Branch: d15-7
Build date: 2018-05-29 20:00:44-0400

=== Xamarin Inspector ===

Version: 1.4.0
Hash: b3f92f9
Branch: master
Build date: Fri, 19 Jan 2018 22:00:34 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 705030007
Git revision: 13cecd02aceddf29a1ed57b86f81c02994df1483
Build date: 2018-06-14 15:48:08-04
Xamarin addins: 7065de97cf22c9038fdc39dd627f2c30790fd8af
Build lane: monodevelop-lion-d15-7

=== Operating System ===

Mac OS X 10.13.5
Darwin 17.6.0 Darwin Kernel Version 17.6.0
    Tue May  8 15:22:16 PDT 2018
    root:xnu-4570.61.1~1/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

MvvmCross Template pack 2.0.1
AddinMaker 1.4.2
Paket 0.4
WakaTime 3.0.3
xUnit.NET 2 testing framework support 0.7.5
Redth's Addins 1.0.9
Internet of Things (IoT) development (Preview) 7.5
willsam100 commented 6 years ago

I ran this from source, and found the the exception is from ImageSource:line 43, ImageSourceImpl is null. I also can't find anywhere the value is set, or any subclasses (given ImageSource is abstract).

Are images supported? It looks like this hasn't been implemented.

jlu015 commented 6 years ago

Images are supported, but currently you have to provide your own implementation of ImageSource and IImageSource, as in this example - ImageSource acts as a factory for image classes, which have to provide methods to save them as BMP and JPEG.

Once you have this implementation, you pass it to PDFSharp through setting ImageSourceImpl:

// using MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes;
ImageSource.ImageSourceImpl = new MyPortableImageSource();
willsam100 commented 6 years ago

thanks for the help. I have this working now.

As a possible improvement, it could be useful to put a put a try/catch in if the value is null and re-throw with a message to implement it.

feel free to close any time you want.

Dids commented 6 years ago

Just to update on this: the library now throws an exception when the ImageSource implementation is missing. :)