我將.txt轉換爲.pdf。如果.txt文件不是空白,此工作正常。如果是,它會拋出「文檔沒有頁面」的錯誤。將C#中的空白.txt文件轉換爲PDF。
pdf生成後會出現「打開此文件時出錯,文件損壞,無法修復」的錯誤。
守則看到下面
public void converttxttoPDF(string sourcePath, string destPath)
{
try
{
iTextSharp.text.Document document = new iTextSharp.text.Document();
string filename = Path.GetFileNameWithoutExtension(sourcePath);
System.IO.StreamReader myFile = new System.IO.StreamReader(sourcePath);
string myString = myFile.ReadToEnd();
myFile.Close();
if (!Directory.Exists(destPath))
Directory.CreateDirectory(destPath);
iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(destPath + "\\" + filename + ".pdf", FileMode.CreateNew));
document.Open();
document.Add(new iTextSharp.text.Paragraph(myString));
document.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
讓我知道如果任何信息需要。
謝謝
沒有評論...? :-( – 2011-03-31 14:35:31
)你真的想要一個空白的PDF生成嗎?可能有什麼用途?爲什麼不首先檢查文本文件的內容,如果它是空白的則拋出一個消息框? – MAW74656 2011-03-31 16:24:41
@MAW:我知道它是沒有用來生成一個空白的PDF,但你知道測試人員,他們測試的一切..林:-) – 2011-03-31 17:35:49