PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Oct 19, 2019 9:21 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Apr 19, 2019 10:54 am 
Offline

Joined: Fri Apr 19, 2019 10:44 am
Posts: 2
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!


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 19, 2019 1:32 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 483
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 19, 2019 3:26 pm 
Offline

Joined: Fri Apr 19, 2019 10:44 am
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Sun Apr 21, 2019 8:16 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 483
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 240 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group