项目地址:
Pdfium.Net:https://github.com/1000374/Pdfium.Net
PdfiumViewer:https://github.com/1000374/PdfiumViewer
Pdfium.Net 添加文本有3个重载
1.当前重载使用pdf标准字库添加文字(中文会乱码)
/// <summary> /// Add text object using one of the standard PDF fonts. Chinese maybe garbled characters /// </summary> /// <param name="text"></param> /// <param name="posX"></param> /// <param name="posY"></param> /// <param name="font_size">the font size for the new text object.</param> /// <param name="fillColor"></param> /// <param name="fontName"><see cref="Native.Enums.StandardPDFFonts"/></param> /// <param name="scalex"></param> /// <param name="scaley"></param> /// <param name="rotate"></param> /// <param name="shear"></param> /// <param name="blend">Blend mode can be one of following: Color, ColorBurn, ColorDodge, Darken,Difference, Exclusion, HardLight, Hue, Lighten, Luminosity, Multiply, Normal,Overlay, Saturation, Screen, SoftLight.<see cref="BlendMode"/></param> /// <param name="render_mode">render_mode<see cref="FpdfTextRenderMode"/></param> /// <param name="strokeColor">stroke Color render_mode </param> /// <param name="strokeWidth">stroke Width</param> /// <returns></returns> public bool AddString(string text, int posX, int posY, float font_size, Color fillColor, string fontName, float scalex = 1f, float scaley = 1, float rotate = 0, float shear = 0, string blend = BlendMode.Darken, FpdfTextRenderMode render_mode = FpdfTextRenderMode.FILL, Color? strokeColor = null, float strokeWidth = 0.2f)
调用示例:
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"); }
2.当前重载使用获取的PdfFont 对象添加文字,详情请参照
Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--加载字体
/// <summary> /// add Text with add <see cref="FpdfFontT"/> /// </summary> /// <param name="pageNumber"></param> /// <param name="text"></param> /// <param name="posX"></param> /// <param name="posY"></param> /// <param name="font_size"></param> /// <param name="fillColor"></param> /// <param name="font"></param> /// <param name="scalex"></param> /// <param name="scaley"></param> /// <param name="rotate"></param> /// <param name="shear"></param> /// <param name="blend">Blend mode can be one of following: Color, ColorBurn, ColorDodge, Darken,Difference, Exclusion, HardLight, Hue, Lighten, Luminosity, Multiply, Normal,Overlay, Saturation, Screen, SoftLight.<see cref="BlendMode"/></param> /// <param name="render_mode">render_mode<see cref="FpdfTextRenderMode"/></param> /// <param name="strokeColor">stroke Color render_mode </param> /// <param name="strokeWidth">stroke Width</param> /// <returns></returns> public bool AddString(string text, float posX, float posY, float font_size, Color fillColor, PdfFont font, float scalex = 1f, float scaley = 1, float rotate = 0, float shear = 0, string blend = BlendMode.Darken, FpdfTextRenderMode render_mode = FpdfTextRenderMode.FILL, Color? strokeColor = null, float strokeWidth = 0.2f)
调用示例(从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"); } }
3.当前重载需提前调用
public void LoadFont(string fontPath, bool isSubSet = false, string words = null, FpdfFontTypes fontTypes = FpdfFontTypes.TrueType)
方法,且只能调用一次,否则抛出找不到字体对象,或者pdf会多次加载字体导致pdf很大,也就是每次调用都会往doc中加载一次字库。
可创建子集后加载字库,以减少pdf大小,具体参照
Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--创建字符子集
/// <summary> /// add Text with add <see cref="FpdfFontT"/> /// </summary> /// <param name="text"></param> /// <param name="posX"></param> /// <param name="posY"></param> /// <param name="font_size">the font size for the new text object.</param> /// <param name="fillColor">the font color.</param> /// <param name="isSubSet">ttf path use File.ReadAllBytes(fontPath);</param> /// <param name="scalex">scale x</param> /// <param name="scaley">scale y</param> /// <param name="rotate">rotate</param> /// <param name="shear">shear</param> /// <param name="fontTypes"><see cref="FpdfFontTypes"/></param> /// <param name="blend">Blend mode can be one of following: Color, ColorBurn, ColorDodge, Darken,Difference, Exclusion, HardLight, Hue, Lighten, Luminosity, Multiply, Normal,Overlay, Saturation, Screen, SoftLight.<see cref="BlendMode"/></param> /// <param name="render_mode">render_mode<see cref="FpdfTextRenderMode"/></param> /// <param name="strokeColor">stroke Color render_mode </param> /// <param name="strokeWidth">stroke Width</param> /// <returns></returns> public bool AddString(string text, float posX, float posY, float font_size, Color fillColor, float scalex = 1f, float scaley = 1, float rotate = 0, float shear = 0, FpdfFontTypes fontTypes = FpdfFontTypes.TrueType, string blend = BlendMode.Darken, FpdfTextRenderMode render_mode = FpdfTextRenderMode.FILL, Color? strokeColor = null, float strokeWidth = 0.2f)
调用字符集全部加载示例:
using (var doc = PdfDocument.CreateNew()) { var fontPath = @"c:\Windows\fonts\simhei.ttf"; doc.LoadFont(fontPath); var cSharpString = "!you ok 这是第一句。 这是第二行。a you ok"; var page = doc.Pages.Add(612, 792); page.AddString(cSharpString, 10, 50, 12, Color.Black); doc.Save("./Pdfium.NetTests/AddString.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"); }
标签:Pdfium,--,doc,float,Free,Color,mode,var From: https://www.cnblogs.com/xiaohemiao/p/17995042