PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Multple Images from dir to PDF using PDFsharp
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3944
Page 1 of 1

Author:  AutoFace [ Fri Apr 19, 2019 10:54 am ]
Post subject:  Multple Images from dir to PDF using PDFsharp

Hi, I've been try to figure this out for a couple of days now and I'm having a little bit of trouble. I'm using PDFsharp.
I'm basically writing an application that converts numbers to barcodes and then exports all barcodes in to a single pdf file.

Currently I'm at the stage where all the barcodes are created and then saved in a folder as png. Then a loop takes place which should add all of the images to a pdf but the final pdf is only showing a single image.

This is the code I'm using to acheive this:
Code:
void GeneratePDF()
        {
            int i = 0;
            // Create new list for images
            List<Image> barCodeImages = new List<Image>();

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

            foreach (FileInfo barCodeImage in new DirectoryInfo(@"C:\Barcodes\" + dirText.Text).GetFiles())
            {
                // Add images to list
                barCodeImages.Add(Image.FromFile(barCodeImage.FullName));

                //convert Image to XImage for PDF
                Image img = barCodeImages[i];
                MemoryStream stream = new MemoryStream();
                img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

                //Continue creating PDF
                XImage xImg = XImage.FromStream(stream);
                xgr.DrawImage(xImg, 0, 0);
                img.Dispose();
                xgr.Dispose();

                i = i + 1;
            }
           
            // Save to destination file
            FileInfo fi = new FileInfo(@"C:\Barcodes\" + dirText.Text + "\\" + dirText.Text + " - PWAD" + ".png");
            doc.Save(fi.FullName.Replace(fi.Extension, ".pdf"));
            doc.Close();
        }


The following is what is created:
Image

Image

As you can see the files are all exported to a directory but the pdf file only has 1 barcode inside.
Idealy I would like to have 2 collumns of barcodes and the document would generate more pages if needed.

Any one know where I'm going wrong with this?

Many thanks!

Author:  TH-Soft [ Fri Apr 19, 2019 1:32 pm ]
Post subject:  Re: Multple Images from dir to PDF using PDFsharp

Hi!

I can see that you dispose the "xgr" after drawing the first image, so the first image is the only one that actually gets drawn.

Author:  AutoFace [ Fri Apr 19, 2019 3:26 pm ]
Post subject:  Re: Multple Images from dir to PDF using PDFsharp

TH-Soft wrote:
Hi!

I can see that you dispose the "xgr" after drawing the first image, so the first image is the only one that actually gets drawn.



Hi, Thanks for getting back to me. I too noticed this but but i thought with it being in a loop that once it gets destroyed it would get recreated during the next loop.

Even so, I have tried removing and moving it out of the loop but still I am met with the same result which is a single barcode in a pdf file.

Any other ideas?


Thanks

Author:  TH-Soft [ Sun Apr 21, 2019 8:16 am ]
Post subject:  Re: Multple Images from dir to PDF using PDFsharp

So maybe now you paint all images stacked at the same location and thus you only see the last image. Change the co-ordinates within the loop.

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