一、nuget安装
<ItemGroup>
<PackageReference Include="Spire.PDF" Version="10.6.7" />
</ItemGroup>
二、直接上代码
cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
namespace pdfdemo.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
var path = $"{AppDomain.CurrentDomain.BaseDirectory}/133634930918199804.pdf";
var imgPath = $"{AppDomain.CurrentDomain.BaseDirectory}/1336349309181998041.png";
using (Spire.Pdf.PdfDocument document = new Spire.Pdf.PdfDocument())
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
document.LoadFromStream(stream);
using (var memoryStream = new System.IO.MemoryStream())
{
document.SaveToImageStream(0, memoryStream, "image/png");
System.IO.File.WriteAllBytes(imgPath, memoryStream.ToArray());
}
}
}
return View();
}
}
}
运行效果: