import SVG file into page on specific position
Hi i have svg icons which i need to put on one page and generate a pdf from in in vectorformat how can we do this ?
Hi, you need to install the VectSharp, VectSharp.SVG and VectSharp.PDF nuget packages. Then you can do something like this:
using System;
using VectSharp;
using VectSharp.PDF;
namespace SVGIconPDF
{
class Program
{
static void Main(string[] args)
{
// Create a new document
Document outputDocument = new Document();
// Create a new page and add it to the document
Page page = new Page(500, 500);
outputDocument.Pages.Add(page);
// Access the page's Graphics object
Graphics gpr = page.Graphics;
// Open the icon file
Page svgIcon = VectSharp.SVG.Parser.FromFile(@"C:\Users\Giorgio\Downloads\rocket.svg");
Console.WriteLine("Nominal icon size: {0}x{1}", svgIcon.Width, svgIcon.Height);
// Draw full-size icon at the origin
gpr.DrawGraphics(0, 0, svgIcon.Graphics);
// Draw resized 100x100 icon at (x, y) = (300, 400)
double targetWidth = 100;
double targetHeight = 100;
double scaleX = targetWidth / page.Width;
double scaleY = targetHeight / page.Height;
gpr.Save();
gpr.Scale(scaleX, scaleY);
gpr.DrawGraphics(300 / scaleX, 400 / scaleY, svgIcon.Graphics);
gpr.Restore();
// Save the output as a PDF file
outputDocument.SaveAsPDF("output.pdf");
}
}
}
Most SVG files should work fine, but bear in mind that VectSharp does not support 100% of the features of SVG. If you have a particular SVG file that is not loaded correctly, I'd be happy to have a look at it and see if the library can be updated to support it.
Hi I am unable to load the svg file into the document In attachment you find a svg
Met vriendelijke groeten,
Wim De Vriendt [email protected]mailto:[email protected] +32(0)473 20 47 01
From: Giorgio Bianchini [email protected] Sent: Tuesday, 8 September 2020 17:25 To: arklumpus/VectSharp [email protected] Cc: Wim De Vriendt [email protected]; Author [email protected] Subject: Re: [arklumpus/VectSharp] import SVG file into page on specific position (#1)
Hi, you need to install the VectSharp, VectSharp.SVG and VectSharp.PDF nuget packages. Then you can do something like this:
using System;
using VectSharp;
using VectSharp.PDF;
namespace SVGIconPDF
{
class Program
{
static void Main(string[] args)
{
// Create a new document
Document outputDocument = new Document();
// Create a new page and add it to the document
Page page = new Page(500, 500);
outputDocument.Pages.Add(page);
// Access the page's Graphics object
Graphics gpr = page.Graphics;
// Open the icon file
Page svgIcon = VectSharp.SVG.Parser.FromFile(@"C:\Users\Giorgio\Downloads\rocket.svg");
Console.WriteLine("Nominal icon size: {0}x{1}", svgIcon.Width, svgIcon.Height);
// Draw full-size icon at the origin
gpr.DrawGraphics(0, 0, svgIcon.Graphics);
// Draw resized 100x100 icon at (x, y) = (300, 400)
double targetWidth = 100;
double targetHeight = 100;
double scaleX = targetWidth / page.Width;
double scaleY = targetHeight / page.Height;
gpr.Save();
gpr.Scale(scaleX, scaleY);
gpr.DrawGraphics(300 / scaleX, 400 / scaleY, svgIcon.Graphics);
gpr.Restore();
// Save the output as a PDF file
outputDocument.SaveAsPDF("output.pdf");
}
}
}
Most SVG files should work fine, but bear in mind that VectSharp does not support 100% of the features of SVG. If you have a particular SVG file that is not loaded correctly, I'd be happy to have a look at it and see if the library can be updated to support it.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/arklumpus/VectSharp/issues/1#issuecomment-688952370, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAGDUD73IL5EWEQF3WO2NIDSEZEC7ANCNFSM4Q776LKQ.
You can download 3 svg files with this link https://www.pureautomation.be/downloads/icons.zip
OK, the issue with those files was twofold: first, they specify the width and height in millimeters, which was causing the parser to fail. Second, they define the fill colour of the path using a CSS stylesheet, which was not supported.
If you try updating VectSharp.SVG to version 1.2.0, that should fix both issues. Let me know if it solves the problem for you!
Hi, my icons must have 10mmx10mm when i get height and size i get 1000 x 1000 what's the resolution you are using ?
Hi the pagesize is this in pixels ?
Hi i have problems with other icons as well can you check pls you can download them at https://www.pureautomation.be/downloads/stickericons.zip
Hi Can you check all icons. Some of them are not showing good. Is this possible ?
Hi, so:
-
The resolution of the PDF file is 72dpi, which means that 10mm are equivalent to 10 / 25.3 * 72 ≈ 28.5 units in VectSharp.
-
The size that you get from Page.Width and Page.Height after loading an SVG file is the size of the SVG "viewbox". These are essentially arbitrary units that are used to define the coordinates of the objects in the file. You can use them to know how much you have to scale the image to get to a certain size: for example, to get a 10mm icon, you need to resize the image so that it has a width and height of 28.5 units, i.e. use a scaling factor of 28.5 / Page.Width (= 0.0285 if the icon's viewbox is 1000x1000).
-
The issue with the "window " icons was that the PDF output was using a non-zero fill rule. VectSharp.PDF version 2.1.2 fixes this by enabling the even-odd fill rulse, if you upgrade all the icons should work fine (see stickericons.pdf).
Thanks for your fast answer. Can we also higher the pdf resolution or is this always by default 72dpi ? For me it's bit new
Not really, resolution in DPI is more of a thing for raster images; the PDF format does not have a concept of "resolution" per se. The "physical" size of a PDF document in inches is (almost) always the size in user units (i.e. the numbers you use in VectSharp) divided by 72.
This has no practical effect when you go and print the file, because since everything is in vector format, you can scale losslessly as much as you want.
You could always do something like this:
Document doc = new Document();
doc.Pages.Add(new Page(28.3, 28.3)); //Add a new page with a size of 1cm x 1cm
Graphics gpr = doc.Pages.Last().Graphics; //Now, 1cm is 28.3 units
gpr.FillRectangle(0, 0, 28.3, 28.3, Colours.Red); //Fill a 1cm x 1cm square
gpr.Scale(28.3 / 1000, 28.3 / 1000); //Now, 1cm is 1000 units
gpr.FillRectangle(0, 0, 1000, 1000, Colours.Blue); //Fill a 1cm x 1cm square
Or even:
double cm = 10.0 / 25.4 * 72.0; //Define 1cm in VectSharp/PDF units.
Document doc = new Document();
doc.Pages.Add(new Page(1 * cm, 1 * cm)); //Add a new page with a size of 1cm x 1cm
Graphics gpr = doc.Pages.Last().Graphics;
gpr.FillRectangle(0, 0, 1 * cm, 1 * cm, Colours.Red); //Fill a 1cm x 1cm square
gpr.FillRectangle(0.25 * cm, 0.25 * cm, 0.5 * cm, 0.5 * cm, Colours.Blue); //Fill a centered 0.5cm x 0.5cm square
In this latter example, you do not even have to think about PDF units anymore after the first line, and you can always work in "cm" (or inches, or whatever you prefer).
Hi I am missing something I would think this would make a rectangle of 0.5 x 0.5 cm -> but it is always filling the complete box 1cm x 1cm
//72dpi = 72 dot per inch ==> 2.83 = 1 mm
Document doc1 = new Document();
doc1.Pages.Add(new Page(28.3, 28.3)); //Add a new page with a size of 1cm x 1cm
Graphics gpr1 = doc1.Pages.Last().Graphics; //Now, 1cm is 28.3 units
//gpr1.FillRectangle(0, 0, 28.3, 28.3, Colours.Red); //Fill a 1cm x 1cm square
gpr1.Scale(1000 / 28.3, 1000 / 28.3); //Now, 1cm is 1000 units
gpr1.FillRectangle(0, 0, 500, 500, Colours.Red); //Fill a 1cm x 1cm square
gpr1.Save();
gpr1.Restore();
// Save the output as a PDF file
doc1.SaveAsPDF("output1.pdf");
the scale in your sample should be 28.3/1000. now its working
Hi everything works but now i need to rotate the complete graphics 180 degrees in the pdf how can i do that ?
Another question i need to streaam the pdf document to the user by MemoryStream memStream = new MemoryStream(); return memstream.toarray();
Yes, you are right, I fixed the sample!
If you want to rotate everything at the end without changing how you draw the icons in the first place, you could draw everything on a temporary Graphics first, then rotate the Page's Graphics and draw the temporary Graphics on that.
Something like:
// Create a temporary Graphics object
Graphics uprightGraphics = new Graphics();
// Draw the icons on the uprightGraphics
// Width and height of the finished page.
double width = 28.3 * 8;
double height = 28.3 * 4;
// Create the actual document and page
Document doc = new Document();
doc.Pages.Add(new Page(width, height));
Graphics gpr = doc.Pages.Last().Graphics;
// Rotate by 180° around the centre of the page
gpr.RotateAt(Math.PI, new Point(width * 0.5, height * 0.5));
// Draw the temporary uprightGraphics
gpr.DrawGraphics(0, 0, uprightGraphics);
Returning the PDF as a byte array should be as simple as:
// Create the MemoryStream
using MemoryStream memStream = new MemoryStream();
// Write the document to the stream
doc.SaveAsPDF(memStream);
// Return as a byte array
return memStream.ToArray();
Hi, I have an issue with svg images with TEXT in it. Can you look at the zip file. In the bin debug folder you find 2 pdfs, one with icon 20.svg and then icons are not aligned good if i have no text in icons they appear good
you can find my test project in https://www.pureautomation.be/downloads/TestVectSharpConsole.zip https://www.pureautomation.be/downloads/TestVectSharpConsole.zip
Can you have a look at it please ? TestVectSharpConsole.zip
OK, this was due to a bug affecting SVG files with a text element with a transform containing one or more tspan elements (the graphics state was not properly restored after the tspan elements had been processed). I belive I have fixed this, it should work if you update VectSharp.SVG to version 1.2.1.
Hi it's working but is it normal i have to build for x64? If I use Any cpu i get badimage format exception and the reason is the dependency ExCSS.dll That one is only working with x64 is that correct ? thanks
I have downloaded all the sources and compiled with anycpu now it's working so i cannot use nuget packages because the require x64