简体   繁体   中英

Add all images in subfolders to PDF document in iTextSharp as new pages?

I have a folder structure like this below:

D:\myphotos\2011-02-09\1.jpg
D:\myphotos\2011-02-10\2.jpg
....
......
............
D:\myphotos\2011-02-23\10.jpg

I need to add all these images as new pages to a pdf document using iTextSharp. How can I achieve this minimally?

Here's the basic idea. You might have to resize the images. If so, just resize the image like you normally would in .Net, save the image to a MemoryStream and create the Jpeg object from the raw bytes.

//Create a new document
iTextSharp.text.Document Doc = new iTextSharp.text.Document(PageSize.LETTER, 20, 20, 20, 20);
//Store the document on the desktop
string PDFOutput = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
PdfWriter writer = PdfWriter.GetInstance(Doc, new FileStream(PDFOutput, FileMode.Create, FileAccess.Write, FileShare.Read));

//Open the PDF for writing
Doc.Open();

string Folder = "C:\\Images";
foreach (string F in System.IO.Directory.GetFiles(Folder, "*.jpg")) {
    //Insert a page
    Doc.NewPage();
    //Add image
    Doc.Add(new iTextSharp.text.Jpeg(new Uri(new FileInfo(F).FullName)));
}

//Close the PDF
Doc.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM