【发布时间】:2019-01-03 04:18:03
【问题描述】:
我正在尝试从制表符分隔的文本文件中读取 HTML 并创建一个 HTML 文件,然后将其转换为 pdf。当我尝试读取文本文件时,我得到了 ' 和其他一些字符的奇怪字符。这是我的代码
var lines = System.IO.File.ReadAllLines(@"C:\temp\Laura.txt", Encoding.GetEncoding("Windows-1255"));
var csv = lines.Select(x =>
{
var parts = x.Split('\t');
return new Articles()
{
id = parts[0].Trim(),
name = parts[1].Trim(),
body = parts[2].Trim(),
//body = WebUtility.HtmlDecode(parts[2].Trim()),
//body = HttpUtility.HtmlEncode(parts[2].Trim()),
//body = WebUtility.HtmlEncode(parts[2].Trim()),
//body = SecurityElement.Escape(parts[2].Trim()),
};
}).ToList();
foreach (var item in csv)
{
string id = item.name;
string filename = item.name + ".html";
string body = item.body;
string path = @"c:\temp\" + filename;
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
File.WriteAllText(path, body);
Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();
Document document = ap.Documents.Open(path);
object oFalse = false;
object oTrue = true;
object OutputFileName = Path.Combine(
Path.GetDirectoryName(path),
Path.GetFileNameWithoutExtension(path) + ".pdf");
object missing = System.Reflection.Missing.Value;
document.PrintOut(
oTrue, // Background
oFalse, // Append
ref missing, // Range
OutputFileName, // OutputFileName
ref missing, // From
ref missing, // To
ref missing, // Item
ref missing, // Copies
ref missing, // Pages
ref missing, // PageType
ref missing, // PrintToFile
ref missing, // Collate
ref missing, // ActivePrinterMacGX
ref missing, // ManualDuplexPrint
ref missing, // PrintZoomColumn
ref missing, // PrintZoomRow
ref missing, // PrintZoomPaperWidth
ref missing // PrintZoomPaperHeight
);
}
}
我已经尝试了注释掉的代码,但似乎没有任何效果。
【问题讨论】:
-
任何示例输入和预期输出?
-
@laura-bejjani 你得到了什么?像这样?
-
@MohamedElrashid - 是的,这正是我为每个 '.对于其他角色,我也得到了类似的东西。在我运行 var parts = x.Split('\t');并查看部分,所以我猜测需要在该行之前完成一些编码/解码。
-
@BagusTesa - 我正在传递一个带有 ID、文件名和一些包含 ' 的 html 的文件。