Save document incrementally

Hi Aspose, I’d like to create new pdf file and save content of it incrementally to avoid huge memory using. I see that Document.Save supports it if I create document based on opened file stream.

using (var file = new FileStream("test1.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
    using (var document = new Document(file)) {
            foreach(....) {
                   //...... appending data
                  document.Save();
            }
    }
}

This code throws exception ‘Incorrect file header’. I tried to open document without association with stream and use save overload that takes stream. But for me it seems that it overwrite file each call, Can you help me with it, please? Thanks

In this example I create empty document to avoid crash and when I save it, document contains 3 empty pages without content

using (var doc = new Document()) {
    doc.Save(resultPdfPath);
}
using (var file = new FileStream(resultPdfPath, FileMode.Open, FileAccess.ReadWrite)) {
    using (var doc1 =  new Document(file)) {
         ///modifying document
        doc1.Save();
    }
}

Found this method HasIncrementalUpdate. It returns for me false. Do you have criterias which document couble incrementally saved?

@mpek

We tested using below code snippet in our environment and we did not notice any issues:

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

test1010.pdf (1.8 KB)

Moreover, incremental updates refer to modifications made to a PDF document without rewriting the entire file. To check if a PDF document has been incrementally updated, you can use the HasIncrementalUpdate method from the Document class in the Aspose.PDF for .NET library. This method will return a boolean value indicating whether any incremental updates were detected in the document.

Can you please make sure that you are using the API with a valid license and you are using the latest version of it? If you still face any issues, please let us know.

@asad.ali
Hi, sorry, but I didn’t get answers for my questions. I’d like to save document incrementially. HasIncrementalUpdate returns me false all times.

  1. What I should do to have IncrementalUpdate for document.
  2. Is it possible to create document from scratch, populate data and incrementally save?

Below example:

var resultPdfPath = "test1.pdf";
using (var doc = new Document()) { //I'd like to avoid this code. Without it have crash in next document ‘Incorrect file header’.
    doc.Save(resultPdfPath);
}
using (var file = new FileStream(resultPdfPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
    using (var document = new Document(file)) {
        bool hasIncrementalUpdate = document.HasIncrementalUpdate(); //returns false for me. I'd like to have true because in next lines I'll generate a lot of data and want to save it incrementally
        var page = document.Pages.Add();
        page.Paragraphs.Add(new TextFragment("Hello World!")); // For me, as result I'l have document with one blank page without "Hello World!" text
        document.Save();
    }
}

@mpek

Aspose.PDF allows incremental saving of a file and you do not have to set some flag or property to enable it. The HasIncrementalUpdate property is only used to determine whether a particular document has incremental updates before or not.

Can you please share if you are using the API with a valid or 30-days free temporary license?

We will log an investigation ticket in our issue tracking system for this feature and will be sharing the ID with you.

@asad.ali I used API via your license. My Customer Purchase Order No. CC1559. Regarding HasIncrementalUpdate what I should do to use incremental update option for my newly created document?

@mpek

As shared earlier, you do not need to enable incremental updates in the Document (new or existing). The Document.Save() method can be used to save the changes incrementally while processing and modifying the files. The existing property and method you are seeing is only to determine if document has incremental changes or not.

Does this answer your question? Please share your feedback so that we can further proceed accordingly with the issue where you want to prevent the step of saving the file first to use incremental updates features.

@asad.ali yes, thank you. Previously, I supposed that not every document support incremental save and this method shows me whether this document supports it or not. Than I got it, thank you

@mpek

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-60092

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.