Start Using the Filter API
To use the .NET Filter SDK, open your project in Visual Studio and add a reference (Project > Add Project Reference) to the library filter_dotnet.dll
, which can be found in the bin
directory. Then add the following statement to your program code.
using KeyView.Filter;
Create a Session
To use the Filter API, first create a session:
using (Session session = new Session(binDir, license))
{
// Configure the session and open a document
}
binDir
should be a string
that holds the location of the Filter SDK binaries, and license
should be a string
that holds your license key.
The Session
class implements the IDisposable
interface, so you can enclose your use of the session in a using
block to ensure that resources are automatically freed when no longer required.
To use the Filter API in a multi-threaded application, create a unique session on each thread and do not share a session or document between threads.
Open a Document
A Document
object provides access to the data within a file, including the file format, text, metadata, and any subfiles. Obtain a Document
object from a file or stream by calling the Open()
method on your Session
. For example:
string inputFilePath = "InputFile.docx";
using (Document myDoc = session.Open(inputFilePath))
{
// Use the document object to detect the file format, filter text,
// access metadata and extract subfiles
}
Determine the Format of a Document
You can find the format of a document by using the Info()
method on a Document
object. For example:
using (Document myDoc = session.Open(inputFilePath))
{
Console.WriteLine("File class: " + myDoc.Info().DocClassName());
Console.WriteLine("File format: " + myDoc.Info().DocFormat().ToString());
Console.WriteLine("Description: " + myDoc.Info().Description());
Console.WriteLine("File version: " + myDoc.Info().Version().ToString());
}
Filter a Document
To filter a document and write the text to a file or stream, call the Filter()
method on a Document
object. For example:
using (Document myDoc = session.Open(inputFilePath))
{
try
{
// Filter to a file
myDoc.Filter(inputFilePath + ".filtered.txt");
// Alternatively, filter to a stream
// using (FileStream fs = File.Create(inputFilePath + ".filtered.txt"))
// {
// myDoc.Filter(fs);
// }
}
catch (KeyViewException e)
{
Console.WriteLine($"{e.Message}");
}
}
Access Metadata
You can access document metadata through the Metadata()
method on a Document
object. For example:
using (Metadata metadata = myDoc.Metadata())
{
foreach (var element in metadata) using (element)
{
// Process the metadata element
}
}
Extract Subfiles
You can iterate over subfile information using the Subfiles()
method on a Document
object. Each element returned by the iterator contains information about the subfile, and a method that you can use to extract it:
foreach (Subfile file in myDoc.Subfiles()) using (file)
{
Console.WriteLine($"Subfile {file.Index()}, size: {file.Size()}");
if (file.GetSubfileType() != SubfileType.Folder)
{
string outputPath = GenerateOutputPath(file);
file.Extract(outputPath);
}
}