使用iTextSharp或iText等库,您可以通过PdfReader从PDF文档中提取元数据:
using (var reader = new PdfReader(pdfBytes))
{
return reader.Metadata == null ? null : Encoding.UTF8.GetString(reader.Metadata);
}这些类型库完全解析PDF文档,然后才能添加元数据。在我的例子中,这将导致系统资源的高使用率,因为我们每秒收到许多请求,并且PDF很大。
有没有一种方法可以从PDF中提取元数据,而不必先将其完全加载到内存中?
发布于 2021-11-09 09:18:42
iText 5.x也允许部分阅读PDF,只是看起来有点复杂。
而不是
using (var reader = new PdfReader(pdfBytes))使用
using (var reader = new PdfReader(new RandomAccessFileOrArray(pdfBytes), null, true))其中最终的true请求部分读取。
发布于 2021-11-09 08:31:19
使用PDF4NET,您可以提取XMP元数据,而无需将整个文档加载到内存中:
// This does a minimal parsing of the PDF file and loads
// only a few objects from the file
PDFFile pdfFile = new PDFFile(new MemoryStream(pdfBytes));
string xmpMetadata = pdfFile.ExtractXmpMetadata();更新1:代码更改为从字节数组加载文件
免责声明:我为开发PDF4NET库的公司工作。
https://stackoverflow.com/questions/69886240
复制相似问题