故障排除

故障排除

使用 Aspose.PDF FOSS for .NET 时的常见问题及解决方案


文档加载

问题原因解决方案
Document.Open 在有效的 PDF 上抛出异常文件受密码保护在构造函数中提供密码
大型 PDF 导致内存不足整个文件加载到字节数组中逐页处理或使用流式处理
Pages[0] 抛出索引错误页面使用基于 1 的索引对第一页使用 Pages[1]

注释

ProblemCauseSolution
保存后注释不可见矩形面积为零或超出页面边界验证坐标位于页面范围内 MediaBox
LinkAnnotation.Uri 返回 null链接使用 GoTo 或 JavaScript 动作通过 PdfAction.Create 解决并检查 ActionType
Flatten() 抛出异常注释缺少 /P(页面)引用确保注释是通过 Page.Annotations 添加的

文本提取

问题原因解决方案
提取的文本乱码非标准编码或 CID 字体映射检查字体嵌入;扫描的 PDF 可能需要 OCR
未找到文本片段页面内容为光栅图像在文本提取前使用 OCR
正则表达式未返回匹配PDF 文本布局插入空格使用更宽松的模式或规范化空格

渲染和转换

ProblemCauseSolution
Rendered image is blurryResolution too lowIncrease DPI in Resolution constructor
HTML output missing imagesExternal image paths not configuredUse HtmlSaveOptions with embedded images
PDF/A conversion removes annotationsTarget profile disallows annotationsUse PDF/A-2 or PDF/A-3

表单字段

ProblemCauseSolution
Field value is emptyField has no /V entryCheck field.Value is not null
FillField has no effectField name mismatchUse Form.FieldNames to list available names
ExportToJson produces empty outputWidget annotation not correctly castVerify the annotation is a WidgetAnnotation

通用技巧

  • 始终在 using 中包装 Document 以释放文件句柄。
  • 使用 Document.Open(byte[]) 进行内存工作流,以避免文件锁定。
  • 在按索引访问页面之前检查 doc.Pages.Count
  • 在所有修改完成后一次性保存文档,以获得最佳性能。

另请参阅

 中文