[英]iText7 Filestream Null Reference Error in C#
I'm attempting to convert a html file to a pdf file.我正在尝试将 html 文件转换为 pdf 文件。 This is my code:
这是我的代码:
using iText.Html2pdf
.
.
.
public bool HtmlToPDF(string folderName, string htmlFilePath, string otherNum)
{
Console.WriteLine("Running HtmltoPDF\n");
string outputPath = new Uri(folderName + "\\" + otherNum.pdf).LocalPath;
try
{
FileStream htmlSource = File.Open(htmlFilePath, FileMode.Open);
FileStream pdfDest = File.Open(outputPath, FileMode.Create);
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
Console.WriteLine("Successfully completed HtmltoPDF and returned true\n");
return true;
}
Running the code "HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);"运行代码“HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);” causes the following error to be thrown:
导致抛出以下错误:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=itext.io
StackTrace:
at iText.IO.Font.FontCache..cctor()
I'm really at a loss as this seems to be a simple operation.我真的很茫然,因为这似乎是一个简单的操作。 Any help would be greatly appreciated.
任何帮助将不胜感激。
You need a using
statement in your FileStream objects.您需要在 FileStream 对象中
using
语句。 If you don't use using
, they're not disposed.如果您不使用
using
,则不会丢弃它们。
using FileStream htmlSource = File.Open(htmlFilePath, FileMode.Open);
using FileStream pdfDest = File.Open(outputPath, FileMode.Create);
After C# 8.0 using
statements can be single line. C# 8.0 之后
using
语句可以是单行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.