C# iText 7 切分PDF

一、itext

我要使用itext做一个pdf的页面大小一致性处理,然后再根据数据切分出需要的pdf.

iText的官网有关于它的介绍,https://itextpdf.com/ 然后在官网可以查找api文档https://api.itextpdf.com/。

其中我要使用的是itext7+,主要在 iText.Kernel.Pdf 命名空间下。

C# iText 7 切分PDF

二、处理PDF页面大小一致

由于原始PDF 是扫描图片合成来的,有些页面扫描的图片规格不一致,导致pdf阅读性很差。

C# iText 7 切分PDF

对于这个pdf我进行处理,首先是在nuget 里面搜索 itext 进行安装,使用itext7。

C# iText 7 切分PDF

处理PDF大小方法:

        public void RestPageSize(string sourcePdfPath, string outputPdfPath)
        {
            PdfReader pdfReader = null;
            PdfDocument pdfDocument = null;
            PdfWriter pdfWriter = null;
            PdfDocument outPDfDoc = null;
            try
            {
                pdfReader = new PdfReader(sourcePdfPath);
                pdfDocument = new PdfDocument(pdfReader);
                var outDir = System.IO.Path.GetDirectoryName(outputPdfPath);
                if (!Directory.Exists(outDir))
                {
                    Directory.CreateDirectory(outDir);
                }

                pdfWriter = new PdfWriter(outputPdfPath);
                outPDfDoc = new PdfDocument(pdfWriter);

                outPDfDoc.SetDefaultPageSize(PageSize.A3);

                for (int i = 1; i < pdfDocument.GetNumberOfPages() + 1; i++)
                {
                    var page = pdfDocument.GetPage(i);
                    var formXObject = page.CopyAsFormXObject(outPDfDoc);
                    var xPercent = PageSize.A3.GetWidth() / page.GetPageSize().GetWidth();
                    var yPercent = PageSize.A3.GetHeight() / page.GetPageSize().GetHeight();
                    PdfCanvas pdfCanvas = new PdfCanvas(outPDfDoc.AddNewPage());
                    pdfCanvas.AddXObjectWithTransformationMatrix(formXObject, xPercent, 0, 0, yPercent, 0, 0);
                }

                pdfWriter.Flush();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                if (pdfReader != null)
                {
                    pdfReader.Close();
                }
                if (pdfDocument != null)
                {
                    pdfDocument.Close();
                }
                if (outPDfDoc != null)
                {
                    outPDfDoc.Close();
                }
                if (pdfWriter != null)
                {
                    pdfWriter.Close();
                    pdfWriter.Dispose();
                }
            }

折叠

思路:遍历原来的PDF页码,将原来的PDF页码对象拷贝 PdfFormXObject到要生成的PDF文档中,首先要copy页面对象才能使用,不然直接获取的page对象是原来文档的,我们无法操作。

var formXObject = page.CopyAsFormXObject(outPDfDoc);

然后对页面进行缩放计算,我们新的PDF默认设置成A3大小,通过计算原始页面和新页面宽高比例进行缩放。

计算完成后,在新文档中使用 PdfCanvas 对象新添加一页,然后将 PdfFormXObject 写入到新添加的页中。

处理后的PDF:

C# iText 7 切分PDF

三、切分PDF

切分PDF 就比较简单了,直接从原始文件中拷贝页面到新PDF文档中就行了。

切分PDF 方法:

        public void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
        {
            PdfReader pdfReader = null;
            PdfDocument pdfDocument = null;
            PdfWriter pdfWriter = null;
            PdfDocument outPDfDoc = null;
            try
            {
                pdfReader = new PdfReader(sourcePdfPath);
                pdfDocument = new PdfDocument(pdfReader);
                var outDir = Path.GetDirectoryName(outputPdfPath);
                if (!Directory.Exists(outDir))
                {
                    Directory.CreateDirectory(outDir);
                }

                pdfWriter = new PdfWriter(outputPdfPath);
                outPDfDoc = new PdfDocument(pdfWriter);

                pdfDocument.CopyPagesTo(startPage, endPage, outPDfDoc);
                pdfWriter.Flush();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }
            finally
            {
                if (pdfReader != null)
                {
                    pdfReader.Close();
                }
                if (pdfDocument != null)
                {
                    pdfDocument.Close();
                }
                if (outPDfDoc != null)
                {
                    outPDfDoc.Close();
                }

                if (pdfWriter != null)
                {
                    pdfWriter.Close();
                    pdfWriter.Dispose();
                }
            }

        }

折叠

注意:对写入流要进行 pdfWriter.Flush()将缓冲区数据写入PDF后再关。

四、添加PDF图片水印

        public static void SetWatermark(string sourcePdfPath, string outputPdfPath)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourcePdfPath), new PdfWriter(outputPdfPath));
            Document document = new Document(pdfDoc);
            PdfCanvas over;
            PdfExtGState gs1 = new PdfExtGState();

