故障排除
使用 Aspose.PDF FOSS for .NET 时的常见问题及解决方案
文档加载
| 问题 | 原因 | 解决方案 |
|---|
Document.Open 在有效的 PDF 上抛出异常 | 文件受密码保护 | 在构造函数中提供密码 |
| 大型 PDF 导致内存不足 | 整个文件加载到字节数组中 | 逐页处理或使用流式处理 |
Pages[0] 抛出索引错误 | 页面使用基于 1 的索引 | 对第一页使用 Pages[1] |
注释
| Problem | Cause | Solution |
|---|
| 保存后注释不可见 | 矩形面积为零或超出页面边界 | 验证坐标位于页面范围内 MediaBox |
LinkAnnotation.Uri 返回 null | 链接使用 GoTo 或 JavaScript 动作 | 通过 PdfAction.Create 解决并检查 ActionType |
Flatten() 抛出异常 | 注释缺少 /P(页面)引用 | 确保注释是通过 Page.Annotations 添加的 |
文本提取
| 问题 | 原因 | 解决方案 |
|---|
| 提取的文本乱码 | 非标准编码或 CID 字体映射 | 检查字体嵌入;扫描的 PDF 可能需要 OCR |
| 未找到文本片段 | 页面内容为光栅图像 | 在文本提取前使用 OCR |
| 正则表达式未返回匹配 | PDF 文本布局插入空格 | 使用更宽松的模式或规范化空格 |
渲染和转换
| Problem | Cause | Solution |
|---|
| Rendered image is blurry | Resolution too low | Increase DPI in Resolution constructor |
| HTML output missing images | External image paths not configured | Use HtmlSaveOptions with embedded images |
| PDF/A conversion removes annotations | Target profile disallows annotations | Use PDF/A-2 or PDF/A-3 |
表单字段
| Problem | Cause | Solution |
|---|
| Field value is empty | Field has no /V entry | Check field.Value is not null |
FillField has no effect | Field name mismatch | Use Form.FieldNames to list available names |
| ExportToJson produces empty output | Widget annotation not correctly cast | Verify the annotation is a WidgetAnnotation |
通用技巧
- 始终在
using 中包装 Document 以释放文件句柄。 - 使用
Document.Open(byte[]) 进行内存工作流,以避免文件锁定。 - 在按索引访问页面之前检查
doc.Pages.Count。 - 在所有修改完成后一次性保存文档,以获得最佳性能。
另请参阅