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¶
- Generate a new
Guidas the documentId(static factories). (BR-19) - For the path overload: read all bytes when the file exists; when it does not exist and
mustExistis true, throwExceptionwith the missing path; otherwise continue with null bytes. (BR-20) - Trim leading dots from the supplied extension and compose
FileNameas{Id}.{FileExtension}. (BR-21) - Instance
Load(): returntrueimmediately when bytes are already in memory; otherwise readFilePathfrom disk when it exists and returntrue; returnfalsewhen 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 = falsesucceeds but leavesFilenull; a subsequent instanceLoad()call on that document returnsfalse(BR-05, BR-20).
Related documentation¶
Sources¶
PdfAPI.Core\Document.cs