2

I'm trying to create a new Aspose.Pdf.Document using a FileStream to a new File, but it always throws an "Incorrect File Header" Exception. I need to work with the FileStream so that I can incrementally save the Document when merging other Pdf documents without keeping all of the Streams in scope.

According to the documentation, the following is the code to create a Document with a FileStream (I changed FileMode.Open to FileMode.OpenOrCreate since I don't have an existing Pdf file and want to start with a blank Document).

await using var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
var document = new Document(fileStream);

This code throws an "Incorrect File Header" Exception unless the FileStream points to an existing valid Pdf file.

The following code works, but it's kind of silly to create and dispose a Document just so that we can work with the Document through the FileStream.

var fileName = Path.GetTempFileName();
var doc = new Document();
doc.Save(fileName);
doc.Dispose();

await using var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
var document = new Document(fileStream);

I have to be missing something painfully obvious, because this is an incredibly simple use case and I don't see anything about it when searching online.

Failwyn
  • 767
  • 8
  • 22
  • The file is completely blank, I would assume that there would be a way to tell Aspose that you want to create a new Pdf document using a blank file, rather than load a Pdf document from the file; If the hacky workaround I implemented is the only way to do it, I'll keep it, I'd just rather do it the right way. – Failwyn Jan 25 '22 at 14:28
  • But Aspose.Pdf should be able to handle creating a new File without the workaround I implemented, no? – Failwyn Jan 25 '22 at 15:04
  • The FileStream creates the file on disk, I tried with .tmp and .pdf, the filename doesn't matter; I believe the error is that Aspose is trying to load a Pdf from the document rather than identifying that the file has no contents, so it should just create a new Document that can be saved to the FileStream. It could just be a limitation of Aspose.Pdf, I hope not, but it probably is... – Failwyn Jan 25 '22 at 15:21
  • 1
    I posted the question there, I will update this post with their response. – Failwyn Jan 25 '22 at 17:23

1 Answers1

2

You cannot initialize the Document object with an empty Stream or invalid PDF file. File or Stream should be a valid PDF document. In order to use the incremental saving approach, you can initialize the FileStream with a new file and keep saving the Document into it. For example, please check the below sample code snippet:

using var fileStream = new FileStream(dataDir + "output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
{
 var document = new Document();
 document.Pages.Add();
 document.Save(fileStream);
 document.Pages.Add();
 document.Save(fileStream);
}

Please note that the FileStream needs to remain open during the whole process of PDF generation. Along with that, you can also use Document.Save(); method (without any constructor) to implement incremental saving.

We believe that you have also posted a similar inquiry in Aspose.PDF official support forum and we have responded to you there as well. You can please follow up on it there and carry on the discussion in case you need more information.

This is Asad Ali and I work as Developer Evangelist at Aspose.

Asad Ali
  • 361
  • 1
  • 15
  • Yes and thanks for your time and assistance Asad Ali, once we resolve the issue in the Aspose forums I will post all relevant information here to close out this question. Edit: Actually, I should probably accept this as the answer since the issue we are working through on the forum concerns incremental saves rather than just creating a Document with a FileStream to a new File. – Failwyn Jan 26 '22 at 17:59