PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Creating a PDF from a Canvas List
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2615
Page 1 of 1

Author:  Stefan Erler [ Wed Oct 16, 2013 3:03 pm ]
Post subject:  Creating a PDF from a Canvas List

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 creating
Code:
 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:)

Author:  Thomas Hoevel [ Wed Oct 16, 2013 4:51 pm ]
Post subject:  Re: Creating a PDF from a Canvas List

Hi!
Stefan Erler wrote:
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.
PDF files are vector files and have no DPI at all. You can call DrawImage to draw any image at any size (without adding dummy page sizes to PDFsharp).

I can't help with the canvas problem.

With respect to memory usage: There are two builds you can try: GDI+ and WPF.
You can do it in two phases: create 10 PDF files with one page each in the first phase, then concatenate them to one big PDF in the second phase. This may reduce the memory consumption.

Author:  Stefan Erler [ Thu Oct 17, 2013 8:03 am ]
Post subject:  Re: Creating a PDF from a Canvas List

Thank you for you Replay Thomas Hoevel
It helps me a lot.
Quote:
PDF files are vector files and have no DPI at all. You can call DrawImage to draw any image at any size (without adding dummy page sizes to PDFsharp).
I didn't know that, so it was just a DrawImage scale problem at all.

Quote:
create 10 PDF files with one page each in the first phase, then concatenate them to one big PDF
I used that way now, I didn’t tried that before because i thought if i will import them they use the same space as before...

So Just the Canvas problem Left.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/