iText7生成的PdfPolyGeomAnnotation在浏览器PDF查看器中不显示的问题求助
问题描述
使用C#版iText7创建的Polyline Annotation(折线注释)在Adobe Acrobat中可正常渲染,但Chrome等浏览器PDF查看器无法显示,其他类型注释(如Ink、Highlight、Circle)无异常。
代码片段
List<float> vertices = new List<float>(); // Create Polyline Annotation PdfPolyGeomAnnotation polyline = PdfPolyGeomAnnotation.CreatePolyLine(rect, points.ToArray()); polyline.SetColor(Colour.GetColorValue()); // Set Line Width polyline.SetBorder(new PdfAnnotationBorder(0, 0, CommentData.strokewidth)); polyline.SetFlags(PdfAnnotation.PRINT | PdfAnnotation.LOCKED | PdfAnnotation.READ_ONLY); // Set Opacity if (CommentData.opacity > 0 && CommentData.opacity <= 1) { polyline.SetOpacity(new PdfNumber(CommentData.opacity)); } // Add Annotation to Page page.AddAnnotation(polyline);
示例输入参数
- Rect: { x: 23.12, y: 5.94, width: 38.25, height: 15.75 }
- Points (Vertices): [23.12, 5.94, 56.87, 21.68, 61.37, 13.43]
- Stroke Width: 2
- Opacity: 0.5
解决方案
浏览器PDF查看器对Polyline注释的兼容性要求更严格,需补充以下设置:
- 显式生成注释外观字典
部分浏览器依赖明确的外观定义,需手动生成并关联注释:
// 将注释设为间接引用 polyline.MakeIndirect(pdfDoc); // 生成默认外观(若不存在) PdfAppearance appearance = polyline.GetAppearance(); if (appearance == null) { appearance = PdfAppearance.CreateAppearance(pdfDoc, rect.Width, rect.Height); polyline.SetAppearance(PdfName.N, appearance); }
- 修正Border参数配置
原代码中PdfAnnotationBorder的第一个参数(虚线模式)设为0可能导致解析异常,改为明确指定实线模式:
polyline.SetBorder(new PdfAnnotationBorder(null, 0, CommentData.strokewidth));
- 添加注释意图标识
显式指定注释类型为折线,帮助浏览器识别:
polyline.Put(PdfName.IT, PdfName.PolyLine);
- 校验PDF坐标系统
确认顶点坐标基于PDF页面左下角原点(而非屏幕坐标系),若为屏幕坐标需转换:
float pageHeight = page.GetMediaBox().GetHeight(); List<float> convertedPoints = new List<float>(); for (int i = 0; i < points.Count; i += 2) { convertedPoints.Add(points[i]); // X坐标保留 convertedPoints.Add(pageHeight - points[i+1]); // Y坐标转换为PDF标准 } // 使用转换后的坐标创建注释 PdfPolyGeomAnnotation polyline = PdfPolyGeomAnnotation.CreatePolyLine(rect, convertedPoints.ToArray());
整合后的完整代码
List<float> vertices = new List<float>(); float pageHeight = page.GetMediaBox().GetHeight(); List<float> convertedPoints = new List<float>(); for (int i = 0; i < points.Count; i += 2) { convertedPoints.Add(points[i]); convertedPoints.Add(pageHeight - points[i+1]); } // Create Polyline Annotation PdfPolyGeomAnnotation polyline = PdfPolyGeomAnnotation.CreatePolyLine(rect, convertedPoints.ToArray()); polyline.SetColor(Colour.GetColorValue()); // 修正Border设置 polyline.SetBorder(new PdfAnnotationBorder(null, 0, CommentData.strokewidth)); // 添加Intent标识 polyline.Put(PdfName.IT, PdfName.PolyLine); polyline.SetFlags(PdfAnnotation.PRINT | PdfAnnotation.LOCKED | PdfAnnotation.READ_ONLY); // Set Opacity if (CommentData.opacity > 0 && CommentData.opacity <= 1) { polyline.SetOpacity(new PdfNumber(CommentData.opacity)); } // 生成外观并设为间接对象 polyline.MakeIndirect(pdfDoc); PdfAppearance appearance = polyline.GetAppearance(); if (appearance == null) { appearance = PdfAppearance.CreateAppearance(pdfDoc, rect.Width, rect.Height); polyline.SetAppearance(PdfName.N, appearance); } // Add Annotation to Page page.AddAnnotation(polyline);
内容的提问来源于stack exchange,提问作者MiaoWin
相关产品推荐
相关产品推荐

