PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Apr 28, 2024 3:48 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Add page
PostPosted: Mon Nov 25, 2013 10:07 pm 
Offline

Joined: Mon Nov 11, 2013 4:29 pm
Posts: 3
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();       
        }


Top
 Profile  
Reply with quote  
 Post subject: Re: Add page
PostPosted: Tue Nov 26, 2013 10:12 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Add page
PostPosted: Tue Nov 26, 2013 3:24 pm 
Offline

Joined: Mon Nov 11, 2013 4:29 pm
Posts: 3
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;
                }
            }


Top
 Profile  
Reply with quote  
 Post subject: Re: Add page
PostPosted: Tue Nov 26, 2013 3:25 pm 
Offline

Joined: Mon Nov 11, 2013 4:29 pm
Posts: 3
Sorry, the line:
XImage imgToPdf = XImage.FromGdiPlusImage(lista[i]);


Top
 Profile  
Reply with quote  
 Post subject: Re: Add page
PostPosted: Tue Nov 26, 2013 3:51 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
You should add "i < lista.Count" to all "while" statements.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 372 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group