首页 文章

现有 PDF 到 PDF/A“转换”

提问于
浏览
0

我想把现有的 pdf 变成 pdf/a-1b。据我所知,itext 无法将 pdf 转换为 pdf/a,因为它符合 pdf/a 标准。但它绝对可以将文档标记为 pdf/a。但是,我查看了各种示例,我似乎无法弄清楚如何做到这一点。主要问题是

writer.PDFXConformance = PdfWriter.PDFA1B;

不再起作用了。第一个 PDFA1B 无法识别,其次,pdfwriter 似乎已被重写,并且没有太多关于此的信息。似乎唯一的(在 itext java 版本中)方式是:

PdfAWriter writer = PdfAWriter.getInstance(document, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_1B);

但这需要一种文件类型,即。从头开始创建 pdf 时可以使用它。

有人可以用当前版本的 itextsharp 给出 pdf 到 pdf/a 转换的例子吗?谢谢。

1 回答

  • 1

    我无法想象这样做的正当理由,但显然你有一个。

    iText 中的一致性设置旨在与PdfWriter一起使用,并且该对象(通常)仅用于与新文档一起使用。因为 iText 从未打算将文档转换为符合性,而这正是它的构建方式。

    要执行您想要执行的操作,您可以打开原始文档并更新文档字典中的相应标记,也可以创建包含相应条目的新文档,然后导入旧文档。下面的代码显示了后一种方法,它首先创建一个常规的 non-conforming PDF,然后创建第二个文档,表明它符合要求,即使它可能会也可能不会。有关详细信息,请参阅代码注释。这针对 iTextSharp 5.4.2.0.

    //Folder that we're working from
    var workingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    
    //Create a regular non-conformant PDF, nothing special below
    var RegularPdf = Path.Combine(workingFolder, "File1.pdf");
    using (var fs = new FileStream(RegularPdf, FileMode.Create, FileAccess.Write, FileShare.None)) {
        using (var doc = new Document()) {
            using (var writer = PdfWriter.GetInstance(doc, fs)) {
                doc.Open();
    
                doc.Add(new Paragraph("Hello world!"));
    
                doc.Close();
            }
        }
    }
    
    //Create our conformant document from the above file
    var ConformantPdf = Path.Combine(workingFolder, "File2.pdf");
    using (var fs = new FileStream(ConformantPdf, FileMode.Create, FileAccess.Write, FileShare.None)) {
        using (var doc = new Document()) {
    
            //Use PdfSmartCopy to get every page
            using (var copy = new PdfSmartCopy(doc, fs)) {
    
                //Set our conformance levels
                copy.SetPdfVersion(PdfWriter.PDF_VERSION_1_3);
                copy.PDFXConformance = PdfWriter.PDFX1A2001;
    
                //Open our new document for writing
                doc.Open();
    
                //Bring in every page from the old PDF
                using (var r = new PdfReader(RegularPdf)) {
                    for (var i = 1; i <= r.NumberOfPages; i++) {
                        copy.AddPage(copy.GetImportedPage(r, i));
                    }
                }
    
                //Close up
                doc.Close();
            }
        }
    }
    

    只是要 100%清楚,这将不会成为一个合适的 PDF,只是一个说它符合的文件。

相关问题