Introduction
This tip is an extension of the article Converting Image File to PDF.
Background
Often, there is a need to convert many files into single PDF or many PDFs. This article describes a simple program allowing this. The program is based on the Open Source library called PDFSharp.
Instead of single TextBox
for input files, there is a ListBox
enabling to select more files. With Up/Down button, you can change the source files order. The order in the ListBox
represents the order of the pages in the created PDF document. The source image files can be added as well via Drag&Drop operation from file explorer. Alternatively can be checked automatic delete of source files after succesful convertion.
Using the Code
Details about the conversion technique is described in the base article Converting Image File to PDF. The processing can run in two modus: single PDF with multiple pages, or multiple PDF with single page. The core worker thread is calling the PDFSharp library in the following way:
for (int i = 1; i < fNames.Length; i++)
{
PdfDocument doc = new PdfDocument();
toolStripStatusLabel1.Text = "Processing " + fNames[i];
doc.Pages.Add(new PdfPage());
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(fNames[i]);
xgr.DrawImage(img, 0, 0);
img.Dispose();
xgr.Dispose();
FileInfo fi = new FileInfo(fNames[i]);
doc.Save(fi.FullName.Replace(fi.Extension,".PDF"));
doc.Close();
}
Points of Interest
This is a very simple WinForm application helping to produce PDF file in an effective way.