【发布时间】:2022-10-02 04:32:09
【问题描述】:
我正在使用 syncfusion OCR 扫描生成文档的 PDF,并将其作为最终结果推送以供下载。我正在尝试从流中获取文件并将其复制到我的服务器,但我收到一条错误消息流不支持阅读.这是我的代码
try
{
string binaries = Path.Combine(this._hostingEnvironment.ContentRootPath, \"Tesseractbinaries\", \"Windows\");
//Initialize OCR processor with tesseract binaries.
OCRProcessor processor = new OCRProcessor(binaries);
//Set language to the OCR processor.
processor.Settings.Language = Languages.English;
string path = Path.Combine(this._hostingEnvironment.ContentRootPath, @\"Data\\font\", \"times.ttf\");
FileStream fontStream = new FileStream(path, FileMode.Open);
//Create a true type font to support unicode characters in PDF.
processor.UnicodeFont = new PdfTrueTypeFont(fontStream, 8);
//Set temporary folder to save intermediate files.
processor.Settings.TempFolder = Path.Combine(this._hostingEnvironment.ContentRootPath, \"Data\");
//Load a PDF document.
FileStream inputDocument = new FileStream(Path.Combine(this._hostingEnvironment.ContentRootPath, \"Data\", \"pistone.pdf\"), FileMode.Open);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument);
//Perform OCR with language data.
string tessdataPath = Path.Combine(this._hostingEnvironment.ContentRootPath, \"tessdata\");
//string tessdataPath = Path.Combine(@\"tessdata\");
processor.PerformOCR(loadedDocument, tessdataPath);
//Save the PDF document.
MemoryStream outputDocument = new MemoryStream();
loadedDocument.Save(outputDocument);
outputDocument.Position = 0;
//Dispose OCR processor and PDF document.
processor.Dispose();
loadedDocument.Close(true);
//Download the PDF document in the browser.
FileStreamResult fileStreamResult = new FileStreamResult(outputDocument, \"application/pdf\");
fileStreamResult.FileDownloadName = \"OCRed_PDF_document.pdf\";
//setting a path for saving it to my server and copying it to the folder downloads
string filePath = Path.Combine(\"downloads\", fileStreamResult.FileDownloadName);
using (Stream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write))
{
fileStream.CopyTo(fileStream);
}
return fileStreamResult;
}
catch (Exception ex)
{
throw;
}
标签: c# asp.net-mvc syncfusion