تغییر نام دسته جمعی تعدادی فایل PDF بر اساس متادیتای فایلها
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۱۰/۲۳ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System.IO;
using iTextSharp.text.pdf;
namespace BatchRename
{
class Program
{
private static string getTitle(PdfReader reader)
{
string title;
reader.Info.TryGetValue("Title", out title); // Reading PDF file's meta data
return string.IsNullOrWhiteSpace(title) ? string.Empty : title.Trim();
}
private static string getSubject(PdfReader reader)
{
string subject;
reader.Info.TryGetValue("Subject", out subject); // Reading PDF file's meta data
return string.IsNullOrWhiteSpace(subject) ? string.Empty : subject.Trim();
}
static void Main(string[] args)
{
var dir = @"D:\Path";
if (!dir.EndsWith(@"\"))
dir = dir + @"\";
foreach (var file in Directory.GetFiles(dir, "*.pdf"))
{
var reader = new PdfReader(file);
var title = getTitle(reader);
var subject = getSubject(reader);
reader.Close();
string newFile = string.Empty;
if (!string.IsNullOrWhiteSpace(title))
{
newFile = dir + title + ".pdf";
}
else if (!string.IsNullOrWhiteSpace(subject))
{
newFile = dir + subject + ".pdf";
}
if (!string.IsNullOrWhiteSpace(newFile))
File.Move(file, newFile);
}
}
}
}