Sample code for using the Apryse Server SDK to programmatically extract text as an XLIFF document, and to be able to translate and reflow the translated result into the document. Sample code is provided in Python, C++, C#, Java and Objective-C.
Learn more about our Server SDK.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2025 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 + "find-replace-test.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 + "find-replace-test.xlf", options);
56
57 // Save the fielded PDF
58 doc.Save(output_path + "find-replace-test-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 - find-replace-test_(en_to_fr).xlf
62 // Perform the translation using the pre-prepared translated xliff
63 TransPDF::ApplyXLIFF(doc, input_path + "find-replace-test_(en_to_fr).xlf", options);
64
65 // Save the translated PDF
66 doc.Save(output_path + "find-replace-test-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}
1//
2// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using pdftron;
8using pdftron.Common;
9using pdftron.Filters;
10using pdftron.SDF;
11using pdftron.PDF;
12using pdftron.FDF;
13
14// The following sample illustrates how to extract xlf from a PDF document for translation
15// It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF
16namespace TransPDFTestCS
17{
18 class Class1
19 {
20 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
21 static Class1() {}
22
23 [STAThread]
24 static void Main(string[] args)
25 {
26 PDFNet.Initialize(PDFTronLicense.Key);
27
28 // Relative path to the folder containing test files
29 string input_path = "../../TestFiles/";
30 string output_path = "../../TestFiles/Output/";
31
32 try
33 {
34 // Read a PDF document from a stream or pass-in a memory buffer...
35 FileStream istm = new FileStream(input_path + "find-replace-test.pdf", FileMode.Open, FileAccess.Read);
36
37 using (PDFDoc doc = new PDFDoc(istm))
38 {
39 TransPDFOptions options = new TransPDFOptions();
40
41 // Set the source language in the options
42 options.SetSourceLanguage("en");
43
44 // Set the number of pages to process in each batch
45 options.SetBatchSize(20);
46
47 // Optionally, subset the pages to process
48 // This PDF only has a single page, but you can specify a subset of pages like this
49 // options.SetPages("-2,5-6,9,11-");
50
51 // Extract the xlf to file and field the PDF for translation
52 TransPDF.ExtractXLIFF(doc, output_path + "find-replace-test.xlf", options);
53
54 // Save the fielded PDF
55 doc.Save(output_path + "find-replace-test-fielded.pdf", SDFDoc.SaveOptions.e_linearized);
56
57 // The extracted xlf can be translated in a system of your choice
58 // In this sample a pre-prepared translated file is used - find-replace-test_(en_to_fr).xlf
59
60 // Perform the translation using the pre-prepared translated xliff
61 TransPDF.ApplyXLIFF(doc, input_path + "find-replace-test_(en_to_fr).xlf", options);
62
63 // Save the translated PDF
64 doc.Save(output_path + "find-replace-test-fr.pdf", SDFDoc.SaveOptions.e_linearized);
65 doc.Close();
66 }
67 }
68 catch (PDFNetException e)
69 {
70 Console.WriteLine(e.Message);
71 }
72 PDFNet.Terminate();
73 }
74 }
75}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import java.io.File;
7import java.io.IOException;
8
9import javax.imageio.ImageIO;
10
11import com.pdftron.common.PDFNetException;
12import com.pdftron.pdf.*;
13import com.pdftron.sdf.SDFDoc;
14
15// The following sample illustrates how to extract xlf from a PDF document for translation
16// It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF
17public class TransPDFTest {
18
19 public static void main(String[] args)
20 {
21 PDFNet.initialize(PDFTronLicense.Key());
22
23 // Relative path to the folder containing test files
24 String input_path = "../../TestFiles/";
25 String output_path = "../../TestFiles/Output/";
26
27 // Open a PDF document to translate
28 try (PDFDoc doc = new PDFDoc(input_path + "find-replace-test.pdf"))
29 {
30 TransPDFOptions options = new TransPDFOptions();
31
32 // Set the source language in the options
33 options.setSourceLanguage("en");
34
35 // Set the number of pages to process in each batch
36 options.setBatchSize(20);
37
38 // Optionally, subset the pages to process
39 // This PDF only has a single page, but you can specify a subset of pages like this
40 // options.setPages("-2,5-6,9,11-");
41
42 // Extract the xlf to file and field the PDF for translation
43 TransPDF.extractXLIFF(doc, output_path + "find-replace-test.xlf", options);
44
45 // Save the fielded PDF
46 doc.save(output_path + "find-replace-test-fielded.pdf", SDFDoc.SaveMode.LINEARIZED, null);
47
48 // The extracted xlf can be translated in a system of your choice
49 // In this sample a pre-prepared translated file is used - find-replace-test_(en_to_fr).xlf
50
51 // Perform the translation using the pre-prepared translated xliff
52 TransPDF.applyXLIFF(doc, input_path + "find-replace-test_(en_to_fr).xlf", options);
53
54 // Save the translated PDF
55 doc.save(output_path + "find-replace-test-fr.pdf", SDFDoc.SaveMode.LINEARIZED, null);
56 doc.close();
57 }
58 catch (PDFNetException e)
59 {
60 e.printStackTrace();
61 System.out.println(e);
62 }
63
64 PDFNet.terminate();
65 }
66}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9// The following sample illustrates how to extract xlf from a PDF document for translation
10// It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF
11
12int main(int argc, char *argv[])
13{
14 @autoreleasepool {
15 int ret = 0;
16 [PTPDFNet Initialize: 0];
17
18 NSString *inputPath = @"../../TestFiles/";
19 NSString *outputPath = @"../../TestFiles/Output/";
20
21 @try
22 {
23 // Open the PDF to translate
24 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: [inputPath stringByAppendingString:@"find-replace-test.pdf"]];
25
26 PTTransPDFOptions* options = [[PTTransPDFOptions alloc] init];
27
28 // Set the source language in the options
29 [options SetSourceLanguage: @"en"];
30
31 // Set the number of pages to process in each batch
32 [options SetBatchSize: 20];
33
34 // Optionally, subset the pages to process
35 // This PDF only has a single page, but you can specify a subset of pages like this
36 // [options SetPages: "-2,5-6,9,11-"];
37
38 // Extract the xlf to file and field the PDF for translation
39 [PTTransPDF ExtractXLIFF: doc output_xliff: [outputPath stringByAppendingString:@"find-replace-test.xlf"] options: options];
40
41 // Save the fielded PDF
42 [doc SaveToFile : [outputPath stringByAppendingString:@"find-replace-test-fielded.pdf"] flags: e_ptlinearized];
43
44 // The extracted xlf can be translated in a system of your choice
45 // In this sample a pre-prepared translated file is used - find-replace-test_(en_to_fr).xlf
46
47 // Perform the translation using the pre-prepared translated xliff
48 [PTTransPDF ApplyXLIFF: doc incoming_xliff : [inputPath stringByAppendingString:@"find-replace-test_(en_to_fr).xlf"] options: options];
49
50 // Save the translated PDF
51 [doc SaveToFile : [outputPath stringByAppendingString:@"tagged-fr.pdf"] flags: e_ptlinearized];
52 [doc Close];
53 }
54 @catch(NSException *e)
55 {
56 NSLog(@"%@", e.reason);
57 ret = 1;
58 }
59
60 [PTPDFNet Terminate: 0];
61 return ret;
62 }
63}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales