繁体   English   中英

将子文件夹中的所有图像作为新页面添加到iTextSharp中的PDF文档中?

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

我的文件夹结构如下:

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

我需要使用iTextSharp将所有这些图像作为新页面添加到pdf文档中。 我怎样才能做到这一点?

这是基本思想。 您可能需要调整图像大小。 如果是这样,只需像通常在.Net中一样调整图像大小,将图像保存到MemoryStream并从原始字节创建Jpeg对象。

//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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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