【发布时间】:2019-04-08 12:14:04
【问题描述】:
程序从图像文件中读取文本,然后将其显示在网站上。但我也想从该文本创建一个 pdf 文件。我找到了一种创建 pdf 的方法,但我无法将文本值传递给这个 pdf 创建操作。我在我的项目中使用 Tesseract OCR 和 Syncfusion.Pdf.Net.Core NuGet 包。第一个用于从图像中读取文本。后者用于从文本创建 pdf。
这是我的 HomeController.cs 代码
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult ProcessFile(IFormFile file)
{
if (file == null || file.Length == 0)
return Content("file not selected");
using (var engine = new TesseractEngine(Path.Combine(_environment.WebRootPath, "tessdata"), "eng", EngineMode.Default))
{
using (var image = new Bitmap(file.OpenReadStream()))
{
using (var page = engine.Process(image))
{
IndexViewModel model = new IndexViewModel();
model.Text = page.GetText();
model.MeanConfidence = page.GetMeanConfidence();
return View("Index", model);
}
}
}
}
public ActionResult CreateDocument()
{
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Set the standard font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);
//Draw the text
graphics.DrawString("", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
//Saving the PDF to the MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Download the PDF document in the browser.
FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");
fileStreamResult.FileDownloadName = "Output.pdf";
return fileStreamResult;
}
【问题讨论】:
-
“但我无法将文本值传递给此 pdf 创建操作” - 为什么不呢?你还没有描述你的问题。
标签: c# asp.net-core ocr