本文介绍了合并pdf并保留SetTagged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp5.x.我正在尝试合并两个pdf并保留isTagged标志.当我删除copy.SetTagged();结果pdf包含两个pdf,非常好.添加copy.SetTagged()时会出现异常

I'm using iTextSharp 5.x. I'm trying to merge two pdfs and preserve the isTagged flag. When I remove copy.SetTagged(); the result pdf contains both pdfs which is great. When adding the copy.SetTagged() is get an exception

Exception -->System.ObjectDisposedException: Cannot access a closed file.
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.get_Position()

这是代码

List<string> filesToMerge = new List<string> { "C:/dev/dcs/wp-cla-dcs/Hex/Docs/metadata/coverPage.pdf", "C:/dev/dcs/wp-cla-dcs/Hex/Docs/metadata/49W7a.pdf" };
string outputFileName = "C:/dev/dcs/wp-cla-dcs/Hex/Docs/metadata/results.pdf";

using (FileStream outFS = new FileStream(outputFileName, FileMode.Create))
using (Document document = new Document())
//  using (PdfCopy copy = new PdfCopy(document, outFS))
using (PdfCopy copy = new PdfSmartCopy(document, outFS))
{
    {
        copy.SetTagged();
        // Set up the iTextSharp document
        document.Open();
        foreach (string pdfFile in filesToMerge)
        {
            using (var reader = new PdfReader(pdfFile))
            {
                copy.AddDocument(reader);
                copy.FreeReader(reader);
            }
        }

    }
}

推荐答案

尽管有@ bruno-lowagie的评论,但使用iText5可以达到更好的效果.

despite @bruno-lowagie's comment, I have had better results doing this with with iText5.

Uisng iText7,PdfMerger保留了几个未加标签的内容(所有内容均已在源文档中加标签).但是,iText5中的PdfCopy正常工作,只需要手动添加Xmp元数据,标题,lang等:

Uisng iText7, PdfMerger left several contents untagged (all were tagged in the source document). PdfCopy in iText5 however worked just fine, only needed to manually add Xmp metadata, title, lang, etc:

public static void CombineMultiplePDFs(string[] fileNames, string outFile)
{
    var lang = "en";
    var title = "My new title";

    // step 1: creation of a document-object
    Document document = new Document();

    // step 2: we create a writer that listens to the document
    FileStream newFileStream = new FileStream(outFile, FileMode.Create);
    PdfCopy writer = new PdfCopy(document, newFileStream);

    writer.SetTagged();

    writer.PdfVersion = PdfWriter.VERSION_1_7;
    writer.AddViewerPreference(PdfName.DISPLAYDOCTITLE, new PdfBoolean(true));
    writer.Info.Put(PdfName.TITLE, new PdfString(title));
    writer.CreateXmpMetadata();

    // step 3: we open the document
    document.Open();

    // set meta data
    document.AddLanguage(lang);
    document.AddTitle(title);

    // keep an array of all open readers so they can be closed again.
    var readers = new PdfReader[fileNames.Length];
    for (var fi = 0; fi < fileNames.Length; fi++)
    {
        // we create a reader for a certain document
        var fileName = fileNames[0];
        PdfReader reader = new PdfReader(fileName);
        readers[fi] = reader;
        reader.ConsolidateNamedDestinations();

        // step 4: we add content
        for (int i = 1; i <= reader.NumberOfPages; i++)
        {
            // IMPORTANT: the third param is is "KeepTaggedPdfStructure"
            PdfImportedPage page = writer.GetImportedPage(reader, i, true);
            writer.AddPage(page);
        }
    }

    // step 5: we close the document and writer
    writer.Close();
    document.Close();

    // close readers only after document is lcosed
    foreach (var r in readers)
    {
        r.Close();
    }
}

这篇关于合并pdf并保留SetTagged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 20:57