I got some Problems with Saving a Canvas to a A3 PDF.
I'm currently in my internship semester so it's the first time I’m working with PDFsharp so i'm sorry if there may a lot of easier ways to do this:)
First Problem i got was that if I use PdfSharp.
PageSize.A3 the Resolution was to low it was fuzzy. So i Added a new A3 Resolutions with 300dpi, called A3300.
I show all Canvas in an TabControle Window. From This Window i start creating my pdf.
Now it works fine if i create Just the first page(Canvas). But i need up to 10 pages. Then always the first page is ok and the others are empty. About this i found out if i Open the Canvas in an extra window before i create the pdf with ShowDialog() it works for all.. Im really confused.
Second Problem i'm running out of memory because the
XGraphics File got each page ~700MB later as PDF 0.7MB. I tried it with gfx.Dispose(); but it didn't work.
So what is the problem with the empty pages if i don't open an extra window and is there a way to delete the gfx file from the memory?
//PDF creatingCode:
try
{
s_document = new PdfDocument();
s_document.Info.Title = "PDFKlemmen";
s_document.Info.Author = Environment.UserName + " FESTO AG & Co. KG ";
s_document.Info.Subject = "Belegungsplan IO";
for (int i = 0; i < 2; i++) Can create 2 without memory exception
{
cCanvastest ct = new cCanvastest(Can[i]); //Open the Canvas ina window
ct.ShowDialog();
PdfSharp.Pdf.PdfPage page = s_document.AddPage();
switch ((int)dpi)
{
case 72: page.Size = PdfSharp.PageSize.A372; break;
case 96: page.Size = PdfSharp.PageSize.A396; break;
case 300: page.Size = PdfSharp.PageSize.A3300; break;
default: page.Size = PdfSharp.PageSize.A3300; dpi = 300d; break;
}
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)(1114 / 72 * dpi), (int)(1584 / 72 * dpi), dpi, dpi, PixelFormats.Pbgra32);
bitmap.Render(Can[i]);
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
BitmapImage bmp = new BitmapImage() { CacheOption = BitmapCacheOption.OnLoad };
System.IO.MemoryStream outStream = new System.IO.MemoryStream();
encoder.Save(outStream);
outStream.Seek(0, System.IO.SeekOrigin.Begin);
bmp.BeginInit();
bmp.StreamSource = outStream;
bmp.EndInit();
XImage image = XImage.FromBitmapSource(bmp);
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawImage(image, 0, 0, image.PixelWidth, image.PixelHeight);
gfx.Dispose();
image.Dispose();
outStream.Close();
ct.Close();
}
s_document.Save(sPfad);
}
I hope some one can help me:)