Generate PDF images using PDFSharp -Windows Form

1

I'm using window form .net and would like to convert scanned images to PDF, thus generating PDF images. I'm using PDFSharp. I have the following code but it only generates a page. What commands should I use to generate grab the images and generate the PDF with pages. at first I think of loading a DataTable and after that read each of the lines and generate but I do not know how to do it .... Could you give me a tip?

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            string source = (e.Argument as string[])[0];
            string destinaton = (e.Argument as string[])[1];

            PdfDocument doc = new PdfDocument();
            doc.Pages.Add(new PdfPage());
            XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);


            XImage img = XImage.FromFile(source);
            doc.Pages[0].Width = XUnit.FromPoint(img.Size.Width);
            doc.Pages[0].Height = XUnit.FromPoint(img.Size.Height);
            xgr.DrawImage(img, 0, 0, img.Size.Width, img.Size.Height); 
            doc.Save(destinaton);
            doc.Close();
            success = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    
asked by anonymous 31.03.2015 / 16:08

1 answer

1

In the following snippet, you added the first page [1]:

    doc.Pages.Add(new PdfPage());

And added an image to the first page ( index = 0, indicated with the doc.pages [0]) code of your PdfDocument [2]:

>
    XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);    
    xgr.DrawImage(img, 0, 0, img.Size.Width, img.Size.Height);

To create more pages, you only need to instantiate a new page [1] and add the desired content to each new page [2].

    
15.04.2015 / 22:54