首页 > 其他分享 >Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--PDF操作

Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--PDF操作

时间:2024-02-02 20:11:06浏览次数:26  
标签:doc Pdfium Free pathPdf using var PDF new

项目地址:

Pdfium.Net:https://github.com/1000374/Pdfium.Net

PdfiumViewer:https://github.com/1000374/PdfiumViewer

Pdfium.Net支持对PDF操作:

  • 转成图片
  • 拆分
  • 合并
  • 插入
  • 替换
  • 删除
  • 旋转
  • 导出缩略图
  • 多页合并成一页

1.转成图片

1.基于GDI+下:

  /// <summary>
  /// Renders a page of the PDF document to the provided graphics instance. <see cref="Platforms.Windows"/>
  /// </summary>
  /// <param name="graphics">Graphics instance to render the page on.</param>
  /// <param name="dpiX">Horizontal DPI.</param>
  /// <param name="dpiY">Vertical DPI.</param>
  /// <param name="bounds">Bounds to render the page in.</param>
  /// <param name="rotation"><see cref="FpdfRotation"/></param>
  /// <param name="flags">Flags used to influence the rendering.</param>
  public void Render(Graphics graphics, float dpiX, float dpiY, Rectangle bounds, FpdfRotation rotation, RenderFlags flags)
        

示例代码:

 var pathPdf = "./Pdfium.NetTests/resources/annotation_highlight_long_content.pdf";
 using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
 {
     var page0 = doc.Pages[0];
     Image image = new Bitmap((int)page0.Width, (int)page0.Height);
     var g = Graphics.FromImage(image);
     page0.Render(g, g.DpiX, g.DpiY, new Rectangle(
         AdjustDpi(g.DpiX, 0),
         AdjustDpi(g.DpiY, 0),
         AdjustDpi(g.DpiX, image.Width),
         AdjustDpi(g.DpiY, image.Height)),FpdfRotation.Rotate0,RenderFlags.None);
     image.Save("./Pdfium.NetTests/Render.png");
 }

  

2.不基于GDI+

  /// <summary>
  /// Renders a page of the PDF document to an image.
  /// </summary>
  /// <param name="width">Width of the rendered image.</param>
  /// <param name="height">Height of the rendered image.</param>
  /// <param name="dpiX">Horizontal DPI.</param>
  /// <param name="dpiY">Vertical DPI.</param>
  /// <param name="rotate">Rotation.</param>
  /// <param name="flags">Flags used to influence the rendering.</param>
  /// <returns>The rendered image.</returns>
  public Image Render(int width, int height, float dpiX, float dpiY, FpdfRotation rotate, RenderFlags flags)
       

 

示例代码:

  var pathPdf = "./Pdfium.NetTests/resources/annotation_highlight_long_content.pdf";
  using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
      for (int i = 0; i < doc.PageCount; i++)
      {
          using (var image = doc.Pages[i].Render((int)doc.Pages[i].Width * 4 / 3, (int)doc.Pages[i].Height * 4 / 3, 96, 96, FpdfRotation.Rotate0, RenderFlags.Annotations | RenderFlags.CorrectFromDpi))
          {

          }
      }

  

3.不基于GDI+ 获取指定区域

区域坐标可通过框选获取,详情查看此

Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--PDF预览器框选

        /// <summary>
        /// Renders the page.
        /// </summary>
        /// <param name="width">
        /// The full width of the rendered image in px.
        /// If 0, width will be calculated from height using the correct apsect ratio.
        /// Height and width can not be both set to zero. 
        /// </param>
        /// <param name="height">
        /// The full wiheightth of the rendered image in px.
        /// If 0, height will be calculated from height using the correct apsect ratio.
        /// Height and width can not be both set to zero.
        /// </param>
        /// <param name="clipX">X value of the start point of the clipping area</param>
        /// <param name="clipY">Y value of the start point of the clipping area</param>
        /// <param name="clipWidth">Width of the clip area</param>
        /// <param name="clipHeight">Height of the clip area</param>
        /// <param name="dpiX">DPI to render page. If set, width and height will accept percentage.</param>
        /// <param name="dpiY">DPI to render page. If set, width and height will accept percentage.</param>
        /// <param name="rotate">Specify rotation.</param>
        /// <param name="flags">Specify flags.</param>
        /// <returns>Image from the page.</returns>
        public Image Render(int width, int height, int clipX, int clipY, int clipWidth, int clipHeight, float dpiX, float dpiY, FpdfRotation rotate, RenderFlags flags)

  

