Please, notice that this problem can only be reproduced with Visual Studio 2017 Preview. Otherwise, you won't be able to use iText7.
I'm posting this because it's very likely that the problem will remain when the final version comes out.
You can reproduce the issue following these steps:
- Create a new Universal Windows project targeting Windows 10 Insider Preview (build 16278), also as the minimum version.
- Add the package iText7, version 7.0.4-netstandard
- Add one sample PDF file to the root of the project with Buid Action=Content and named "doc.pdf". It can be any PDF. I'm using this one: sample PDF.
- Add this code to the MainPage.xaml.cs (code-behind)
MainPage.xaml.cs:
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
using (var stream = await GetStream("doc.pdf"))
{
var pdf = new PdfDocument(new PdfReader(stream));
var text = PdfTextExtractor.GetTextFromPage(pdf.GetPage(1));
}
}
private async Task<Stream> GetStream(string filename)
{
var uri = new Uri(new Uri("ms-appx:///"), filename);
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
return await file.OpenStreamForReadAsync();
}
}
}
- Run the project
It will throw the NullReferenceException like this:
at iText.Kernel.Font.DocType1Font.CreateFontProgram(PdfDictionary fontDictionary, FontEncoding fontEncoding, CMapToUnicode toUnicode)
at iText.Kernel.Font.PdfType1Font..ctor(PdfDictionary fontDictionary) at iText.Kernel.Font.PdfFontFactory.CreateFont(PdfDictionary fontDictionary) at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.GetFont(PdfDictionary fontDict) at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.SetTextFontOperator.Invoke(PdfCanvasProcessor processor, PdfLiteral operator, IList1 operands) at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.InvokeOperator(PdfLiteral operator, IList1 operands) at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.ProcessContent(Byte[] contentBytes, PdfResources resources) at iText.Kernel.Pdf.Canvas.Parser.PdfCanvasProcessor.ProcessPageContent(PdfPage page) at iText.Kernel.Pdf.Canvas.Parser.PdfTextExtractor.GetTextFromPage(PdfPage page, ITextExtractionStrategy strategy, IDictionary`2 additionalContentOperators) at iText.Kernel.Pdf.Canvas.Parser.PdfTextExtractor.GetTextFromPage(PdfPage page) at App1.MainPage.d__1.MoveNext()