ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.08k stars 237 forks source link

Unable to create an XImage from a PDF? #126

Open StefH opened 4 years ago

StefH commented 4 years ago

Code:

var stream= File.OpenRead("c:\\temp\\A4.pdf");
var image = XImage.FromStream(() => stream);

-->

Unhandled exception. SixLabors.ImageSharp.UnknownImageFormatException: Image cannot be loaded. Available decoders:
 - TGA : TgaDecoder
 - BMP : BmpDecoder
 - PNG : PngDecoder
 - JPEG : JpegDecoder
 - GIF : GifDecoder

   at SixLabors.ImageSharp.Image.Load[TPixel](Configuration configuration, Stream stream, IImageFormat& format)
   at SixLabors.ImageSharp.Image.Load[TPixel](Stream stream, IImageFormat& format)
   at PdfSharpCore.Utils.ImageSharpImageSource`1.FromStreamImpl(String name, Func`1 imageStream, Nullable`1 quality)
   at MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes.ImageSource.FromStream(String name, Func`1 imageStream, Nullable`1 quality)
   at PdfSharpCore.Drawing.XImage..ctor(Func`1 stream)
   at PdfSharpCore.Drawing.XImage.FromStream(Func`1 stream)
   at ConsoleAppPDF.PDF.Make() in C:\dev\ConsoleAppPKI\ConsoleAppPDF\CombinePDF.cs:line 31
   at ConsoleAppPDF.Program.Main(String[] args) in C:\dev\ConsoleAppPKI\ConsoleAppPDF\Program.cs:line 9
JHBonarius commented 3 years ago

That's an easy one. Like the Readme says, PdfSharpCore uses SixLabors ImageSharp for image handling. And that only supports the image formats stated here. And NB: PDF is not an image format at all.

ststeiger commented 3 years ago

@JHBonarius: True, but that is not entirely correct. PDF basically is a vector-graphics format. For example, you can open a svg in chrome, and save it as pdf. Then you can zoom-in ad infinitum, without losss of quality.

But what certainly is impossible is an automatic conversion from raster graphics to vector graphics. However, you could actually automatically convert a vector-graphic into a raster-graphic. Actually, using AI or other sophisticated algorithms, you might be able to convert a raster graphic into a vector graphics. But both directions certainly aren't supported by ImageSharp.

@StefH: You can actually embed a PDF into a PDF, but not as XImage. You can use XPdfForm to load the pdf, then gfx.DrawImage(form, PdfSettings.VwsDrawingSettings.rectForm) to draw it. VB.NET example (using PdfSharp - change Namespace):


                Using form As PdfSharp.Drawing.XPdfForm = PdfSharp.Drawing.XPdfForm.FromStream(ms)
                    form.PageNumber = 1
                    ' Max-Width
                    ' Max-Height
                    Dim ActualDrawingSize As System.Drawing.Size = GetDrawingBoundingSize(form, PdfSettings.VwsDrawingSettings.Width, PdfSettings.VwsDrawingSettings.Height)

                    ' Draw the page identified by the page number like an image
                    PdfSettings.VwsDrawingSettings.rectForm = New PdfSharp.Drawing.XRect(PdfSettings.VwsDrawingSettings.X, PdfSettings.VwsDrawingSettings.Y, ActualDrawingSize.Width, ActualDrawingSize.Height)

                    ' Max-Width
                    ' Max-Height
                    PdfSettings.VwsDrawingSettings.rectFormMaxSize = New PdfSharp.Drawing.XRect(PdfSettings.VwsDrawingSettings.X, PdfSettings.VwsDrawingSettings.Y, PdfSettings.VwsDrawingSettings.Width, PdfSettings.VwsDrawingSettings.Height)

                    ' Centering
                    PdfSettings.VwsDrawingSettings.rectForm.X = PdfSettings.VwsDrawingSettings.rectForm.X + (PdfSettings.VwsDrawingSettings.rectFormMaxSize.Width - PdfSettings.VwsDrawingSettings.rectForm.Width) / 2.0
                    PdfSettings.VwsDrawingSettings.rectForm.Y = PdfSettings.VwsDrawingSettings.rectForm.Y + (PdfSettings.VwsDrawingSettings.rectFormMaxSize.Height - PdfSettings.VwsDrawingSettings.rectForm.Height) / 2.0

                    gfx.DrawImage(form, PdfSettings.VwsDrawingSettings.rectForm)
                    PdfSettings.Outline.[Do](gfx, PdfSettings.VwsDrawingSettings.rectForm)
                    PdfSettings.Outline.[Do](gfx, PdfSettings.VwsDrawingSettings.rectFormMaxSize, PdfSharp.Drawing.XColors.HotPink)

                    PdfSettings.VwsDrawingSettings.Outline(gfx, PdfSettings.VwsDrawingSettings.rectFormMaxSize)
                End Using ' XPdfForm form