Start Using KeyView
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 KeyView bin directory. Then add the following statement to your program code.
using KeyView.Filter;
Create a KeyView Session
To use KeyView, first create a KeyView 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 KeyView 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 KeyView in a multi-threaded application, create a unique session on each thread and do not share a session or document between threads.
Configure the Session
You can configure the behavior of KeyView by setting optional configuration options through the Config()
method of the Session
object. The following example enables logical ordering for PDF files:
session.Config().PdfLogicalReading(LogicalPDFDirection.automatic);
The Config()
method returns a reference to the active configuration, so you can also chain configuration options:
session.Config().HiddenText(true).HeaderAndFooter(true);
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);
}
}