arklumpus / VectSharp

A light library for C# vector graphics
GNU Lesser General Public License v3.0
226 stars 22 forks source link

How to merge Pages together? #21

Closed TealEgg closed 2 years ago

TealEgg commented 2 years ago

Hello, I was just wondering if it is possible to merge pages together, instead of creating a raster image of each page and putting all the images into one page? I want to do this, so the final file can be in svg/pdf format. Thanks in advance! Edit: I also tried using DrawGraphics, but I kept getting a VectSharp.UnbalancedStackException

arklumpus commented 2 years ago

Hi! Using the DrawGraphics method is indeed the right thing to do:

// Create the two pages and draw on them.
Page page1 = ...
Page page2 = ...

// Create the final "container" page.
Page finalPage = new Page(100, 100);

// Draw the first page on the container page.
finalPage.Graphics.DrawGraphics(0, 0, page1.Graphics);

// Draw the second page on the container page.
finalPage.Graphics.DrawGraphics(0, 0, page2.Graphics);

To avoid the UnbalancedStackException, you need to make sure that, for each page, the number of calls to Graphics.Save and the number of calls to Graphics.Restore are equal. The message of the exception should tell you which one is in excess.

TealEgg commented 2 years ago

How do make the number of calls to Save and Restore equal. For me, I have 1 Save and 0 Restores.

arklumpus commented 2 years ago

Just add a call to Restore somewhere, or remove the call to Save (which is doing nothing, if you never call Restore)!

TealEgg commented 2 years ago

Okay, now I'm just confused, they are equal, yet it still gives it an error: image

Edit: Just when I posted this, I fixed the problem. I was using the graphics of a page to draw graphics instead of creating a new graphics instance. Thanks for the help and sorry for the trouble!

arklumpus commented 2 years ago

Glad to hear that you got it working! Feel free to reopen this if you're still having issues. Also, if you share your code it might be easier to figure out what is the problem!