Skip to content
DemoFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Set DocumentAI Model ​

Overview ​

Before using OCR, Layout Analysis, Table Recognition you need to set the DocumentAI model path first.

SetDocumentAIModel supports an optional gpu_id parameter used to specify the GPU device index for the AI model. When gpu_id is -1, GPU acceleration is disabled.

Model Packages ​

The AI capabilities of ComPDF Conversion SDK (layout analysis, table recognition, OCR, orientation correction, and so on) depend on model files. The SDK package ships with the base model documentai.model, so the default configuration requires no extra download. Download an extension package only when you need higher accuracy or extended features, place it in a directory of your choice, and pass its path to LibraryManager.SetDocumentAIModel.

Download address (all model packages, including the base package): https://download.compdf.com/download/models

There is one base package and several extension packages:

FileSizePurposeAvailability / When needed
documentai.modelabout 230 MBBase package: layout analysis, table recognition (balanced profile), and all basic OCR languagesIncluded in the SDK package; required for any AI feature (layout analysis, OCR, table recognition)
table_legacy.modelabout 270 MBTable extension package: resources required by the high-accuracy profileSeparate download; only needed when you use the TableRecognitionAccuracy.e_HighAccuracy profile, not needed for the default balanced profile
ocr_server.modelabout 139 MBHigh-accuracy OCR enhancement package, covering Chinese, English, Japanese, and other languagesSeparate download; needed when you require higher OCR accuracy
document_process.modelabout 37 MBDocument orientation classification and skew correctionSeparate download; needed when you use EnableDocumentOrientationClassification (orientation classification) or EnableDocumentDewarp (dewarping)

The four files total about 676 MB. No extension package needs to be downloaded unless you need the extra features: the default configuration only uses the documentai.model shipped in the SDK package.

Load Model Packages ​

All model packages are loaded through the same interface:

c#
// Load the base package first.
LibraryManager.SetDocumentAIModel("path/documentai.model", -1);
// Load extension packages when needed; capabilities that are already loaded are kept.
LibraryManager.SetDocumentAIModel("path/table_legacy.model", -1);
LibraryManager.SetDocumentAIModel("path/document_process.model", -1);
  • Each call loads only the capabilities contained in that file and merges them with the capabilities already loaded; it does not replace them. There is no required loading order.
  • The second parameter gpu_id specifies the GPU device index used by the AI model. -1 means the GPU is not used, and the default is -1.
  • If a package fails to load, the capabilities loaded before are kept and a specific error code is returned. The SDK does not silently degrade.
  • Calling ReleaseDocumentAIModel() releases all loaded models. You must load them again before using AI features afterwards.
  • While a conversion task is running, or while a release has not yet completed, loading again is rejected and ErrorCode.e_ErrConverting is returned.

Version Matching ​

  • Model packages must match the SDK version. The documentai.model shipped in the SDK package is the version matching 4.3.0; please also download the 4.3.0 version of any extension package.
  • The documentai.model of 4.2.0 and earlier does not contain the resources required by the balanced profile, while 4.3.0 keeps the same file name. If you copied an older documentai.model into your own deployment directory or project, overwrite it with the new file from the 4.3.0 SDK package when upgrading; do not keep using the old file with the same name. Loading an old model returns an error and does not automatically fall back to the high-accuracy profile.
  • Do not replace only the dynamic libraries or only the model files; they must be updated together.

Troubleshooting ​

SymptomPossible cause
SetDocumentAIModel returns an errorWrong path; the model file does not match the SDK version; the file is incomplete (interrupted download)
Tables look different from (or worse than) expected after selecting HighAccuracytable_legacy.model has not been loaded (tables fell back to rule-based recognition)
A conversion returns ErrorCode.e_ErrLicenseOCRPermissionDeny (29)The license does not include the OCR permission; unrelated to the model

Set AI Model Instance Count ​

If you need to control the number of Layout Analysis and Table Recognition model instances, call SetDocumentAIModelCount:

c#
// The first parameter is the number of Layout Analysis model instances,
// the second is the number of Table Recognition model instances.
LibraryManager.SetDocumentAIModelCount(1, 1);

If this interface is not called, the default model instance count is 1 for both.

Sample ​

c#
// Set the OCR model.
string modelPath = "***";
LibraryManager.SetDocumentAIModel(modelPath);

Use Your Own AI Engine (SDK v4.1.0+) ​

This option is available only in SDK v4.1.0 or later. If you prefer to run OCR, Layout Analysis, or Table Recognition with your own model or a third-party service instead of the bundled DocumentAI model, the SDK exposes callback hooks on ConvertCallback that let you supply the results as JSON. See 3.12 Use Custom AI Models via Callbacks for details. When all three capabilities you need are covered by your own callbacks, SetDocumentAIModel does not have to be called.