[英]C# Convert pdf to txt
我熱烈歡迎...我有一個問題,我正在嘗試將PDF轉換為txt,但是我無法保存txt文件? 有人請幫我嗎?
using System;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System.IO;
namespace ZestawienieFaktur
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] filePaths = Directory.GetFiles(@"D:\\faktury\\", "*.pdf");
foreach (string fp in filePaths)
{
ExtractTextFromPdf(fp);
}
}
public static string ExtractTextFromPdf(string path)
{
using (PdfReader reader = new PdfReader(path))
{
StringBuilder text = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
}
string lines = text.ToString();
using (var file = new StreamWriter(@"D:\faktury\test1.txt"))
{
file.WriteLine(lines);
file.Close();
}
}
}
}
}
在文件夾中,我有一些不同名稱的pdf文件。 而且我想將所有格式都轉換為txt格式。 謝謝...
您應該改為刪除return
關鍵字,而僅返回void
。 它不執行的原因是因為它在return
之后停止執行其余代碼。 更改為此:
public static void ExtractTextFromPdf(string path)
{
using (PdfReader reader = new PdfReader(path))
{
StringBuilder text = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
}
string lines = "";
using(var file = new StreamWriter(path2))
{
file.WriteLine(lines);
file.Close();
}
}
}
希望能幫助到你!
好的,謝謝朋友...
using System;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System.IO;
namespace ZestawienieFaktur
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] filePaths = Directory.GetFiles(@"D:\faktury\", "*.pdf");
foreach (string fp in filePaths)
{
ExtractTextFromPdf(fp);
}
}
public static string ExtractTextFromPdf(string path)
{
using (PdfReader reader = new PdfReader(path))
{
StringBuilder text = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
}
string lines = text.ToString();
using (var file = new StreamWriter(@"D:\faktury\test1.txt"))
{
file.WriteLine(lines);
file.Close();
}
return lines;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.