我有一个文件夹,其中包含超过200K的图像。某些图像将遵循以下文件名模式:
5093_1.jpg
5093_2.jpg
5093_3.jpg
5093_4.jpg
5093_5.jpg
5094_1.jpg
5094_2.jpg
5094_3.jpg我的计划是使用iTextSharp将每组图像合并成PDF。当我说一组图片时,下面的图片
5093_1.jpg
5093_2.jpg
5093_3.jpg
5093_4.jpg
5093_5.jpg将变为5093.pdf,其余为5094.pdf。
类似于下面的内容
iTextSharp.text.Document Doc = new iTextSharp.text.Document(PageSize.LETTER);
//Store the document on the desktop
string PDFOutput = Path.Combine(PDFFolder, "PDFs", tmp[0] + "_" + tmp[1].Replace(".jpg", "") + ".pdf");
PdfWriter writer = PdfWriter.GetInstance(Doc, new FileStream(PDFOutput, FileMode.Create, FileAccess.Write, FileShare.Read));
//Open the PDF for writing
Doc.Open();
Doc.NewPage();
//Doc.Add(new iTextSharp.text.Jpeg(new Uri(fi.FullName)));
Image jpg = Image.GetInstance(new Uri(fi.FullName));
jpg.ScaleToFit(700f, 700f);
Doc.Add(jpg);
Doc.Close();我的问题是,我确实找到了所有5093或任何数字顺序的文件,这样我就可以循环并粘贴它们为PDF。
非常感谢你的帮助
发布于 2012-06-27 23:02:25
var path = //your path
var files = Directory.GetFiles(path, "*_*.jpg");
//group only by the bit of the filename before the '_'
var groupedBySamePre_Value = files.GroupBy(p => Path.GetFileNameWithoutExtension(p).Split('_')[0]);
foreach (var group in groupedBySamePre_Value)
{
//this is a new file group pdf
foreach (var file in group.OrderBy(p => p))
{
//add the file to the pdf
}
//end of file group pdf
}发布于 2012-06-27 23:00:31
也许你可以这样做:
Regex regex= new Regex(@"[0-9]+_[0-9].jpg");
var files = Directory.GetFiles(yourPath, "*.jpg").
Where(path => regex.IsMatch(path)).ToList();在regex中,我假定格式如下
{at least one number}_{one number}.jpg对你来说应该很管用。
发布于 2012-06-27 23:02:23
另一个使用Array.Sort的建议是:
DirectoryInfo dirInfo=new DirectoryInfo(imageDirPath);
FileInfo fileInfos = dirInfo.GetFiles(*_*.jpg);
Array.Sort(fileInfos, delegate(FileInfo f1, FileInfo f2) {
return f1.Name.CompareTo(f2.Name);
});https://stackoverflow.com/questions/11229077
复制相似问题