带有Windows 10 Universal App的iTextSharp [英] iTextSharp with a Windows 10 Universal App

查看:162
本文介绍了带有Windows 10 Universal App的iTextSharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让iTextSharp与Windows 10 Universal应用程序一起使用?当我尝试通过NuGet包管理器添加iTextSharp版本5.5.8时,我收到以下错误:

Is there a way to get iTextSharp to work with a Windows 10 Universal app? When I try to add iTextSharp version 5.5.8 through the NuGet package manager I get the following errors:

iTextSharp 5.5.8 is not compatible with UAP, Version=v10.0(win10-XXX)
Some packages are not compatible ith UAP,Version=v10.0(win10-XXX)

其中XXX是x64或x86等平台。
我们在Windows 8商店应用程序中使用iTextSharp来获取PDF模板,然后使用用户提供的数据填充字段。用户还提供模板,以便按照他们想要的方式格式化文档。为此,我们使用iTextSharp库中的PDFStamper类来执行此操作,如以下代码所示:

Where XXX is the platform like x64 or x86. We use iTextSharp in a Windows 8 Store app to take a PDF template and then populate the fields with data provided by the user. The user also provides the template so the document is formatted the way they want it. For this we re using the PDFStamper class from the iTextSharp library to do this as shown in the following code:

    public async Task<byte[]> fillPDF(string templatePath, FormData mergeDataItems)
        {


            StorageFile file = await StorageFile.GetFileFromPathAsync(templatePath);
            var buf = await FileIO.ReadBufferAsync(file);


            var reader = new PdfReader(buf.ToArray());
            var outStream = new MemoryStream();
            var stamper = new PdfStamper(reader, outStream);

            var form = stamper.AcroFields;
            form.GenerateAppearances = true; //Added this line, fixed my problem
            var fieldKeys = form.Fields.Keys;

            foreach (KeyValuePair<String, String> pair in mergeDataItems.MergeFieldValues)
            {
                if (fieldKeys.Any(f => f == pair.Key))
                {
                    form.SetField(pair.Key, pair.Value);
                }
            }
            stamper.Close();
            reader.Close();

            return flattenPdf(outStream.ToArray());
        }

此处

    private static byte[] flattenPdf(byte[] pdf)
        {
            var reader = new PdfReader(pdf);

            var outStream = new MemoryStream();
            var stamper = new PdfStamper(reader, outStream);

            stamper.FormFlattening = true;

            stamper.Close();
            reader.Close();

            return outStream.ToArray();
        }

任何帮助让iTextSharp与Windows 10应用程序一起使用或任何建议如何在没有iTextSharp的情况下从模板生成PDF文档将不胜感激。
谢谢,

Any help with getting iTextSharp to work with a windows 10 app or any suggestions on how to generate a PDF document from a template without iTextSharp would be greatly appreciated. Thanks,

推荐答案

UWP框架显然是.NET生态系统的重大更新,特别是在安全方面和密码学。例如,在UWP中,散列算法类位于命名空间 Windows.Security.Cryptography.Core 中,而直到.NET 4.x它们在<$ c $中C> System.Security.Cryptography 。一些类也被重命名。

The UWP framework is an apparently breaking update for the .NET ecosystem, especially with regards to security and cryptography. For example, in UWP the hashing algorithm classes are located in the namespace Windows.Security.Cryptography.Core, while up till .NET 4.x they are in System.Security.Cryptography. Some classes have also been renamed.

这是iTextSharp的一个重大变化,也是因为它的安全依赖性BouncyCastle,因为 System.Security .Cryptography 程序集用于数字签名。 NuGet或UWP本身显然知道所使用的.NET程序集,并拒绝添加无法在UWP中编译的依赖项 - 无论您是否使用与数字签名相关的任何iTextSharp功能。

This is a breaking change for iTextSharp, and also for its security dependency, BouncyCastle, because the System.Security.Cryptography assembly is used for digital signatures. NuGet, or UWP itself, is apparently aware of the .NET assemblies that are used and refuses to add dependencies that won't compile in UWP - regardless of whether you use any iTextSharp functionality related to digital signatures.

仅供参考我是iText的员工,几周前我们就发现了这个问题。调查仍在进行中,因此我可能无法在此解释中做到一切正确。我们还在制定一项支持UWP和.NET 4.x的策略 - 据我所知,这在加密方面是互斥的。如果你的谷歌术语与UAP不兼容,那么你可以阅读很多知名库的类似问题,所以这不仅是一个问题, iTextSharp。

FYI I am an iText employee, and we discovered this problem only a few weeks ago. The investigation is still ongoing, so I might not get everything right in this explanation. We are also working on a strategy to support both UWP and .NET 4.x - which are, as I understand it, mutually exclusive when it comes to cryptography. If you Google the term "is not compatible with UAP", then you can read about similar issues for a lot of well-known libraries, so this is not only a problem for iTextSharp.

如果您真的需要快速解决方案,那么您可以下载iTextSharp的源代码并将其复制粘贴到Visual Studio中的新UWP类库项目中。编译错误的数量看起来令人生畏,但其中大约80%对我来说似乎微不足道。例如其中一半是对Exception类的继承构造函数中的Serializable和SerializationInfo的引用,它们不再存在,因此可以安全地删除它们(!)。

If you really need a solution fast, then you can download the source code for iTextSharp and copy-paste it into a new UWP Class Library project in Visual Studio. The number of compile errors looks daunting, but about 80% of them seem trivial to me. e.g. Half of them are references to Serializable and SerializationInfo in an inherited constructor for Exception classes which doesn't exist anymore, so they can probably (!) be deleted safely.

替代方案是等待iTextSharp的新版本,目前(2016年1月6日)没有预定的发布日期。此外,为了清楚起见,还没有决定是否以及如何通过iTextSharp的下一个版本或任何后续版本支持UWP。

The alternative is to wait for a new release of iTextSharp, for which there is currently (Jan 6th, 2016) no scheduled release date. Also, just to be clear, no decision has been made whether and how to support UWP by the next release or any following release in iTextSharp.

我最近写了一篇关于这个问题的博客文章,作为iText团队的官方声明。 http://itextpdf.com/blog/itextsharp-and-uwp

I recently wrote a blog post about this problem, as an official statement from the iText team. http://itextpdf.com/blog/itextsharp-and-uwp

这篇关于带有Windows 10 Universal App的iTextSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