# Import and export

This page drives you through available possibilities to import content into iink SDK or to export it for external usage.

Interactive Ink SDK differentiates serialization/deserialization (storage of the full content of the model in a fast and space efficient manner for future reuse by the SDK) and import/export (as a way to exchange iink content with other applications).

## Importing content

### Import into a block

This part describes import behavior for all cases but text data import from JIIX, which is described here.

You can import data into content blocks. For example, the following code will import the “Hello iink SDK” text string into a “Text” part:

IINKContentPart *textPart = ... // Get the part
self.editor.part = textPart;
[self.editor import_:IINKMimeTypeText data:@"Hello iink SDK" block:self.editor.rootBlock error:nil];

In this case, you could have omitted to specify the block. As the part only hosts a single root block, iink SDK can figure by itself where to import the content:

[self.editor import_:IINKMimeTypeText data:@"Hello iink SDK" block:nil error:nil];

For parts that can host multiple blocks, such as “Text Document” parts, you need to explicitly specify the target block. If it does not exist yet, you can call addBlock:type:error: and directly pass the data to import.

The list of supported mime types for a given block can be obtained by calling getSupportedImportMimeTypes: on the editor. For instance:

NSArray<IINKMimeTypeValue *> *supportedMimeTypes = [self.editor getSupportedImportMimeTypes:block];
Importing content is “destructive”: Pre-existing content will be cleared and replaced (except for text data import from JIIX).

### Text JIIX import

When it comes to textual data, JIIX import is so far limited to text words candidate changes. More import capabilities will be provided later on.

To change the text candidates within a given Text or Diagram block:

1. Export the block to the JIIX format.
2. Replace the label of the target word with another word from the candidates list.
3. Import the modified JIIX data back to your block.
Importing JIIX data is only possible if the target block was not modified since it was exported. For more information, refer to this page.

### Raw ink import

To import raw ink content, instantiate an editor and pass it an array of pointer events. Note that in this scenario, except if you are working on a “Drawing” part, the recognition engine will automatically process the new strokes. This approach is documented in the Editing part of this guide.

## Exporting content

### Make sure that recognition is complete

Recognition can sometimes take a certain time to complete, especially if you send many strokes to the editor at once.

If you want to make sure that you export the final recognition results, you have to call waitForIdle before export_:mimeType:error:.

### Select what you want to export

Export operations are made on content blocks. For instance, this allows you to export a specific diagram from a Text Document part).

You can retrieve the list of supported export mime types for a given block by calling getSupportedExportMimeTypes: on the editor:

NSArray<IINKMimeTypeValue *> *supportedMimeTypes = [self.editor getSupportedExportMimeTypes:block];

To export content, call the export_:mimeType:error: method of the editor object, passing it the block to export and the desired mime type:

// Export a math block to MathML
NSString *result = [self.editor export_:mathBlock mimeType:IINKMimeTypeMathML error:nil];

// Export a text document to docx
[self.editor export_:textDocBlock toFile:"export.docx" mimeType:IINKMimeTypeDOCX imageDrawer:imageDrawer error:nil];

The API provides a convenience method that lets you omit the mime type if iink SDK is able to guess it unambiguously from the file extension:

// Export a text document to docx
[self.editor export_:block toFile:"export.docx" imageDrawer:imageDrawer error:nil];
You can call the IINKMimeTypeGetFileExtensions: method defined in the IINKMimeTypeValue class from the IINKMimeType.h header file to get the extensions supported for a given mime type.

### Image drawer

Certain formats require you to provide an object conforming to the IINKImageDrawer protocol to let iink SDK generate images from the content. This is expectedly the case for png and jpeg exports, but also for formats such as docx.

A default, ready-to-use, image drawer implementation is provided by MyScript as part of the UI Reference Implementation.

If the format does not require an image drawer, you can provide the export method with a null pointer instead.

To know which formats require an image drawer, refer to this page.

### Textual vs. binary exports

Textual format exports are returned as a string that can then be programmatically manipulated. Binary formats, on the other hand, are saved as files on the disk at a location you can specify.

ImageLoader *imageLoader = [[ImageLoader alloc] init];
ImageDrawer *imageDrawer = [[ImageDrawer alloc] init];

[self.editor export_:self.editor.rootBlock toFile:@"out/export.docx" imageDrawer:imageDrawer error:nil];

You can call the IINKMimeTypeIsTextual: method defined in the IINKMimeTypeValue class of the IINKMimeType.h header file to know whether a format is textual or binary.

### Apply a specific configuration

Some export functions let you temporarily “override” the current editor configuration for the need of a specific export. This is useful if you want to tune the export parameters (like the type of information to export to JIIX) without impacting your global or your editor configurations.

The following example shows how you can export a block recognition result as JIIX without including original ink information:

// Create an empty parameter set
IINKParameterSet *params = [engine createParameterSet];
// Set the appropriate configuration to exclude strokes from the export
[params setBoolean:NO forKey:@"export.jiix.strokes" error:nil];
// Export a block with the new configuration
NSString *jiix = [self.editor export_:block mimeType:IINKMimeTypeJIIX overrideConfiguration:params error:nil];

## Supported imports/exports

### Exchange format

Interactive Ink SDK defines its own format, called JIIX (short for JSON Interactive Ink eXchange format).

This format provides a consistent representation of the different types of content that are supported, covering semantics, positions, styling and ink-related aspects.

Thanks to its JSON syntax, it stays readable and can be easily parsed, making it appropriate to exchange information with the host application or as a transitory representation to support custom export formats.

The complete JIIX reference can be found here.

### Other formats

Interactive Ink SDK allows you to import and export some commonly used formats, such as LaTeX for math content or Docx in the case of Text Document blocks.

The full list can be found here.

## Back to the example

For the calculator example, you already wrote some code that could let you save the history to the part metadata and retrieve it later. Let’s now see how you can populate the history string list using the export functionality.

Here, you will want to append a new item to the history after each computation that result in a new state (pressing “=” several times will just register a single state). You can thus improve the solve: method:

- (IBAction)solve:(id)sender
{
// Convert the content of the part
[self.editor convert:self.editor.rootBlock
targetState:IINKConversionStateDigitalEdit
error:nil];

// Add the item to the history if there was a change
NSString *latexStr = [editor export_:block mimeType:IINKMimeTypeLaTeX error:nil];
if ([latexStr length] > 0 && ([history count] == 0 || ![latexStr isEqualToString:[history lastObject]]))
}