.Net5 WebApi中操作PDF(分割复制PDF指定页 and 合并多个PDF文件)

.Net5 WebApi中操作PDF(分割复制PDF指定页 and 合并多个PDF文件)

  • 安装Nuget包
  • PDF工具类 and 合并多个PDF为一个PDF

安装Nuget包

.Net Framework:install-package iTextSharp
.Net Core/.Net5:install-package iTextSharp.LGPLv2.Core(我安装的1.7.1)

PDF工具类 and 合并多个PDF为一个PDF


using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Levox.Framework.Core.CommonUtils;
using Levox.Framework.Core.Extensions;
using Levox.Framework.Core.Models.Exceptions;
using Levox.Framework.Core.Models.File;namespace Levox.Framework.Core.Pdfs
{public class PdfUtil{/// /// 复制目标PDF文件的指定页内容,生成一个新的PDF文件   LastUpdateDate:2021-06-29 14:36:33.875  Author:Lingbug/// /// 新PDF文件名(自动识别是否带有.pdf后缀),为空默认为OutputSplitPdfFileName/// 目标PDF文件在系统内的相对路径/// 要复制的页集合,从1开始/// 新的PDF文件相对路径public static string CreatePdfByCustomPages(string outputPdfFileName, string sourcePdfPath, IEnumerable<int> pageNumbers){//校验if (sourcePdfPath.IsNullOrWhiteSpaceString()) throw new ErpFriendlyException("要分割复制的PDF源文件路径不能为空!");//校验if (pageNumbers.IsNullOrEmptyList()) throw new ErpFriendlyException("要复制的PDF页码集合不能为空!");//去重排序pageNumbers = pageNumbers.Distinct().OrderBy(r => r);//校验if (pageNumbers.First() <= 0) throw new ErpFriendlyException("要复制的PDF页码必须大于0!");//完整路径string sourcePdfFullPath = FileUtil.GetFullPath(sourcePdfPath);//校验if (!File.Exists(sourcePdfFullPath)) throw new ErpFriendlyException("要复制分割的PDF源文件不存在!");//读取源文件var reader = new PdfReader(sourcePdfFullPath);//校验if (pageNumbers.Last() > reader.NumberOfPages) throw new ErpFriendlyException($"要复制的PDF页码必须小于等于源文件总页数【{reader.NumberOfPages}】!");//结果文件var fileOutput = GetOutputPdfFileInfo(outputPdfFileName, "OutputSplitPdfFileName");//初始化var sourceDocument = new Document(reader.GetPageSizeWithRotation(pageNumbers.First()));//初始化var pdfCopyProvider = new PdfCopy(sourceDocument, new FileStream(fileOutput.FileFullPath, FileMode.Create));//打开sourceDocument.Open();//复制指定页foreach (var pageNumber in pageNumbers) pdfCopyProvider.AddPage(pdfCopyProvider.GetImportedPage(reader, pageNumber));//关闭sourceDocument.Close();//关闭reader.Close();//返回return fileOutput.FilePath;}/// /// 将多个PDF文件合并为一个PDF文件   LastUpdateDate:2021-06-29 16:23:23.438  Author:Lingbug/// /// 新PDF文件名(自动识别是否带有.pdf后缀),为空默认为OutputMergePdfFileName/// 要合并的源PDF文件相对路径集合/// 新的PDF文件相对路径public static string MergePdfs(string outputPdfFileName, params string[] sourcePdfPathList){//校验if (sourcePdfPathList.IsNullOrEmptyList()) throw new ErpFriendlyException("要合并的PDF相对路径集合不能为空!");//过滤sourcePdfPathList = sourcePdfPathList.Where(r => !r.IsNullOrWhiteSpaceString()).Distinct().ToArray();//校验if (sourcePdfPathList.IsNullOrEmptyList()) throw new ErpFriendlyException("要合并的PDF相对路径集合不能为空!");//获取完整路径var sourcePdfFullPathList = sourcePdfPathList.Select(sourcePdfPath =>{//完整路径string sourcePdfFullPath = FileUtil.GetFullPath(sourcePdfPath);//校验if (!File.Exists(sourcePdfFullPath)) throw new ErpFriendlyException("要合并的PDF源文件不存在:" + sourcePdfPath);//返回return sourcePdfFullPath;}).ToArray();//结果文件var fileOutput = GetOutputPdfFileInfo(outputPdfFileName, "OutputMergePdfFileName");//读取器PdfReader reader;//文档Document doc = new Document();//写入器PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(fileOutput.FileFullPath, FileMode.Create));//打开文档doc.Open();//内容容器PdfContentByte cb = writer.DirectContent;//页PdfImportedPage newPage;for (int i = 0; i < sourcePdfFullPathList.Length; i++){//读取文件reader = new PdfReader(sourcePdfFullPathList[i]);//总页数int totalPages = reader.NumberOfPages;for (int j = 1; j <= totalPages; j++){//读取页newPage = writer.GetImportedPage(reader, j);//页大小doc.SetPageSize(reader.GetPageSize(j));//开启新页doc.NewPage();//填充内容cb.AddTemplate(newPage, 0, 0);}}//关闭文档doc.Close();//返回return fileOutput.FilePath;}/// /// 构建输出PDF文件信息   LastUpdateDate:2021-06-29 16:14:28.454  Author:Lingbug/// /// /// /// private static SaveFileOutput GetOutputPdfFileInfo(string outputPdfFileName, string defaultOutputPdfFileName){//默认名称if (outputPdfFileName.IsNullOrWhiteSpaceString()) outputPdfFileName = defaultOutputPdfFileName;//格式化outputPdfFileName = outputPdfFileName.AutoAppendEnd(".pdf");//结果文件return new SaveFileOutput(outputPdfFileName, "UploadFiles", "Temp", "Pdfs");}}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部