我需要将值传递给另一个ActionResult

时间:2019-04-08 12:14:05

标签: c# asp.net-core ocr

程序从图像文件中读取文本,然后将其显示在网站上。但我也想从该文本创建一个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;

}

2 个答案:

答案 0 :(得分:0)

如果将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的控制器进行单元测试。

答案 1 :(得分:-1)

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

warning: not deleting branch 'fix/my-branch' that is not yet merged to
     'refs/remotes/origin/fix/my-branch', even though it is merged to HEAD.
error: The branch 'fix/my-branch' is not fully merged.