PdfiumViewer组件扩展(Pdfium.Net.Free)--PDF操作

项目地址:

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

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

Pdfium.Net.Free 支持

  • .NETFramework 4.0

  • .NETFramework 4.5

  • .NETStandard 2.0

  • .Net8.0

可以和PdfiumViewer.Free共同使用预览、编辑pdf,也可以直接引用Pdfium.Net.Free 操作pdf,Pdfium.Net.Free封装了现有Pdfium的函数,实现了部分操作pdf的功能,部分功能等待后续~~

Pdfium.Net支持对PDF操作:

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

1.转成图片

1.基于GDI+下:

1
2
3
4
5
6
7
8
9
10
11
/// <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)
      

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
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+

1
2
3
4
5
6
7
8
9
10
11
12
/// <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)
     

 

示例代码:

1
2
3
4
5
6
7
8
9
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预览器框选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/// <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)

  

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.拆分

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

1
2
3
4
5
6
7
8
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对象保存

1
2
3
4
5
6
7
8
9
10
11
12
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.合并

1
2
3
4
5
6
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.插入

1
2
3
4
5
6
7
8
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.替换  

1
2
3
4
5
6
7
8
9
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.删除

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

  

也可通过以下删除

1
2
3
4
5
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <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
}

示例:

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

  

8.导出缩略图

1
2
3
4
5
6
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

1
2
3
4
5
6
7
8
9
10
/// <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)

  

1
2
3
4
5
6
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))
{}
 }

  

posted @   小树禾小央  阅读(559)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示