Skip to content

Load a document

This operation covers the static factory and instance methods that construct a Document and populate its in-memory bytes from a file path or a byte array.

Field Value
Operation ID OP-09
Trigger Caller invokes Document.Load(string, bool), Document.Load(byte[], string), Document.CreateEmpty(string), or the instance Load()
Inputs File path and mustExist flag, or bytes + extension
Outputs A new/loaded Document, or true/false from the instance Load()
Primary components PdfAPI.Core.Document.Load, Document.CreateEmpty, instance Document.Load()
Entry point PdfAPI.Core\Document.cs

Steps

  1. Generate a new Guid as the document Id (static factories). (BR-19)
  2. For the path overload: read all bytes when the file exists; when it does not exist and mustExist is true, throw Exception with the missing path; otherwise continue with null bytes. (BR-20)
  3. Trim leading dots from the supplied extension and compose FileName as {Id}.{FileExtension}. (BR-21)
  4. Instance Load(): return true immediately when bytes are already in memory; otherwise read FilePath from disk when it exists and return true; return false when neither is available. (BR-05)

Process flow

The following diagram visualizes this operation's control flow.

flowchart TD
    Start(["Document.Load / CreateEmpty"]) --> Id["Assign new Guid Id"]
    Id --> Path{Path overload?}
    Path -->|Yes| Exists{File exists?}
    Exists -->|Yes| Read["Read all bytes"]
    Exists -->|No| MustExist{mustExist?}
    MustExist -->|Yes| Throw(["Throw Exception:<br>file does not exist"])
    MustExist -->|No| NullBytes["Continue with null bytes"]
    Path -->|No| BytesOverload["Assign supplied bytes"]
    Read --> Name["Trim extension,<br>compose FileName"]
    NullBytes --> Name
    BytesOverload --> Name
    Name --> Done([Return Document])

Failure and edge-case behavior

  • Loading from a non-existent path with mustExist = false succeeds but leaves File null; a subsequent instance Load() call on that document returns false (BR-05, BR-20).

Sources

  • PdfAPI.Core\Document.cs