iTextSharp with a Windows 10 Universal App

霸气de小男生 提交于 2019-12-22 08:44:42

问题


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)

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());
        }

and here

    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();
        }

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,


回答1:


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.

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.

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.

EDIT

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.

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.

EDIT 2

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



来源:https://stackoverflow.com/questions/34611859/itextsharp-with-a-windows-10-universal-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!