Random occurrence of "Input string was not in a correct format." when loading a PDF document

I get a seemingly random occurrence of this exception. It happens e.g. once in 2-3-4 attempts to execute the same code with loading the same PDF file.

Here’s the code (slightly modified from "Cannot handle iref streams." in PDF with GemBox.Document ) :

using ( MemoryStream outputPdfStream = new MemoryStream() )
			{
				PdfDocument pdfDocument = password == null
					? PdfDocument.Load( path )
					: PdfDocument.Load( path,
						new GemBox.Pdf.PdfLoadOptions { Password = password } );

				pdfDocument.SaveOptions.CrossReferenceType = PdfCrossReferenceType.Table;
				pdfDocument.SaveOptions.Encryption = null;
				pdfDocument.Save( outputPdfStream );

				return DocumentModel.Load( outputPdfStream );
			}

The exception is:

------------------------------
Program Location:

   at GemBox.Document.PdfLoadOptions.7k4lmxmhj5tk8h83eglk6fss34tasuhn  (Stream , String )
   at GemBox.Document.DocumentModel.Load(Stream stream)

===================================

Input string was not in a correct format. (mscorlib)

------------------------------
Program Location:

   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at   . ()
   at   .()
   at   .(   )
   at   .(PdfArray , Boolean )
   at   .(   )
   at   .(   , Boolean )
   at   .(PdfReferenceTable )
   at   .()
   at   .(Stream , String ,    ,    )
   at GemBox.Document.PdfLoadOptions.7k4lmxmhj5tk8h83eglk6fss34tasuhn  (Stream , String )

Any clues what could be the source of these random exceptions?

Hi Ivan,

Can you send us one of those PDF files that reproduce this so that we can investigate it?

Regards,
Mario

class Program
{
	static DocumentModel _LoadPdfFile(string path, string password = null)
	{
		using (MemoryStream outputPdfStream = new MemoryStream())
		{
			PdfDocument pdfDocument = password == null ? 
				PdfDocument.Load(path, new GemBox.Pdf.PdfLoadOptions()) :
				PdfDocument.Load(path, new GemBox.Pdf.PdfLoadOptions {Password = password});

			pdfDocument.SaveOptions.CrossReferenceType = PdfCrossReferenceType.Table;
			pdfDocument.SaveOptions.Encryption = null;
			pdfDocument.Save(outputPdfStream);

			return DocumentModel.Load(outputPdfStream);
		}
	}


	static void Main()
	{
		GemBox.Pdf.ComponentInfo.SetLicense("FREE-LIMITED-KEY");
		GemBox.Document.ComponentInfo.SetLicense("FREE-LIMITED-KEY");

		GemBox.Pdf.ComponentInfo.FreeLimitReached += (sender, e) =>
			e.FreeLimitReachedAction = GemBox.Pdf.FreeLimitReachedAction.Stop;

		GemBox.Document.ComponentInfo.FreeLimitReached += (sender, e) =>
			e.FreeLimitReachedAction = GemBox.Document.FreeLimitReachedAction.Stop;

		for (int i = 0; i < 100; i++)
		{
			try
			{
				_LoadPdfFile(@"CustomInvoice.pdf");
			}
			catch (Exception e)
			{
				Console.WriteLine("Failed at iteration " + i);
				Console.WriteLine(e);
			}
		}

		Console.Read();
	}
}

NOTE: If I do just DocumentModel.Load(path) without doing pdfDocument.SaveOptions.CrossReferenceType = PdfCrossReferenceType.Table; it seems to work fine.

It happens both with CustomInvoice.pdf (from your examples at /document/examples/c-sharp-read-extract-pdf-text/305 ) and another random PDF file I’ve tried.

Hi Ivan,

I was unable to reproduce this issue, I tried with our CustomInvoice.pdf and with an encrypted version of it (used the Password Protect a PDF online for free | Adobe Acrobat).

Can you please send us a small Visual Studio project that reproduces your issue so that we can investigate it?

Regards,
Mario

The code that I sent is a console app, you just need to add the references and using statements:

using System;
using System.Collections.Generic;
using System.IO;
using GemBox.Document;
using GemBox.Document.Tables;
using GemBox.Pdf;

I updated to the latest versions of the DLLs but got the same issue

image

From 100 iterations e.g. 5-7 fail (for no good reason).

The issue gets reproduced here, too PDF Fail | C# Online Compiler | .NET Fiddle

Hi Ivan,

Please try again with this bugfix:
https://www.gemboxsoftware.com/document/nightlybuilds/GBD33v1341.zip

Or this NuGet package:
Install-Package GemBox.Document -Version 33.0.1341-hotfix

Does this solve your issue?

Regards,
Mario

Thanks, that solved it!