如何使用Syncfusion库在Word转PDF时保留墨迹注释?
解决Syncfusion DocIORenderer转换Word到PDF时墨迹注释丢失的问题
默认情况下,Syncfusion DocIORenderer可能未开启墨迹注释的渲染支持,你可以通过以下两种方式解决这个问题:
方法一:启用内置的注释渲染选项
首先确保你使用的是Syncfusion DocIORenderer的最新版本(旧版本可能不支持墨迹注释转换),然后在转换前配置渲染选项,开启注释包含开关:
using (WordDocument wordDocument = new WordDocument("你的源文档.docx")) { using (DocIORenderer renderer = new DocIORenderer()) { // 启用注释渲染,涵盖墨迹注释 renderer.Settings.RenderOptions.IncludeAnnotations = true; // 执行转换 PdfDocument pdfDocument = renderer.ConvertToPDF(wordDocument); pdfDocument.Save("转换后的文档.pdf"); } }
这个选项会让渲染器自动处理Word中的各类注释(包括墨迹),是最简便的解决方案。
方法二:手动提取并添加墨迹注释
如果上述方法无法生效(比如使用的版本不支持自动转换墨迹),可以手动遍历Word文档中的墨迹注释,将其转换为PDF的墨迹标注并添加到生成的PDF中:
using (WordDocument wordDocument = new WordDocument("你的源文档.docx")) { using (DocIORenderer renderer = new DocIORenderer()) { PdfDocument pdfDocument = renderer.ConvertToPDF(wordDocument); PdfPage targetPage = pdfDocument.Pages[0]; // 假设墨迹在第一页,可根据实际调整 // 遍历Word中的墨迹注释 foreach (WSection section in wordDocument.Sections) { foreach (WParagraph para in section.Body.Paragraphs) { foreach (WInline inline in para.Inlines) { if (inline is WInkAnnotation wordInk) { // 将Word墨迹的笔画数据转换为PDF支持的格式 List<PointF[]> pdfStrokes = new List<PointF[]>(); foreach (InkStroke stroke in wordInk.Strokes) { pdfStrokes.Add(stroke.Points.Select(p => new PointF(p.X, p.Y)).ToArray()); } // 创建PDF墨迹注释 PdfInkAnnotation pdfInk = new PdfInkAnnotation( new RectangleF(wordInk.Bounds.X, wordInk.Bounds.Y, wordInk.Bounds.Width, wordInk.Bounds.Height), pdfStrokes.ToArray() ); // 匹配原墨迹的样式 pdfInk.Color = wordInk.Color; pdfInk.Border.Width = wordInk.StrokeWidth; // 添加到PDF页面 targetPage.Annotations.Add(pdfInk); } } } } pdfDocument.Save("转换后的文档.pdf"); } }
注意事项
- 若使用手动转换方式,需要注意Word与PDF的坐标系统差异,可能需要根据页面缩放比例调整墨迹的位置坐标
- 部分旧版Syncfusion库可能没有
WInkAnnotation类型,建议升级到最新稳定版 - 如果墨迹注释位于Word的浮动层(如文本框内),需要额外处理其容器的定位逻辑
内容的提问来源于stack exchange,提问作者Rishabh Nigam
相关产品推荐
相关产品推荐

