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
- Follow the Getting started guide
- Initialize the Docutain .NET MAUI SDK as described here
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;
}
Document.WritePDF is also available synchronously. We recommend the asynchronous variant, since it keeps your UI thread free. See Asynchronous Methods for more details.
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:
| Value | Description |
|---|---|
FitToPages | Each page keeps the aspect ratio of the scanned image. This is the default. |
A4 / A4Landscape | A4 portrait / landscape. |
A5 / A5Landscape | A5 portrait / landscape. |
Letter / LetterLandscape | US Letter portrait / landscape. |
Legal / LegalLandscape | US 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);
maxSizeKB defaults to 0, which means no size limit.