Original: https://www.cnblogs.com/Leo_wl/p/16531067.html
Author: HackerVirus
Title: C# iText 7 切分PDF

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/545656/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 你不得不知的python apply()

    大家好,我是小寒 原文链接 今天给大家带来一篇 如何在 pandas 上使用 apply 方法,如果觉得不错,欢迎关注起来。 本文的内容主要如下: 在 Pandas Series …

    技术杂谈 2023年7月25日
    088
  • 研发过程中的文档管理与工具

    写文档也是技术活 01:实践 对于多数开发同学来说,很多时候即讨厌没有研发文档,但是自己又不愿意常写文档,痛且倔强着; 程序员该不该写文档,与争论哪种编程语言最好一样,想撕的嘴不留…

    技术杂谈 2023年7月23日
    076
  • 随意写文件命令?怎么向屏幕输出带空格的字符串,比如”hello world”?

    写文件命令:vi 向屏幕输出带空格的字符串:echo hello world Java Program! Original: https://www.cnblogs.com/pro…

    技术杂谈 2023年5月31日
    097
  • OpenSSL命令—pkcs7

    用途: 用于处理DER或者PEM格式的pkcs#7文件。 用法: openssl pkcs7 [-inform PEM|DER] [-outform PEM|DER] [-in f…

    技术杂谈 2023年5月31日
    0106
  • golang interface用法

    接口是一个或多个方法签名的集合,任何类型的方法集中只要拥有与之对应的全部方法,就表示它”实现”了该接口无须在该类型上显式添加接口声明。 所谓对应方法,是指有…

    技术杂谈 2023年5月31日
    0118
  • AWS产品目录

    Loading 计算 Amazon EC2:弹性虚拟机 AWS Batch:批处理计算 Amazon ECR:Docker容器管理 Amazon ECS:高度可扩展的快速容器管理服…

    技术杂谈 2023年5月30日
    089
  • c json解析示例

    json-c是最主流的json c库。[root@hs-10-20-30-193 build]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local ….

    技术杂谈 2023年6月1日
    099
  • 【微信公众号】对接公众号别忘了配置域名权限

    在开发我的唯一客服系统的时候,增加了对接公众号的功能 需要在下面设置里,增加上域名权限 设置与开发===> 公众号设置 ===> 功能设置 ===>各种域名添加,…

    技术杂谈 2023年6月1日
    0133
  • ArcGIS Pro SDK 分级专题

    List listClassBreaks = new List { new CIMClassBreak { Symbol = SymbolFactory.Instance.Cons…

    技术杂谈 2023年5月30日
    095
  • 使用Animate.css

    Animate.css是一个css动画库,可以做出一些非常好看的动画; 官网:https://animate.style Animate.css非常容易上手,但是动画是一开始就加载…

    技术杂谈 2023年7月23日
    088
  • 说透缓存一致性与内存屏障

    https://www.cnblogs.com/chanmufeng/p/16523365.html Original: https://www.cnblogs.com/diyun…

    技术杂谈 2023年5月31日
    099
  • 运算符(1)

    算数运算符 在Java中,使用算术运算符+、-、*、/表示加、减、乘、除运算。当参与/运算的两个操作数都是整数时,表示整数除法运算;否则,表示浮点除法。整数的求余操作(取模)用%表…

    技术杂谈 2023年7月11日
    074
  • SpringBoot-druid

    SpringBoot-druid 9.1 druid简介 Java程序很大一部分要操作数据库,为了提高性能操作数据库,又不得不使用数据库连接池。 Druid 是阿里巴巴开源平台上一…

    技术杂谈 2023年6月21日
    0138
  • FlinkSQL之Windowing TVF

    Windowing TVF 在Flink1.13版本之后出现的替代之前的Group window的产物,官网描述其 is more powerful and effective s…

    技术杂谈 2023年7月24日
    075
  • Pycharm报错 TypeError: argument of type ‘WindowsPath‘ is not iterable 问题处理【转】

    问题描述: 修改python代码后,项目报错提示信息为:TypeError: argument of type ‘WindowsPath’ is not i…

    技术杂谈 2023年5月31日
    0106
  • B树详解

    B树系列文章 1. B树-介绍 2. B树-查找 3. B树-插入 4. B树-删除 什么是B树 B树(英语:B-tree)是一种自平衡的树,能够保持数据有序。使用B树这种数据结构…

    技术杂谈 2023年6月21日
    0105
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球