Generate PDF images using Pdfsharp -Windows Form

Asked

Viewed 786 times

1

I am using window form . net and would like to convert scanned images to PDF thus generating images pages in PDF. I’m using the Pdfsharp. I have the following code but it only generates a page. Which commands should I use to generate the images and generate the PDF with pages. At first I think about loading a Datatable and after that read each of the lines and generate but I do not know how to do.... Could you give me a hint?

    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);
        }
    }

1 answer

1

In the following excerpt, 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 just need to instantiate a new page[1] and add to each new page the desired content[2].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.