Document Translation Reflow

Sample code for using Apryse SDK to programmatically extract text as a XLIFF document to be able to be translated and how to reflow the translated result into the document. Sample code provided in Python, C++, C#, Java and Objective-C.

Learn more about our Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/PageSet.h>
9#include <Filters/MappedFile.h>
10#include <Filters/FilterReader.h>
11#include <Filters/FilterWriter.h>
12#include <PDF/ElementWriter.h>
13#include <PDF/ElementReader.h>
14#include <PDF/TransPDF.h>
15#include <PDF/TransPDFOptions.h>
16
17#include <iostream>
18#include <fstream>
19#include "../../LicenseKey/CPP/LicenseKey.h"
20
21using namespace std;
22using namespace pdftron;
23using namespace SDF;
24using namespace PDF;
25using namespace Filters;
26
27int main(int argc, char *argv[])
28{
29 int ret = 0;
30 PDFNet::Initialize(LicenseKey);
31
32 // Relative path to the folder containing test files.
33 string input_path = "../../TestFiles/";
34 string output_path = "../../TestFiles/Output/";
35
36 // The following sample illustrates how to extract xlf from a PDF document for translation.
37 // It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF.
38 try
39 {
40 // Open a PDF to translate.
41 PDFDoc doc(input_path + "tagged.pdf");
42 TransPDFOptions options = TransPDFOptions();
43
44 // Set the source language in the options
45 options.SetSourceLanguage("en");
46
47 // Set the number of pages to process in each batch
48 options.SetBatchSize(20);
49
50 // Optionally, subset the pages to process
51 // This PDF only has a single page, but you can specify a subset of pages like this
52 // options.SetPages("-2,5-6,9,11-");
53
54 // Extract the xlf to file and field the PDF for translation
55 TransPDF::ExtractXLIFF(doc, output_path + "tagged.xlf", options);
56
57 // Save the fielded PDF
58 doc.Save(output_path + "tagged-fielded.pdf", SDFDoc::e_linearized);
59
60 // The extracted xlf can be translated in a system of your choice.
61 // In this sample a pre-prepared translated file is used - tagged_(en_to_fr).xlf
62 // Perform the translation using the pre-prepared translated xliff
63 TransPDF::ApplyXLIFF(doc, input_path + "tagged_(en_to_fr).xlf", options);
64
65 // Save the translated PDF
66 doc.Save(output_path + "tagged-fr.pdf", SDFDoc::e_linearized);
67 doc.Close();
68 }
69 catch(Common::Exception& e)
70 {
71 cout << e << endl;
72 ret = 1;
73 }
74 catch(...)
75 {
76 cout << "Unknown Exception" << endl;
77 ret = 1;
78 }
79
80 PDFNet::Terminate();
81 return ret;
82}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales