3

I am calling a function in my class library project from WorkerService class. Both apps are using .net core 3.1. I am compiling the code locally on Windows 10 and using Visual Studio 2019.

What am I exactly missing here?

Here's my code using PdfSharp

                PdfDocument doc = new PdfDocument();
                doc.Options.FlateEncodeMode = PdfFlateEncodeMode.BestCompression;
                doc.Options.UseFlateDecoderForJpegImages = PdfUseFlateDecoderForJpegImages.Automatic;
                doc.Options.NoCompression = false;
                doc.Options.CompressContentStreams = true;
                Image MyImage = Image.FromFile(imageLocation);
                for (int PageIndex = 0; PageIndex < MyImage.GetFrameCount(FrameDimension.Page); PageIndex++)
                {
                    MyImage.SelectActiveFrame(FrameDimension.Page, PageIndex);
                    XImage img = XImage.FromGdiPlusImage(MyImage);
                    var page = new PdfPage();
                    page.Size = PageSize.Letter;
                    if (MyImage.Width > MyImage.Height)
                    {
                        // In case the scanned POD is landscape, rotate the image by 90 degrees
                        MyImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
                        page.Rotate = 270;
                    }
                    doc.Pages.Add(page);
                    XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[doc.Pages.Count - 1]);
                    xgr.DrawImage(img, 0, 0, page.Width, page.Height);
                    xgr.Dispose();
                }
                doc.Save(Savepath);
                doc.Close();

It is blowing up on this line

 Image MyImage = Image.FromFile(imageLocation);
frozenmirage
  • 55
  • 1
  • 2
  • 9

1 Answers1

1

The WHOLE concept of .net core is to reduce, if not outright remove dependances on windows code, and thus it becomes multi-platform, and even quite much CPU "processor" agnostic (allowing .net code to run on your phone, tablet, Linux, or even some small Raspberry device, and you don't even need a Intel/AMD processor for such code to run).

As such, you have to remove your dependences on windows.system.drawing of which PDF sharp uses. In fact, OFTEN using any non .net core library and reference is going to cause you much grief, since any .net library (non core) is going to often cause a dependency on windows.

I believe you can stick to text only, but if you are shoving in graphics, then you have to change your code.

there is a nuget package now here:

https://github.com/ststeiger/PdfSharpCore

It not 100% prime time ready, but some are reporting good success with that library.

A discussion of this issue can be found here:

https://github.com/empira/PDFsharp/issues/6

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Thank you for the answer. I downloaded nuget package PdfSharpCore but that one cannot find FromGdiPlusImage. For context, I am converting tif image to pdf. – frozenmirage Nov 09 '22 at 22:46
  • As noted, you can (apparently) consume .net assemblies from .net core. This may help: https://stackoverflow.com/questions/46788729/consume-net-core-library-from-net-standard-project. If you are running on a windows only platform, then you should be able to use + consume .net framework assemblies, and that would include pdf sharp. However, if you not running (or don't want to be limited to windows platform, then you have to use the pdfsharp core version - and you need to find/use/have a .net core image library. – Albert D. Kallal Nov 09 '22 at 22:53
  • That's what I thought! I am running on windows only platform and I am still presented with the error when I run the code. – frozenmirage Nov 09 '22 at 23:14
  • `FromGdiPlusImage` relies on GDI+. If the WorkerService class does not provide access to that assembly, then `FromGdiPlusImage` cannot be used. – I liked the old Stack Overflow Nov 10 '22 at 08:53
  • 2
    This is a non-answer -- I still have no idea why this library would complain "System.Drawing.Common is not supported on this platform" on Windows. – Eric M. Nov 27 '22 at 05:54