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

C# 是否将子文件夹中的所有图像作为新页面添加到iTextSharp中的PDF文档?,c#,pdf,pdf-generation,itextsharp,C#,Pdf,Pdf Generation,Itextsharp,我的文件夹结构如下所示: 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

我的文件夹结构如下所示:

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

这是基本的想法。您可能需要调整图像的大小。如果是这样,只需像在.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();

string[]folders=System.IO.Directory.GetDirectories(@“C:\My Sample Path\”、“*”、System.IO.SearchOption.AllDirectories);获取所有子目录。string[]folders=System.IO.Directory.GetDirectory(@“C:\My Sample Path\”、“*”、System.IO.SearchOption.AllDirectory);获取所有子目录。