PdfiumViewer组件扩展(Pdfium.Net.Free)--快速入门
项目地址:
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的功能,部分功能等待后续~~
打开PDF:
var pathPdf = "./Pdfium.NetTests/resources/about_blank.pdf"; using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf)))) Assert.IsTrue(!doc.IsNull);
创建PDF 添加页面:
using (var doc = PdfDocument.CreateNew()) { doc.Pages.Add(612, 792); Assert.AreEqual(612, doc.Pages[0].Width); }
添加字符集
//添加通过字符集 using (var doc = PdfDocument.CreateNew()) { var fontPath = @"c:\Windows\fonts\simhei.ttf"; byte[] fontData = File.ReadAllBytes(fontPath); var font = doc.TextLoadFont(fontData, fontData.Length, PdfFontTypes.TrueType, true); } //添加标准字符集 using (var doc = PdfDocument.CreateNew()) { var font = doc.TextLoadStandardFont(StandardPDFFonts.Arial); }
添加文字:
//添加文字 指定字库 字库内嵌 using (var doc = PdfDocument.CreateNew()) { var fontPath = @"c:\Windows\fonts\simhei.ttf"; doc.LoadFont(fontPath); var cSharpString = "!you ok 这是第一句。 这是第二行。a you ok 你好 abcdefghijklmnopqrstuvwxyz1234567890-*/+~`@#$%^&*(){}[];:'|?,,。.!!"; var page = doc.Pages.Add(612, 792); page.AddString(cSharpString, 10, 50, 12, Color.Black); doc.Save("./Pdfium.NetTests/AddString2.pdf"); } //使用标准字库 英语 中文乱码 using (var doc = PdfDocument.CreateNew()) { var cSharpString = "!you ok 这是第一句。 这是第二行。a you ok"; var page = doc.Pages.Add(612, 792); page.AddString(cSharpString, 10, 50, 12, Color.Black, StandardPDFFonts.TimesNewRoman); doc.Save("./Pdfium.NetTests/AddString1.pdf"); } //字符子集 根据写入问题拆分字符子集并嵌入pdf内 using (var doc = PdfDocument.CreateNew()) { var fontPath = @"c:\Windows\fonts\simhei.ttf"; var cSharpString = "!you ok 这是第一句。"; var cSharpString2 = " 这是第二行。a you ok 你好 abcdefghijklmnopqrstuvwxyz1234567890-*/+~`@#$%^&*(){}[];:'|?,,。.!!"; doc.LoadFont(fontPath, true, cSharpString + cSharpString2); var page = doc.Pages.Add(612, 792); page.AddString(cSharpString, 10, 50, 12, Color.Black); page.AddString(cSharpString2, 10, 80, 12, Color.Black); doc.Save("./Pdfium.NetTests/AddString3.pdf"); }
获取pdf内部已有字符添加文字:
var pathPdf = "./Pdfium.NetTests/resources/fontText.pdf"; using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf)))) { var font = doc.Pages[0].GetTextObjFont("你", out float size); if (!font.IsNull) { var emb = font.IsEmbedded; var cSharpString = "你好啊,abc"; var page = doc.Pages[0]; page.AddString(cSharpString, 10, 50, size, Color.Black, font); doc.Save("./Pdfium.NetTests/TextObjFont.pdf"); } }
添加水印:
//样式1 实线 using (var doc = PdfDocument.CreateNew()) { doc.Pages.Add(612, 792); var fontPath = @"c:\Windows\fonts\simhei.ttf"; var cSharpString = "我是水印"; doc.LoadFont(fontPath, true, cSharpString); doc.WaterMark(cSharpString, 20, Color.FromArgb(50, 255, 0, 0), totleHeight: 120); doc.Save("./Pdfium.NetTests/WaterMark.pdf"); } //样式2 中空 using (var doc = PdfDocument.CreateNew()) { doc.Pages.Add(612, 792); var fontPath = @"c:\Windows\fonts\simhei.ttf"; doc.LoadFont(fontPath); var cSharpString = "我是水印"; doc.WaterMark(cSharpString, 20, Color.FromArgb(50, 255, 0, 0), totleHeight: 120, render_mode: PdfTextRenderMode.Stroke); doc.Save("./Pdfium.NetTests/WaterMark1.pdf"); }
添加图片:
//添加流 using (var doc = PdfDocument.CreateNew()) { var imagePath = "./Pdfium.NetTests/resources/mona_lisa.jpg"; var page = doc.Pages.Add(612, 792); var stream = new MemoryStream(File.ReadAllBytes(imagePath)); page.AddImage(stream, 50, 100); doc.Save("./Pdfium.NetTests/AddImage.pdf"); } //添加image对象 using (var doc = PdfDocument.CreateNew()) { var imagePath = "./Pdfium.NetTests/resources/mona_lisa.jpg"; var image = Image.FromFile(imagePath, true); var page = doc.Pages.Add(612, 792); page.AddImage(image, 50, 100); var page1 = doc.Pages.Add(612, 792); page.AddImage(image, 50, 500); doc.Save("./Pdfium.NetTests/AddImage1.pdf"); }
pdf每一页转成图片:
//返回image 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, PdfRotation.Rotate0, PdfRenderFlags.Annotations | PdfRenderFlags.CorrectFromDpi)) { Assert.IsNotNull(image); } } //通过DOC获取 image 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.Render(0, 96, 96, true)) { Assert.IsNotNull(image); } } //通过DOC获取 stream 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 stream = doc.RenderStream(0, 96, 96, true)) { var image=Image.FromStream(stream); } } //自己实现GDI+ 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)), PdfRotation.Rotate0, PdfRenderFlags.None); image.Save("./Pdfium.NetTests/Render.png"); } private static int AdjustDpi(double value, double dpi) { return (int)(value / 100.0 * dpi); }
Ubuntu 请使用如下方式:(可引用SkiaSharp或者直接保存流到图片)
using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf)))) for (int i = 0; i < doc.PageCount; i++) { var localPath = Path.Combine(path, $"{i}.png"); using (var stream = doc.RenderStream(i, (int)doc.Pages[i].Width * 4 / 3, (int)doc.Pages[i].Height * 4 / 3, 96 * 2, 96 * 2, PdfRenderFlags.Annotations | PdfRenderFlags.CorrectFromDpi)) { //using (var filestream = new FileStream(localPath,FileMode.OpenOrCreate)) //{ // stream.CopyTo(filestream); //} var bitmip = SKBitmap.Decode(stream); var image = SKImage.FromBitmap(bitmip); // 将图片保存为PNG到本地路径 using (var imageFileStream = new FileStream(localPath, FileMode.Create, FileAccess.Write)) { image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(imageFileStream); } } }
pdf页局部转成图片:
//返回stream 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].RenderStream((int)doc.Pages[i].Width * 4 / 3, (int)doc.Pages[i].Height * 4 / 3, 96, 96, PdfRotation.Rotate0, PdfRenderFlags.Annotations | PdfRenderFlags.CorrectFromDpi)) { Assert.IsNotNull(image); } } //返回stream 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].RenderStream( (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 PdfRotation.Rotate0, // no rotation PdfRenderFlags.None // no render flags )) { } }
导出pdf内嵌缩略图:
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(); Assert.IsNotNull(image); }
获取PDF内文字:
var pathPdf = "./Pdfium.NetTests/resources/text_render_mode.pdf"; using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf)))) { var text = doc.Pages[0].GetPdfText(); Assert.AreEqual("fill\r\nstroke\0", text); }
获取PDF指定区域的文字:
//page: 0,x: 235,y: 238,x1: 327,y1: 287 Text: fill var pathPdf = "./Pdfium.NetTests/resources/text_render_mode.pdf"; using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf)))) { var text = doc.Pages[0].GetBoundedText(235, 238, 327, 287); Assert.AreEqual("fill", text); }
拆分pdf:
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.GetPage(i); Assert.AreEqual(1, doci.PageCount); } } 拆分多页: var pathPdf = "./Pdfium.NetTests/resources/viewer_ref.pdf"; using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf)))) { var pages = new int[] { 1, 2, 3 }; var doci = doc.GetPage(pages); Assert.AreEqual(3, doci.PageCount); }
合并pdf:
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.MergePage(doc1); Assert.AreEqual(10, doc.PageCount); }
插入pdf:
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); var w = doc1.Pages[0].Width; var w1 = doc.Pages[1].Width; Assert.AreEqual(w, w1); }
替换pdf:
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); }
多页pdf合并成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)) Assert.AreEqual(612 * 2, doc1.Pages[0].Width); }
删除页:
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.RotatePage(0, PdfRotation.Rotate90); }
获取pdf内签名信息:
var pathPdf = "./Pdfium.NetTests/resources/signature_reason.pdf"; using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf)))) { var len = doc.GetSignatureCount(); for (int i = 0; i < len; i++) { var sign = doc.GetSignatureObject(i); var encoding = sign.GetSubFilter(); var strSign = sign.GetContents(); var strSign1 = sign.GetByteRange(); var strReason = sign.GetReason(); var strTime = sign.GetTime(); var press = sign.GetDocMDPPermission(); } }
获取当前页字符信息:
var pathPdf = "./Pdfium.NetTests/resources/fontText.pdf"; using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf)))) { var page0 = doc.Pages[0]; var infos = page0.GetCharacterInformation(); }
打印:
var printer = new PdfPrinter("Microsoft Print To PDF"); //printer.Print(fileName); printer.Print(fileName, documentName: "with name");