示例代码:

 var pathPdf = "./Pdfium.NetTests/resources/annotation_highlight_long_content.pdf";
 using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
     for (int i = 0; i < doc.PageCount; i++)
     {
         using (var image = doc.Pages[i].Render(
             (int)doc.Pages[i].Width,
             (int)doc.Pages[i].Height,
             16, // x of the top/left of clipping rectangle
            283, // y of the top/left point of clipping rectangle
            555, // width of clipping reactangle
            316, // height of clipping reactangle
            0,//DPIx
            0,//DPIy
            FpdfRotation.Rotate0, // no rotation
            RenderFlags.None // no render flags
                                ))
         {
             Assert.AreEqual(555, image.Width);
         }
     }

  

2.拆分

可使用封装好的函数直接获取每一页后保存

     var pathPdf = "./Pdfium.NetTests/resources/viewer_ref.pdf";
     using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
     {
         for (int i = 0; i < doc.PageCount; i++)
         {
             var doci = doc.GetPDFPage(i);
         }
     }

 

也可直接使用doc对象保存

  var pathPdf = "./Pdfium.NetTests/resources/viewer_ref.pdf";
  using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
  {
      for (int i = 0; i < doc.PageCount; i++)
      {
          var newDocument = PdfDocument.CreateNew();
          if (newDocument.Pages.Add(doc, i))
          {
              newDocument.Save($"./Pdfium.NetTests/AddTest{i}.pdf");
          }
      }
  }

  

3.合并

  var pathPdf = "./Pdfium.NetTests/resources/viewer_ref.pdf";
  using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
  using (var doc1 = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
  {
      doc.MergePDF(doc1);
  }

4.插入

 var pathPdf = "./Pdfium.NetTests/resources/viewer_ref.pdf";
 var pathPdf1 = "./Pdfium.NetTests/resources/zero_length_stream.pdf";

 using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
 using (var doc1 = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf1))))
 {
     doc.ImportPage(doc1, 1);
 }

  

5.替换  

 var pathPdf = "./Pdfium.NetTests/resources/viewer_ref.pdf";
 var pathPdf1 = "./Pdfium.NetTests/resources/zero_length_stream.pdf";

 using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
 using (var doc1 = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf1))))
 {
     doc.DeletePage(1);
     doc.ImportPage(doc1, 1);
 }

 

6.删除

 var pathPdf = "./Pdfium.NetTests/resources/viewer_ref.pdf";
 using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
 {
      doc.DeletePage(0); 
 }

  

也可通过以下删除

  var pathPdf = "./Pdfium.NetTests/resources/viewer_ref.pdf";
  using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
  {
      doc.Pages.RemoveAt(0);
  }

  

7.旋转

pdf可进行360度旋转 具体看枚举值FpdfRotation

 /// <summary>
 /// Specifies the rotation of pages shown in the PDF renderer.
 /// </summary>
 public enum FpdfRotation
 {
     /// <summary>
     /// Rotates the output 0 degrees.
     /// </summary>
     Rotate0,
     /// <summary>
     /// Rotates the output 90 degrees.
     /// </summary>
     Rotate90,
     /// <summary>
     /// Rotates the output 180 degrees.
     /// </summary>
     Rotate180,
     /// <summary>
     /// Rotates the output 270 degrees.
     /// </summary>
     Rotate270
 }

示例:

 var pathPdf = "./Pdfium.NetTests/resources/viewer_ref.pdf";
 using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
 {
     doc.RotatePage(0,FpdfRotation.Rotate90);
 }

  

8.导出缩略图

  var pathPdf = "./Pdfium.NetTests/resources/simple_thumbnail.pdf";
  using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
      for (int i = 0; i < doc.PageCount; i++)
      {
          var image = doc.Pages[i].RenderThumbnail();
      }

  

9.多页合并成一页

函数声明:

  • num_pages_on_x_axis :横向几页合并成一页默认2

  • num_pages_on_y_axis :纵向几页合并成一页默认1

   /// <summary>
   /// Create a new document from |src_doc|.  The pages of |src_doc| will be combined to provide |num_pages_on_x_axis x num_pages_on_y_axis| pages per |output_doc| page.
   /// number of pages per page = num_pages_on_x_axis * num_pages_on_y_axis
   /// </summary>
   /// <param name="output_width">The output page width in PDF "user space" units.</param>
   /// <param name="output_height">The output page height in PDF "user space" units.</param>
   /// <param name="num_pages_on_x_axis">The number of pages on X Axis.</param>
   /// <param name="num_pages_on_y_axis">The number of pages on Y Axis.</param>
   /// <returns>A handle to the created document, or NULL on failure.</returns>
   public PdfDocument ImportNPagesToOne(float output_width, float output_height, uint num_pages_on_x_axis = 2, uint num_pages_on_y_axis = 1)

  

 var pathPdf = "./Pdfium.NetTests/resources/rectangles_multi_pages.pdf";
 using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
 {
     using (var doc1 = doc.ImportNPagesToOne(612 * 2, 792))
{}
 }

  

