簡體   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