Use Case: Import/Export with the Glasswall Embedded Engine
Image resizer can sit between the Embedded Engine content export and import functionality as a method of reducing the size in bytes of embedded images before importing back into their parent document.
Some users may perform extra operations on these embedded images before importing them back into the clean file. The image resizer can help manage the size of these images and even reduce the overall size of the document in some cases.
To read more about the Glasswall Embedded Engine content export and import, see Content Export and Import.
In this guide, we will use the Embedded Engine wrappers to perform export and import on a sample PDF file, then resize the images before importing back into the original format. Examples and a breakdown of the various Embedded Engine API functions can be found here.
Getting started
Prerequisites
- The Embedded Engine SDK
- A valid license with the Export and Import capabilities
- The relevant libraries for your chosen Embedded Engine wrapper
- The Image Resizer CLI
Sample file
Attached is a PDF sample file, the document contains 2 JPEG embedded images: Sample.pdf
Configuration
The following XML file includes a policy set to sanitise all content.
It is also configured to export images to their raw format under the sysconfig section, see export_embedded_images. For more on engine configuration info see Configuration Management.
<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<pdfConfig>
<acroform>sanitise</acroform>
<actions_all>sanitise</actions_all>
<embedded_files>sanitise</embedded_files>
<embedded_images>sanitise</embedded_images>
<external_hyperlinks>sanitise</external_hyperlinks>
<internal_hyperlinks>sanitise</internal_hyperlinks>
<javascript>sanitise</javascript>
<metadata>sanitise</metadata>
<digital_signatures>sanitise</digital_signatures>
<value_outside_reasonable_limits>sanitise</value_outside_reasonable_limits>
</pdfConfig>
<wordConfig>
<embedded_files>sanitise</embedded_files>
<embedded_images>sanitise</embedded_images>
<external_hyperlinks>sanitise</external_hyperlinks>
<internal_hyperlinks>sanitise</internal_hyperlinks>
<macros>sanitise</macros>
<metadata>sanitise</metadata>
<review_comments>sanitise</review_comments>
<dynamic_data_exchange>sanitise</dynamic_data_exchange>
</wordConfig>
<xlsConfig>
<embedded_files>sanitise</embedded_files>
<embedded_images>sanitise</embedded_images>
<external_hyperlinks>sanitise</external_hyperlinks>
<internal_hyperlinks>sanitise</internal_hyperlinks>
<macros>sanitise</macros>
<metadata>sanitise</metadata>
<review_comments>sanitise</review_comments>
<dynamic_data_exchange>sanitise</dynamic_data_exchange>
<connections>sanitise</connections>
</xlsConfig>
<pptConfig>
<embedded_files>sanitise</embedded_files>
<embedded_images>sanitise</embedded_images>
<external_hyperlinks>sanitise</external_hyperlinks>
<internal_hyperlinks>sanitise</internal_hyperlinks>
<macros>sanitise</macros>
<metadata>sanitise</metadata>
<review_comments>sanitise</review_comments>
</pptConfig>
<tiffConfig>
<geotiff>sanitise</geotiff>
</tiffConfig>
<svgConfig>
<scripts>sanitise</scripts>
<foreign_objects>sanitise</foreign_objects>
<hyperlinks>sanitise</hyperlinks>
</svgConfig>
<webpConfig>
<metadata>sanitise</metadata>
</webpConfig>
<sysConfig>
<interchange_type>sisl</interchange_type>
<export_embedded_images>false</export_embedded_images>
</sysConfig>
</config>
Step 1: Export
Let's first export the original file. Ensure paths to the XML configuration file and your Glasswall license are to hand.
Note: These examples do not contain error handling logic, see Return Types for more information.
C#
using glasswall_core2;
var filePath = "<path-to-input-file>";
var pathToConfig = "<path-to-config>";
var licenseFilePath = "<path-license-file>";
var outputDirectory = "<output-directory>";
Glasswall glasswall = new Glasswall();
int session = 0;
session = glasswall.OpenSession();
glasswall.RegisterLicenseFile(session, licenseFilePath);
glasswall.RegisterPoliciesFile(session, pathToConfig, 0);
glasswall.RegisterInputFile(session, filePath);
glasswall.RegisterExportFile(session, Path.Combine(outputDirectory, $"export.zip"));
glasswall.RunSession(session);
glasswall.CloseSession(session);
Upon successful Export of the sample file, an archive named export.zip should have been created in your chosen output directory.
Inspecting this ZIP file should reveal:
- Several SISL files
- 2 JPEG images
- 41 KB file
- 2 KB file
- 2 JSON files
For more detail on the contents of the export, see Export Package Content
Next, extract the JPEG images from the ZIP for use with the Image Resizer CLI.
Step 2: resize images
Now the embedded images from our sample PDF file have been extracted, we can resize them before reconstructing the clean file.
Details on how to use the CLI can be found by running the -h command. There is also a reference here.
Resize an image
Lets resize the 41 KB JPEG file. Ensure the filename remains unchanged as the Embedded Engine will look for JPEG files with the same names from the ZIP in the next step.
Below we are resizing the 41KB extracted JPEG image from the export ZIP, with a target of 20 KB (20,000 bytes).
mkdir output
./gw_image_resizer -i <image>.jpeg -o output/<image>.jpeg -t 20000
We should now have another JPEG file with the exact same name but with a size of ~20KB.
Replace the image in the export zip
Now we can replace the original 41 KB JPEG file with our resized one by dropping it into export.zip.
Step 3: import
Using the .zip file containing our resized images, let's import it back into a PDF file.
C#
using glasswall_core2;
var exportZipPath = "<export-zip-path>";
var pathToConfig = "<path-to-config>";
var licenseFilePath = "<path-license-file>";
var outputDirectory = "<output-directory>";
Glasswall glasswall = new Glasswall();
int session = 0;
session = glasswall.OpenSession();
glasswall.RegisterLicenseFile(session, licenseFilePath);
glasswall.RegisterPoliciesFile(session, pathToConfig, 0);
glasswall.RegisterImportFile(session, exportZipPath);
glasswall.RegisterOutFile(session, Path.Combine(outputDirectory, $"Sample.Clean.pdf"));
glasswall.RunSession(session);
glasswall.CloseSession(session);
Our sample file becomes Sample.Clean.Pdf and has been sanitised. It also includes the resized JPEG file.