【问题标题】:I need to pass value to another ActionResult我需要将值传递给另一个 ActionResult
【发布时间】: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


【解决方案1】:

如果您将 PDF 创建逻辑移到单独的类(甚至 BLL 项目)中,那就太好了。创建 PDF 的项目可能很长,您可以将其设为背景。

public class PdfCreator : ICreator // Not best naming :)
{
    public byte[] Create(string data) // Or you could pass your own class, dictionary, etc...
    {
        // Your PDF creation logic from public ActionResult CreateDocument()
    }
}

// To easily test and inject
public interface ICreator
{
    byte[] Create(string data);
}

// And in you Controller class
public class HomeController : Controller
{
    private readonly ICreator _pdfCreator = null;

    public HomeController(ICreator creator)
    {
        // You will receive it here from Dependency Injection framework or just initialize
        _pdfCreator = creator ?? new PdfCreator();
    }

    // Use it
    public ActionResult CreateDocument()
    {
        byte[] pdf = _pdfCreator.Create("My specific text");
        // Your logic goes here
    }
}

它可能比您的代码略大,但这让您可以对创建 PDF 的控制器进行单元测试,并使用不同的场景。

【讨论】:

    【解决方案2】:

    使用 Html.Action 从视图中调用 CreateDocument Action 并将您的模型作为参数传递:

    // View returned by ProcessFile
    @Html.Action("CreateDocument", Model)
    
    // Get model from view
    public ActionResult CreateDocument(IndexViewModel model)
    {
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-06
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多