标签:doc,Pdfium,Free,pathPdf,using,var,PDF,new
From: https://www.cnblogs.com/xiaohemiao/p/18002765

相关文章

  • CSharp: QuestPDF 2023.12.4 in doenet 8.0
     /*ide:vs202217.5.net8.0QuestPDF23.12.4from:https://github.com/QuestPDF/QuestPDF/discussions/560*/namespaceConsoleAppFontPdfDemo{usingQuestPDF;usingQuestPDF.Fluent;usingQuestPDF.Infrastructure;usingQuest......
  • laravel8 生成pdf
    privatestaticfunctioncreatePdfSendEmail($job_notes_id){#信息$recruit_job_notes_auth=DB::table('notes_auth')->select(["mobile","job_id"])->find($job_notes_id);if(!$recruit_job_note......
  • JS直接下载PDF文件
    if(ext=="pdf"){varindex=_fileUri.lastIndexOf('/');varname=_fileUri.substr(index+1);varxhr=newXMLHttpRequest();xhr.open('get',_fileUri,true);xhr.responseType='b......
  • 《计算机程序的构造和解释(原书第2版)》PDF
    内容简介《计算机程序的构造和解释》成型于美国麻省理工学院(MIT)多年使用的一本教材,1984年出版,1996年修订为第二版。在过去的二十多年里,该书对于计算机科学的教育计划产生了深刻的影响。在第二版中,大部分重要程序设计系统都重新修改并做过测试,包括各种解释器和编译器。作者根据其后......
  • 【解决方案】Word启动报错:“文件未找到:xxx...Adobe...MacPDFM.framework...”
    ✨报错提示运行时错误“53”:文件末找到:/Library/ApplicationSupport/Adobe/MACPDFM/MacPDFM.framework/Versions/A/MacPDFM✨解决方案实际上是由于AdobeAcrobatProDC卸载残留导致该问题解决方案如下cd~/Library/Group\Containers/UBF8T346G9.Office/User\Con......
  • python提取PDF中表格数据到数据库
    工作中遇到的问题:需要从PDF中提取表格数据,并数据落到MySQL库中。具体情况如下图所示:目录页:代码数据页:使用python实现代码如下(水平有限,此代码仅供自用):importpdfplumberpdf=r'XXX.pdf'wookroot=pdfplumber.open(pdf)pages=wookroot.pagestable_text=''forpage......
  • 【专题】2023年新消费趋势行业报告汇总PDF合集分享(附原数据表)
    原文链接:https://tecdat.cn/?p=35100原文出处:拓端数据部落公众号2022年,全球面临疫情和经济放缓的挑战,给消费市场带来了不确定性。消费者的消费理念和生活方式也发生了变化,更加注重产品的实用性和简单性。居民收入增长放缓,消费支出减少。然而,随着疫情逐渐得到控制,中国消费市场正......
  • 【专题】2023年中国工业互联网平台行业研究报告PDF合集分享(附原数据表)
    原文链接:https://tecdat.cn/?p=33647原文出处:拓端数据部落公众号这份报告合集是基于中国工业产业升级和智能制造的大背景而展开的。报告合集分析了工业互联网平台市场的发展阶段、平台玩家的产品和服务的底层逻辑以及变化趋势,并探讨了补贴减少、数据归属权之争、标准化与盈利模......
  • 《算法导论(原书第3版)》PDF
    内容简介在有关算法的书中,有一些叙述非常严谨,但不够全面;另一些涉及了大量的题材,但又缺乏严谨性。本书将严谨性和全面性融为一体,深入讨论各类算法,并着力使这些算法的设计和分析能为各个层次的读者接受。全书各章自成体系,可以作为独立的学习单元;算法以英语和伪代码的形式描述,具备初步......
  • Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--签名
    项目地址:Pdfium.Net:https://github.com/1000374/Pdfium.NetPdfiumViewer:https://github.com/1000374/PdfiumViewer获取PDF签名信息,不知如何解析签名内容,所以组件没办法做验签。只能获取获取签名个数、编码、原因、时间等基本信息,有知道的大佬可以共同探讨下签名信息包括:签名......