4

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,

Jon H
  • 339
  • 7
  • 18

1 Answers1

6

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

blagae
  • 2,342
  • 1
  • 27
  • 48
  • Thank you for the quick response, I really appreciate it. Basically, if I understand you correctly, I can not use iTextSharp with Windows 10 UWP at this current time. Would you have any time frame when it would support UWP? I am guessing by the description that there is no work around for this issue. – Jon H Jan 05 '16 at 13:15
  • Indeed: as far as I understand the problem, current releases of iTextSharp cannot be used with UWP, and there is currently no roadmap for support for UWP. I'm experimenting with ways to make it work, but don't expect this to be available soon. There is no workaround for now, unless you build iTextSharp from source and are able to eliminate the transgressing code. If you succeed in doing this before I do, you're welcome to submit a pull request on https://github.com/itext/itextsharp :) – blagae Jan 05 '16 at 13:46
  • If I had the time I would be more than happy to take the challenge unfortunately I do not believe our time frame will allow me to do this. We are very close to our initial release and have to have a Windows 10 compatible version ready with our Windows 8.1 version. – Jon H Jan 05 '16 at 15:04