项目地址:
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