Load PDF into Aspose.Words using .NET

Hello!
I’ve faced an issue while using Aspose.Words product described below.
And will be extremely grateful for any assistance.

Issue description

The problem revealed when I have tried to load the PDF document using a MemoryStream created from byte array. When I am calling new Document(memoryStream); it throws the exception below.

It works fine with docx documents or with same PDF and FileStream. But I am using Aspose.Words in the web app and I do want to avoid any interaction with a file system.

Exception

Aspose.Words.FileCorruptedException
  HResult=0x80131500
  Message=The document appears to be corrupted and cannot be loaded.
  Source=Aspose.Words
  StackTrace:
   at Aspose.Words.Document.(Stream , LoadOptions )
   at Aspose.Words.Document.(Stream , LoadOptions )
   at Aspose.Words.Document..ctor(Stream stream, LoadOptions loadOptions)
   at Aspose.Words.Document..ctor(Stream stream)
   at CompareDocs.AsposeTest.Program.Main(String[] args) in C:\Users\Serhii_Iordanov\source\repos\CompareDocs\CompareDocs.Aspose\Program.cs:line 24

  This exception was originally thrown at this call stack:
	System.IO.MemoryStream.Position.get()
	Aspose.Words.Document.(System.IO.Stream, Aspose.Words.LoadOptions)

Inner Exception 1:
ObjectDisposedException: Cannot access a closed Stream.

Code examples

byte[] documentBytes;

using (var fileStream = new FileStream("new.pdf", FileMode.Open))
{
    documentBytes = new byte[fileStream.Length];
    fileStream.Read(documentBytes, 0, (int)fileStream.Length);
}

using (var memoryStream = new MemoryStream(documentBytes))
{
    var document = new Document(memoryStream); // <- an exception goes here
}

Additional information

Aspose.Words version: 20.3.0
Execution environment: .NET Core 3.1, Windows
Tested on different valid PDF files

@siordanov

Please note that Aspose.Words does not import PDF file into its DOM. Please check the supported document formats by Aspose.Words from here:
Supported Document Formats

You can use Aspose.PDF to load PDF file into Document class and perform document processing tasks.

Please let us know if you have any more queries.

1 Like

Thank you for the quick response!

Yeap, I see that PDF is not included into the file types that can be loaded by Aspose.Words.
But I am a bit confused then with a fact that I can load it using the FileStream. Is it just partialy supported?

@siordanov

Aspose.Words does not import PDF into its DOM. Please use Aspose.PDF for your requirement.

Aspose.PDF.Document doesn’t contain the Compare method that I am trying to use in Aspose.Words.
Do you by any chance know, whether there is any way to compare Aspose.PDF.Document with Aspose.Words.Document?

@siordanov

Your query is more related to Aspose.PDF. We are moving this forum thread to Aspose.PDF forum where you will be guided appropriately.

@siordanov

Regretfully Aspose.PDF does not offer such functionality to compare two files. However, you can convert a PDF file into Word document and use Aspose.Words to achieve the purpose.