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

问题描述

我有一个包含大量xmp元数据的PDF文件.当我使用itext7对其进行处理时,程序陷入了var pdfdocument origpdf = new pdfdocument (pdfreader);语句中,并查看源代码,我发现open(空)方法是在Pdfdocument对象的构造函数中执行的,最终卡在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元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 12:02