c#对文档(doc、xls、pdf)的操作的终极dll(sprie.dll,aspose.dll,itextsharp.dll)

c#对文档(doc、xls、pdf)的操作的终极dll(sprie.dll,aspose.dll,itextsharp.dll)

(1)通常处理文档我们都是调用office相应的dll,没有水印效果也很好,但是缺点也很多。

  1. 其一电脑必须安装和开发相应版本的office,安装了其他版本的office或者wps之后出现excel异常来自 HRESULT:0x800A0****问题,很难解决;
  2. 其二代码中经常因为想用的exe进程没有合理关闭导出写的方法没有执行;
  3. 其三电脑必须安装office。

(2)采用sprie.dll,aspose.dll相应的doc、xls、pdf文档,无需安装office软件而且封装了很多好用的方法直接调用,但是这两种dll处理文档由于没有破解不完整转为pdf时存在水印,需要去除水印。如何去除每一个文档的头部水印或者末页水印:使用空白图片遮挡头部处理头部有水印的pdf,采用itextsharp.dll处理pdf文件没有水印的办法舍去末页有水印的pdf,从未完美解决水印问题。

去除头部水印代码

  /// <summary>
        /// 遮挡Spire.xls转pdf之后的头部水印
        /// </summary>
        /// <param name="src">有水印的pdf文件</param>
        /// <param name="dest">消除水印后pdf文件</param>
        public static void CoverUpWaterMark(string srcpdf, string destpdf)
        {
            PdfReader reader = new PdfReader(srcpdf);
            int n = reader.NumberOfPages;
            iTextSharp.text.Rectangle psize = reader.GetPageSize(1);
            float width = psize.Width;
            float height = psize.Height;
            iTextSharp.text.Document document = new iTextSharp.text.Document(psize, 30, 30, 30, 30);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destpdf, FileMode.Create));
            document.Open();
          
            PdfContentByte cb = writer.DirectContent;
            for (int i = 1; i <= n; i++)
            {
                document.NewPage();
                PdfImportedPage  page1 = writer.GetImportedPage(reader, i);
                cb.AddTemplate(page1, 0, 0);
                string kb = AppDomain.CurrentDomain.BaseDirectory + "kb.png";
                if (File.Exists(kb))
                {
                    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(kb);
                    img.ScaleAbsolute(img.Width / 2, img.Height / 2);
                    img.SetAbsolutePosition(7, height - 25);
                    cb.AddImage(img);
                }
            }
            writer.Flush();
            document.Close();
        }

去除最后一页水印代码

   /// <summary>
        /// 已有pdf中拷贝指定的页码范围到一个新的pdf文件中
        /// </summary>
        /// <param name="sourcePdfPath">源pdf文件</param>
        /// <param name="outputPdfPath">目标pdf文件</param>
        /// <param name="startPage">开始页</param>
        /// <param name="endPage">结束页</param>
        public static void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
        {
            PdfReader reader = null;
            Document sourceDocument = null;
            PdfCopy pdfCopyProvider = null;
            PdfImportedPage importedPage = null;
            try
            {
                reader = new PdfReader(sourcePdfPath);
                sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
                pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
                sourceDocument.Open();
                for (int i = startPage; i <= endPage; i++)
                {
                    importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                    pdfCopyProvider.AddPage(importedPage);
                }
                sourceDocument.Close();
                reader.Close();
            }
            catch (Exception ex) { throw ex; }
        }

需要相应的dll请联系QQ:1815222521
在这里插入图片描述
在这里插入图片描述

posted @   XYSGIS  阅读(169)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示