取代PDF文档中的字符串(iTextSharp的或PdfSharp)

问题描述 投票:0回答:2

我们使用非管理具有功能可按替换PDF文件(http://www.debenu.com/docs/pdf_library_reference/ReplaceTag.php)文本DLL。我们正试图迁移到托管解决方案(iTextSharp的或PdfSharp)。我知道,这个问题已经被问过,而且答案是“你不应该做”或“这是不容易被支持PDF”。然而存在,对我们有效的解决方案,我们只是需要将其转换为C#。任何想法我应该怎么处理它?

pdf itextsharp pdfsharp
2个回答
2
投票

根据你的library reference link,您使用Debenu PDFLibrary功能ReplaceTag。据this Debenu knowledge base article

该ReplaceTag功能只是替换页面的内容流中的文本,因此,对于大多数的文件,将不会有任何效果。对于一些简单的文件可能是无法取代的内容,但它真的取决于PDF是如何构造的。本质上,它是一样的做:

DPL.CombineContentStreams();
string content = DPL.GetContentStreamToString();
DPL.SetPageContentFromString(content.Replace("Moby", "Mary"));

这应该是可能的任何通用的PDF库,它肯定是的iText(夏普):

void VerySimpleReplaceText(string OrigFile, string ResultFile, string origText, string replaceText)
{
    using (PdfReader reader = new PdfReader(OrigFile))
    {
        byte[] contentBytes = reader.GetPageContent(1);
        string contentString = PdfEncodings.ConvertToString(contentBytes, PdfObject.TEXT_PDFDOCENCODING);
        contentString = contentString.Replace(origText, replaceText);
        reader.SetPageContent(1, PdfEncodings.ConvertToBytes(contentString, PdfObject.TEXT_PDFDOCENCODING));

        new PdfStamper(reader, new FileStream(ResultFile, FileMode.Create, FileAccess.Write)).Close();
    }
}

警告:就像在Debenu功能的情况下,大多数文档此代码将不会有任何效果,甚至是破坏性的。对于一些简单的文件可能是无法取代的内容,但它真的取决于PDF是如何构造的。

顺便说一句,在Debenu knowledge base article继续:

如果您创建使用Debenu快速PDF库和标准字体,则ReplaceTag功能应该工作一个PDF - 但是,对于那些子集化的字体,甚至字距(其中的话会被分拆)工具创建的PDF文件,然后搜索文本可能赢得”吨是在一个简单的格式的内容。

因此,在短期中,ReplaceTag功能仅在一些有限的情况下工作,是不是,你可以依靠搜索和替换文本的功能。

因此,如果你迁移到托管解决方案时,你也改变正在创建的源文档的方式,很可能既不是Debenu PDFLibrary功能ReplaceTag也不上面的代码将能够根据需要修改的内容。


0
投票

为pdfsharp用户继承人有些可用功能,从我的项目复制,它使用其通过othere方法因此未使用的结果所消耗的实用程序方法。

它可能弄乱的结果(在相同的空间中的所有字符)取决于源材料忽略由字距创建空格,而且

    public static void ReplaceTextInPdfPage(PdfPage contentPage, string source, string target)
    {
        ModifyPdfContentStreams(contentPage, stream =>
        {
            if (!stream.TryUnfilter())
                return false;
            var search = string.Join("\\s*", source.Select(c => c.ToString()));
            var stringStream = Encoding.Default.GetString(stream.Value, 0, stream.Length);
            if (!Regex.IsMatch(stringStream, search))
                return false;
            stringStream = Regex.Replace(stringStream, search, target);
            stream.Value = Encoding.Default.GetBytes(stringStream);
            stream.Zip();
            return false;
        });
    }


    public static void ModifyPdfContentStreams(PdfPage contentPage,Func<PdfDictionary.PdfStream, bool> Modification)
    {

        for (var i = 0; i < contentPage.Contents.Elements.Count; i++)
            if (Modification(contentPage.Contents.Elements.GetDictionary(i).Stream))
                return;
        var resources = contentPage.Elements?.GetDictionary("/Resources");
        var xObjects = resources?.Elements.GetDictionary("/XObject");
        if (xObjects == null)
            return;
        foreach (var item in xObjects.Elements.Values.OfType<PdfReference>())
        {
            var stream = (item.Value as PdfDictionary)?.Stream;
            if (stream != null)
                if (Modification(stream))
                    return;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.