PdfSharpCore
PdfSharpCore copied to clipboard
Unable to create an XImage from a PDF?
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
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.
@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