2023年7月14日发(作者:)
在e中如何将各种⽂档合并为PDF?Aspose快速搞定!在各种业务环境中,将各种⽂档合并为⼀个PDF是客户最常问的问题之⼀。例如,假设您的组织有多个应⽤程序以XPS和PDF⽣成特定的⽂档,使⽤扫描的图像,并且您的⽤户希望将其中⼀些⽂档合并为⼀个PDF。本⽂演⽰了如何使⽤ Core框架将多个⽂档合并到⼀个PDF中。提出了⼏种使⽤.NET合并PDF的⽅法,这些内容在本⽂中进⾏了介绍。在本⽂中,将讨论以下主题:如何使⽤ Core Web API上传PDF或其他⽂档;如何实现简单的Web UI来选择要合并的PDF⽂件;如何实现⽤于合并PDF的简单Web API容器;在本⽂中,我们将创建⼀个简单的 Web API应⽤程序,该应⽤程序允许我们上载⽂档,选择2个或更多⽂件进⾏合并以及下载结果。实施 Core Web App以将各种⽂档合并为PDF步骤1:创建⼀个 Core Web应⽤程序我们将为此应⽤程序使⽤Web应⽤程序(模型-视图-控制器)模板。创建基本应⽤程序后,我们将需要执⾏⼀些其他操作。为.NET库添加作为依赖项(通过Nuget软件包管理器);添加库;将临时⽂件和⽂档的wwwroot⽂件夹添加到该⽂件夹(例如files和temp);在中创建相应的属性"Folders": { "Files": "files", "Temporary" : "temp"
}
步骤2:实施Web API控制器以管理服务器上的⽂件我们的控制器应执⾏以下操作:返回具有某些扩展名的⽂件列表(在本⽰例中,将仅显⽰.pdf,.jpg和.oxps⽂件);允许按⽂件名下载⽂件;允许通过⽂件名删除服务器上的⽂件;using ;using g;using ;using g;using c;using ;using ;using uration;namespace llers{ [Route("api/[controller]")] [ApiController] public class FilesController : ControllerBase { private readonly Dictionary
_contentType = new Dictionary
[HttpGet] public IEnumerable
return (f => new FileViewModel { Name = , Size = }); } [HttpGet("{id}")] public IActionResult OnGetFile(string id) { _ormation($"Get file {id}"); var fileName = e(_storageRootFolder, id); return File(ad(fileName), _contentType[ension(fileName)]); } [HttpDelete("{id}")] public IActionResult OnDeleteFile(string id) { _ormation($"Delete file {id}"); var fileName = e(_storageRootFolder, id); (fileName); return Ok(); }
}}然后将使⽤附加的库来加载⽂件,因此将与加载⽂件相关的代码移⾄单独的控制器是有意义的。步骤3:实现Web API控制器以使⽤上传⽂件库的主要功能是它允许您分块加载⽂件。因此,我们需要实现⼀些⽅法来处理此过程:HTTP GET请求的⽅法,该⽅法应检查服务器上是否存在块;HTTP POST请求的⽅法,该⽅法应该是服务器上的上传块;其他辅助⽅法(⽤于HTTP OPTIONS请求,合并块等)using g;using ;using ;using g;using ;using uration;namespace llers{ [Route("api/[controller]")] [ApiController] public class UploadController : ControllerBase { private readonly ILogger_logger; private readonly string _storageRootFolder; private readonly string _filesRootFolder; public UploadController( ILoggerlogger, IConfiguration configuration, IWebHostEnvironment env) { _logger = logger; _storageRootFolder = e(tPath, configuration["Folders:Temporary"]); _filesRootFolder = e(tPath, configuration["Folders:Files"]); if (!(_storageRootFolder)) Directory(_storageRootFolder); } [HttpOptions] public object UploadFileOptions() { return Ok(); } [HttpGet] public object Upload(int resumableChunkNumber, string resumableIdentifier) { _ormation($"Check if chunck {resumableChunkNumber} from {resumableIdentifier} is here.");
return ChunkIsHere(resumableChunkNumber, resumableIdentifier) ? Ok() : StatusCode(418); } [HttpPost] public IActionResult Upload( [FromQuery(Name = "ResumableIdentifier")] string resumableIdentifier, [FromQuery(Name = "ResumableFilename")] string resumableFilename, [FromQuery(Name = "ResumableChunkNumber")] int resumableChunkNumber, [FromQuery(Name = "ResumableTotalChunks")] int resumableTotalChunks, IFormFile file) { _ormation(me); var stream = (GetChunkFileName(resumableChunkNumber, resumableIdentifier)); (stream); (); TryAssembleFile(resumableFilename, resumableIdentifier, resumableTotalChunks); return Ok(); } #region Chunk methods [NonAction] private string GetChunkFileName(int chunkNumber, string identifier) { return e(_storageRootFolder, $"{identifier}_{chunkNumber}"); } [NonAction] private string GetFilePath(string identifier) { return e(_storageRootFolder, identifier); } [NonAction] private bool ChunkIsHere(int chunkNumber, string identifier) { return (GetChunkFileName(chunkNumber, identifier)); } [NonAction] private bool AllChunksAreHere(string identifier, int chunks) { for (var chunkNumber = 1; chunkNumber <= chunks; chunkNumber++) if (!ChunkIsHere(chunkNumber, identifier)) return false; return true; }
[NonAction] private void DeleteChunks(string identifier, int chunks) { for (var chunkNumber = 1; chunkNumber <= chunks; chunkNumber++) { var
chunkFileName = GetChunkFileName(chunkNumber, identifier); (chunkFileName); } } [NonAction] private string
ConsolidateFile(string identifier, int chunks) { var path = GetFilePath(identifier); using var destStream = (path, 15000); for (var
chunkNumber = 1; chunkNumber <= chunks; chunkNumber++) { var chunkFileName = GetChunkFileName(chunkNumber, identifier); using var
sourceStream = ad(chunkFileName); (destStream); } (); return path; } [NonAction]
private void TryAssembleFile(string rfn, string ri, int rtc) { if (AllChunksAreHere(ri, rtc)) { // Create a single file var path = ConsolidateFile(ri, rtc); //
Move consolidated file (path, e(_filesRootFolder, rfn),true); // Delete chunk files DeleteChunks(ri, rtc); } }
#endregion } }该库将标识符⽤于内部⽬的。它可以以不同的⽅式⽣成。在⽰例应⽤程序中,我们使⽤了⼀个单独的控制器。using ;using System;using ;namespace llers{ [Route("api/[controller]")] [ApiController] public class TokenController : ControllerBase { // GET: api/Token?id=
[HttpPost] public IActionResult PostMergeFiles(IEnumerable
}}如您所见,我们的控制器调⽤HTTP-Post⽅法来合并⽂档。现在我们实现此⽅法。我们合并的想法是将所有页⾯从⼀个⽂档添加到另⼀个⽂档。这很简单,因为我们知道Document类包含⼀个Pages集合,⽽最后⼀个具有Add⽅法。// POST: /api/merge
[HttpPost] public IActionResult PostMergeFiles(IEnumerable
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689309355a228507.html
评论列表(0条)