读取多个文件
Read more than one file
我正在写一个 pdf 到 word 的转换器,它对我来说非常好用。但我希望能够转换多个文件。
现在发生的是它读取第一个文件并执行转换过程。
public static void PdfToImage()
{
try
{
Application application = null;
application = new Application();
var doc = application.Documents.Add();
string path = @"C:\Users\Test\Desktop\pdfToWord\";
foreach (string file in Directory.EnumerateFiles(path, "*.pdf"))
{
using (var document = PdfiumViewer.PdfDocument.Load(file))
{
int pagecount = document.PageCount;
for (int index = 0; index < pagecount; index++)
{
var image = document.Render(index, 200, 200, true);
image.Save(@"C:\Users\chnikos\Desktop\pdfToWord\output" + index.ToString("000") + ".png", ImageFormat.Png);
application.Selection.InlineShapes.AddPicture(@"C:\Users\chnikos\Desktop\pdfToWord\output" + index.ToString("000") + ".png");
}
string getFileName = file.Substring(file.LastIndexOf("\"));
string getFileWithoutExtras = Regex.Replace(getFileName, @"\", "");
string getFileWihtoutExtension = Regex.Replace(getFileWithoutExtras, @".pdf", "");
string fileName = @"C:\Users\Test\Desktop\pdfToWord\" + getFileWihtoutExtension;
doc.PageSetup.PaperSize = WdPaperSize.wdPaperA4;
foreach (Microsoft.Office.Interop.Word.InlineShape inline in doc.InlineShapes)
{
if (inline.Height > inline.Width)
{
inline.ScaleWidth = 250;
inline.ScaleHeight = 250;
}
}
doc.PageSetup.TopMargin = 28.29f;
doc.PageSetup.LeftMargin = 28.29f;
doc.PageSetup.RightMargin = 30.29f;
doc.PageSetup.BottomMargin = 28.29f;
application.ActiveDocument.SaveAs(fileName, WdSaveFormat.wdFormatDocument);
doc.Close();
}
}
我认为我的 foreach 应该不会出现这个问题。是的,这个文件夹中有不止一个 pdf
行
var doc = application.Documents.Add();
在 foreach
循环之外。因此,您只需为所有 *.pdf 文件创建一个 word 文档。
将上面的行移到 foreach
循环中,为每个 *.pdf 文件添加一个新的 word 文档。
我正在写一个 pdf 到 word 的转换器,它对我来说非常好用。但我希望能够转换多个文件。
现在发生的是它读取第一个文件并执行转换过程。
public static void PdfToImage()
{
try
{
Application application = null;
application = new Application();
var doc = application.Documents.Add();
string path = @"C:\Users\Test\Desktop\pdfToWord\";
foreach (string file in Directory.EnumerateFiles(path, "*.pdf"))
{
using (var document = PdfiumViewer.PdfDocument.Load(file))
{
int pagecount = document.PageCount;
for (int index = 0; index < pagecount; index++)
{
var image = document.Render(index, 200, 200, true);
image.Save(@"C:\Users\chnikos\Desktop\pdfToWord\output" + index.ToString("000") + ".png", ImageFormat.Png);
application.Selection.InlineShapes.AddPicture(@"C:\Users\chnikos\Desktop\pdfToWord\output" + index.ToString("000") + ".png");
}
string getFileName = file.Substring(file.LastIndexOf("\"));
string getFileWithoutExtras = Regex.Replace(getFileName, @"\", "");
string getFileWihtoutExtension = Regex.Replace(getFileWithoutExtras, @".pdf", "");
string fileName = @"C:\Users\Test\Desktop\pdfToWord\" + getFileWihtoutExtension;
doc.PageSetup.PaperSize = WdPaperSize.wdPaperA4;
foreach (Microsoft.Office.Interop.Word.InlineShape inline in doc.InlineShapes)
{
if (inline.Height > inline.Width)
{
inline.ScaleWidth = 250;
inline.ScaleHeight = 250;
}
}
doc.PageSetup.TopMargin = 28.29f;
doc.PageSetup.LeftMargin = 28.29f;
doc.PageSetup.RightMargin = 30.29f;
doc.PageSetup.BottomMargin = 28.29f;
application.ActiveDocument.SaveAs(fileName, WdSaveFormat.wdFormatDocument);
doc.Close();
}
}
我认为我的 foreach 应该不会出现这个问题。是的,这个文件夹中有不止一个 pdf
行
var doc = application.Documents.Add();
在 foreach
循环之外。因此,您只需为所有 *.pdf 文件创建一个 word 文档。
将上面的行移到 foreach
循环中,为每个 *.pdf 文件添加一个新的 word 文档。