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:
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!