Skip to main content

PDF Creation

The Docutain SDK for .NET MAUI comes with the ability to create searchable and non-searchable PDF documents either from a scanned document with Docutain's Scanner SDK or imported file.

Initialization

Create a PDF

using Docutain.SDK.MAUI;

//...
//scan or import file
//...

var destinationPath = Path.Combine(FileSystem.CacheDirectory, "TestPDF.pdf");
var pdfFile = await Document.WritePDFAsync(destinationPath);
if(pdfFile != null)
{
//do something with the generated pdf file
}
else
{
//error occured
var error = DocutainSDK.LastError;
}
tip

Document.WritePDF is also available synchronously. We recommend the asynchronous variant, since it keeps your UI thread free. See Asynchronous Methods for more details.

info

A return value of null means the PDF could not be generated, DocutainSDK.LastError states the reason. If the SDK has not been initialized successfully, a DocutainSdkNotInitializedException is thrown instead. See Error Handling for more details.

Page format

You can specify the PDF page format by providing the appropriate value:

var pdfFile = await Document.WritePDFAsync(destinationPath, true, PDFPageFormat.A4);

The following page formats are available:

ValueDescription
FitToPagesEach page keeps the aspect ratio of the scanned image. This is the default.
A4 / A4LandscapeA4 portrait / landscape.
A5 / A5LandscapeA5 portrait / landscape.
Letter / LetterLandscapeUS Letter portrait / landscape.
Legal / LegalLandscapeUS Legal portrait / landscape.

The second parameter determines what happens if the target file already exists. If it is true, the existing file gets overwritten. If it is false, a number is appended to the file name, e.g. TestPDF(1).pdf.

PDF Compression

You can specify a maximum file size for the PDF. If the uncompressed PDF would exceed the maximum file size you have set, it will get compressed until the maximum file size is reached. Please be aware, that if the PDF gets compressed, the quality decreases and the PDF generation will take longer. In order to set a maximum file size, set the maxSizeKB parameter to your desired size. It needs to be set in KB, so if you want 10MB for example, set it to 10 * 1024. Please check below example:

var pdfFile = await Document.WritePDFAsync(destinationPath, true, PDFPageFormat.A4, 10 * 1024);
info

maxSizeKB defaults to 0, which means no size limit.