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

Add page
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2671
Page 1 of 1

Author:  dlinaresg [ Mon Nov 25, 2013 10:07 pm ]
Post subject:  Add page

I have a group of images in a list and put them on a page. Thr problem is I don't know how to add new pages. What do I have to do?

The method:
Code:
 private void SaveImage(List<Image> lista)
        {
            PdfDocument doc = new PdfDocument();
            int imagEnDoc = 0;
            doc.Pages.Add(new PdfPage());           
            XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);           
            //ciclo recorre la lista de imágenes y las agrega al documento           
            int i = 0;
            while (i < lista.Count)
            {               
                int coordY = 60;               
                int posY = 1;   
                while( posY<5)
                {
                    int coordX = 40;
                    int posX=1;
                    while( posX<5)
                    {
                        if (imagEnDoc == lista.Count) break;
                        XImage imgToPdf = XImage.FromGdiPlusImage(lista[i]);                       
                        double height = imgToPdf.PixelHeight*0.35;
                        double width = imgToPdf.PixelWidth*0.35;
                        xgr.DrawImage(imgToPdf, coordX, coordY, width, height);                       
                        imagEnDoc++;
                        i++;
                        posX++;                       
                        coordX= coordX + 120;
                        //MessageBox.Show("Foto en documento: " + coordX.ToString() + ", " + coordY.ToString());
                    }
                    posY++;
                    coordY = coordY + 150;                   
                }               
            }           
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "PDF Files|*.pdf";
            saveFileDialog1.Title = "Save an Image File";
            saveFileDialog1.ShowDialog();           
            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                // Saves the Image via a FileStream created by the OpenFile method.
                System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();
                doc.Save(fs);
                fs.Close();
                MessageBox.Show("Documento guardado");
            }
            lista.Clear();       
        }

Author:  Thomas Hoevel [ Tue Nov 26, 2013 10:12 am ]
Post subject:  Re: Add page

You don't know how to add pages?
What is the purpose of this line:
Code:
doc.Pages.Add(new PdfPage());


You have to get a new XGraphics object for the new page to draw on it.


A typical usage would be something like this:
Code:
PdfPage page = outputDocument.AddPage();
gfx = XGraphics.FromPdfPage(page);

This code must be called whenever a new page is needed.

See also:
http://www.pdfsharp.net/wiki/PageSizes-sample.ashx

Author:  dlinaresg [ Tue Nov 26, 2013 3:24 pm ]
Post subject:  Re: Add page

Thank you very much for your kind reply. (I am really new with c# (and programming) and my english doesn't help me.). I have clear what you say but when I try, the program throw an ArgumentOutOfRangeException" in this line and can't understand why.

Code:
private void SaveImage(List<Image> lista)
        {
            PdfDocument doc = new PdfDocument();
            int page = 0;
            int imagEnDoc = 0;
            MessageBox.Show("Items en lista: " + lista.Count.ToString());
            //int i = 0;
            while (imagEnDoc < 16)
            {
                doc.Pages.Add(new PdfPage());
                XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[page]);

                //ciclo recorre la lista de imégenes y las agrega al documento           
                int i = 0;
                while (i < lista.Count)
                {
                    int coordY = 60;
                    int posY = 1;
                    while (posY < 5)
                    {
                        int coordX = 40;
                        int posX = 1;
                        while (posX < 5)
                        {
                            if (imagEnDoc == lista.Count) break;
                            XImage imgToPdf = XImage.FromGdiPlusImage(lista[i]);
                            double height = imgToPdf.PixelHeight * 0.35;
                            double width = imgToPdf.PixelWidth * 0.35;
                            xgr.DrawImage(imgToPdf, coordX, coordY, width, height);
                            imagEnDoc++;
                            i++;
                            posX++;
                            coordX = coordX + 120;
                            //MessageBox.Show("Foto en documento: " + coordX.ToString() + ", " + coordY.ToString());
                        }
                        posY++;
                        coordY = coordY + 150;
                    }
                    page++;
                    imagEnDoc = 0;
                }
            }

Author:  dlinaresg [ Tue Nov 26, 2013 3:25 pm ]
Post subject:  Re: Add page

Sorry, the line:
XImage imgToPdf = XImage.FromGdiPlusImage(lista[i]);

Author:  Thomas Hoevel [ Tue Nov 26, 2013 3:51 pm ]
Post subject:  Re: Add page

You should add "i < lista.Count" to all "while" statements.

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