本文介绍了有没有办法避免在 iText 7 中加载 XMP 元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量 xmp 元数据的 PDF 文件.我用itext7处理的时候,程序卡在语句var pdfdocument origpdf = new pdfdocument (pdfreader); 看着源码,发现在Pdfdocument对象的构造函数中执行了open(null)方法,最后卡在了reader.pdfaconformancelevel = pdfaconformancelevel.getconformancelevel(xmpmatafactory.Parsefrombuffer (xmpmetadata)); 并且没有办法避免它,xmp 元数据对我来说毫无用处.

I have a PDF file with a huge amount of xmp metadata. When I use itext7 to process it, the program is stuck in the statement var pdfdocument origpdf = new pdfdocument (pdfreader); Looking at the source code, I found that the open (null) method was executed in the constructor of Pdfdocument object, and finally stuck at reader.pdfaconformancelevel = pdfaconformancelevel.getconformancelevel (xmpmatafactory. Parsefrombuffer (xmpmetadata)); and there is no option to avoid it, the xmp metadata is useless to me.

使用 itextSharp,基于 删除 PDF/A 上的 XMP 元数据,我可以获得没有元数据的pdf文件.

With itextSharp, base on Remove XMP Metadata on PDF/A, I can get a pdf file without metadata.

PdfReader reader = new PdfReader(src);
PdfDictionary dict = reader.Catalog;
dict.Remove(PdfName.METADATA);
dict.Remove(PdfName.PROPERTIES);
reader.RemoveUnusedObjects();
PdfStamper stamper = new PdfStamper(reader, new FileStream(target, FileMode.Create, FileAccess.ReadWrite));
stamper.Close();

我尝试创建 PdfReader 或 PdfDocument 的子类,以尝试干扰加载元数据,但都失败了.

I tried to create a subclass of PdfReader or PdfDocument, to try to interfere with loading metadata, but all failed.

使用itext7,有什么办法可以避免加载元数据?或者有什么方法可以删除 XMP 元数据?

Use itext7, Is there any way to avoid loading metadata? Or is there any way to Remove XMP Metadata?

推荐答案

这是您可以覆盖 PdfReader 并跳过读取元数据对象的方法.事实上,对象被读取,但我们读者会表现得好像没有元数据一样,它不会告诉任何人它存在.

This is how you can override PdfReader and skip reading metadata objects. In fact, the objects are read, but we the reader will behave as if there is no metadata and it will not tell anyone that it is present.

自定义PdfReader实现:

private static class MetadataFreePdfReader extends PdfReader {
    public MetadataFreePdfReader(String filename) throws IOException {
        super(filename);
    }

    @Override
    protected PdfObject readObject(PdfIndirectReference reference) {
        PdfObject obj = super.readObject(reference);
        if (obj instanceof PdfStream && PdfName.Metadata.equals(((PdfStream) obj).getAsName(PdfName.Type))) {
            // skip metadata object
            return null;
        }
        return obj;
    }
}

这是将重载实例传递给 PdfDocument 的方式:

This is how you pass the overloaded instance to PdfDocument:

PdfDocument pdfDocument = new PdfDocument(new MetadataFreePdfReader("C:/path/to/140mmX90mm-2000BGJCV1M.pdf"));

这篇关于有没有办法避免在 iText 7 中加载 XMP 元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 23:37