Hex Editor - Binary File Editing Software for Windows
Download Hex Editor Neo Hide this button

Working with Document Object

This section illustrates several document object usage scenarios. All examples in this section suppose that you have a valid reference to a file document object, as described in Creating Document Object section.

All actions that lead to document modification create new operations in document's operation history.

Initializing Document Object

Before you start using the newly created document object, you must initialize it.

If you want to associate a document object with any existing file, call the IFileDocument.Open method. If you want to associate a document object with an empty file, call the IFileDocument.New method. After that, the document object enters the initialized state and is ready to process further requests.

To open a volume, call the IFileDocument.OpenVolume method. You may also open a physical disk (IFileDocument.OpenPhysicalDisk) and process memory (IFileDocument.OpenProcess).

User Interface Analogue: Creating New Documents and Opening Documents.

Closing Document Object

The IFileDocument.Close method is used to close the document object. The object enters an uninitialized state after it is closed. All changes made to the document are silently discarded.

You don't need to explicitly call this method as it is automatically called when object is destroyed. Although, if you are going to reuse the document object (that it, call Open or New method again), this method must be called.

Saving Changes

The IFileDocument.Modified property is TRUE if the document has been modified and FALSE otherwise. It can be used to determine whether the document must be saved. To save a document, call the IFileDocument.Save or IFileDocument.SaveAs methods. The first method overwrites the existing file while the second method creates a copy of the file.

If the document object was initialized with a call to IFileDocument.New method, IFileDocument.Save method cannot be called until you call IFileDocument.SaveAs method that also updates the IFileDocument.FileName property. IFileDocument.FileName property contains the full path to the file this document is currently representing.

User Interface Analogue: Saving Documents.

Reading and Writing Data

The IFileDocument.Read and IFileDocument.Write methods are used to read and write file's data. Calling the IFileDocument.Write method creates new operation in file's operation history.

User Interface Analogue: Data Modification.

Inserting Data

Several file document object's methods are used to insert data into the file. All insert methods increase the file's size and shift remaining data to free up space for inserted data.

IFileDocument.Insert, IFileDocument.InsertByte and IFileDocument.InsertPattern methods are used to insert data into the document.

User Interface Analogue: Data Modification and Insert command.

Deleting Data

IFileDocument.Delete and IFileDocument.DeleteMulti methods are used to delete data from the document. After deletion, the size of the file is decreased and remaining data is shifted.

User Interface Analogue: Delete command.

Filling Data

Unlike Insert group of methods, Fill methods overwrite original file's data with new data.

IFileDocument.Fill, IFileDocument.FillMulti, IFileDocument.FillByte and IFileDocument.FillByteMulti methods are used to fill data.

User Interface Analogue: Fill command.

Find and Find All

Call the IFileDocument.Find method to find a pattern in a document. Call the IFileDocument.FindAll method to locate all occurrences of a pattern in a document.

User Interface Analogue: Find and Find All commands.

Replace and Replace All

The IFileDocument.Replace method is used to replace a single occurrence of the pattern with another pattern. The powerful IFileDocument.ReplaceAll method automatically locates all occurrences of specified pattern in a file and replace them with another pattern.

User Interface Analogue: Replace and Replace All commands.

Calculating Statistics

The IFileDocument.GatherStatistics method calculates the General Statistics for the document (or selection) and IFileDocument.PatternStatistics method calculates the Pattern Statistics for it.

User Interface Analogue: Statistics Tool Window.

Inserting Files

The IFileDocument.PutFile method is used to put the contents of a disk file to the current document.

User Interface Analogue: Insert File command.

Controlling File's Size

The read-write IFileDocument.FileSize property can be used to query or change the file's size. If you use it to change the size, new operation in operation history is created.

User Interface Analogue: Change File Size command.

Undo and Redo

The IFileDocument.CanUndo and IFileDocument.CanRedo properties are TRUE if IFileDocument.Undo and IFileDocument.Redo methods may be called for a document correspondingly. The IFileDocument.Undo method undoes the previous operation, placing it into the operation tail. IFileDocument.Redo method makes the first operation from operation tail a current operation.

User Interface Analogue: Edit » Undo and Edit » Redo commands.

Working with Operation History

IFileDocument.Purge, IFileDocument.SaveHistory and IFileDocument.LoadHistory methods are used to purge an operation history, save it to a file or load from a file correspondingly.

User Interface Analogue: History » Clear History and History » Purge… commands.

##Patch Creation

The IFileDocument.CreatePatch method is used to create a patch from the current state of the document.

User Interface Analogue: File » Create Patch… command.

Clipboard Methods

File document object's IFileDocument.Copy, IFileDocument.Cut and IFileDocument.Paste2 methods are used to put file's data to the Clipboard or paste data from the Clipboard into the document.

The IFileDocument.PasteText method is used to paste text from the Clipboard as it is, either in ANSI or UNICODE. You can also use the IFileDocument.PasteText2 method to paste the text from the Clipboard and convert it to a given encoding.

User Interface Analogue: Edit » Copy, Edit » Merge and Copy, Edit » Cut, Edit » Merge and Cut and Edit » Paste commands.

Bitwise and Other Operations

Four methods, IFileDocument.BitwiseOp, IFileDocument.ArithmeticOp, IFileDocument.ShiftOp and IFileDocument.CaseOp, are used to perform bitwise, arithmetic, shift and case change operations correspondingly.

Regular Expressions

The following methods accept regular expression as their parameters: IFileDocument.FindRegExp2, IFileDocument.FindAllRegExp2, IFileDocument.ReplaceAllRegExp2 and IFileDocument.PatternStatisticsRegExp2.