Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a .SVG to pdf with PdfSharp

Tags:

c#

svg

pdfsharp

I'm using PdfSharp and saving .png files. It works fine. Now I need to save SVG images and I'm getting an error

System.OutOfMemoryException: Out of memory.

The code is

var path = @"D:\Projects\ProjectName\Content\Images\Instruments";
path += Path.GetFileName(instrument.Src); //instrument.src is a valid name and the path is a valid path on the local machine, which is where I'm testing

if (!File.Exists(path))
    return;  //never hit as the path is correct

var img = XImage.FromFile(path); //out of memory
//more code

If I change the above from .svg to .png it works fine (since I have both, a .png and .svg file with the same name)

How can I save the SVG image to PDF using PDF Sharp?

like image 300
MyDaftQuestions Avatar asked Nov 18 '25 16:11

MyDaftQuestions


1 Answers

PDFsharp supports raster images like PNG and JPEG. Vector images like SVG are not yet supported. This is not a bug, this is an implementation restriction. Pages from PDF files can be used like images and allow using vector images with PDFsharp.

XImage.FromFile passes the image to either GDI+ or WPF (depending on the build you are using) and expects to get a raster image in return. I do not know what GDI+ or WPF return for SVG images.

If you find source code that draws SVG images using a Graphics object then you can easily adopt that for PDFsharp's XGraphics object.

Or try to find a library that converts SVG to raster images or PDF files.

like image 134
I liked the old Stack Overflow Avatar answered Nov 21 '25 05:11

I liked the old Stack Overflow