> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/get-started/samples/document-translation-reflow.md).

# Document Translation Reflow Using XLIFF

This sample shows how to use the Apryse Document Reflow to extract an XLIFF document and how to reflow it once translated

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 C++, C#, Java, Objective-C, and Python.

Learn more about our [Server SDK](/core/get-started/get-started.md).

{% tabs %}
{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/PageSet.h>
#include <Filters/MappedFile.h>
#include <Filters/FilterReader.h>
#include <Filters/FilterWriter.h>
#include <PDF/ElementWriter.h>
#include <PDF/ElementReader.h>
#include <PDF/TransPDF.h>
#include <PDF/TransPDFOptions.h>

#include <iostream>
#include <fstream>
#include "../../LicenseKey/CPP/LicenseKey.h"

using namespace std;
using namespace pdftron;
using namespace SDF;
using namespace PDF;
using namespace Filters;

int main(int argc, char *argv[])
{
	int ret = 0;
	PDFNet::Initialize(LicenseKey);

	// Relative path to the folder containing test files
	string input_path =  "../../TestFiles/";
	string output_path = "../../TestFiles/Output/";

	// The following sample illustrates how to extract xlf from a PDF document for translation
	// It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF
	try  
	{
		// Open a PDF to translate
		PDFDoc doc(input_path + "find-replace-test.pdf");
		TransPDFOptions options = TransPDFOptions();

		// Set the source language in the options
		options.SetSourceLanguage("en");

		// Set the number of pages to process in each batch
		options.SetBatchSize(20);

		// Optionally, subset the pages to process
		// This PDF only has a single page, but you can specify a subset of pages like this
		// options.SetPages("-2,5-6,9,11-");

		// Extract the xlf to file and field the PDF for translation
		TransPDF::ExtractXLIFF(doc, output_path + "find-replace-test.xlf", options);

		// Save the fielded PDF
		doc.Save(output_path + "find-replace-test-fielded.pdf", SDFDoc::e_linearized);

		// The extracted xlf can be translated in a system of your choice
		// In this sample a pre-prepared translated file is used - find-replace-test_(en_to_fr).xlf
		// Perform the translation using the pre-prepared translated xliff
		TransPDF::ApplyXLIFF(doc, input_path + "find-replace-test_(en_to_fr).xlf", options);

		// Save the translated PDF
		doc.Save(output_path + "find-replace-test-fr.pdf", SDFDoc::e_linearized);
		doc.Close();
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	PDFNet::Terminate();
	return ret;
}
```

{% endcode %}
{% endtab %}

{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
//
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
//

using System;
using System.IO;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;
using pdftron.FDF;

// The following sample illustrates how to extract xlf from a PDF document for translation
// It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF 
namespace TransPDFTestCS
{
	class Class1
	{
		private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
		static Class1() {}
		
		[STAThread]
		static void Main(string[] args)
		{
			PDFNet.Initialize(PDFTronLicense.Key);

			// Relative path to the folder containing test files
			string input_path =  "../../TestFiles/";
			string output_path = "../../TestFiles/Output/";

			try  
			{
				// Read a PDF document from a stream or pass-in a memory buffer...
				FileStream istm = new FileStream(input_path + "find-replace-test.pdf", FileMode.Open, FileAccess.Read);
				
				using (PDFDoc doc = new PDFDoc(istm))
				{
					TransPDFOptions options = new TransPDFOptions();
					
					// Set the source language in the options
					options.SetSourceLanguage("en");

					// Set the number of pages to process in each batch
					options.SetBatchSize(20);

					// Optionally, subset the pages to process
					// This PDF only has a single page, but you can specify a subset of pages like this
					// options.SetPages("-2,5-6,9,11-");

					// Extract the xlf to file and field the PDF for translation
					TransPDF.ExtractXLIFF(doc, output_path + "find-replace-test.xlf", options);

					// Save the fielded PDF
					doc.Save(output_path + "find-replace-test-fielded.pdf", SDFDoc.SaveOptions.e_linearized);

					// The extracted xlf can be translated in a system of your choice
					// In this sample a pre-prepared translated file is used - find-replace-test_(en_to_fr).xlf

					// Perform the translation using the pre-prepared translated xliff
					TransPDF.ApplyXLIFF(doc, input_path + "find-replace-test_(en_to_fr).xlf", options);

					// Save the translated PDF
					doc.Save(output_path + "find-replace-test-fr.pdf", SDFDoc.SaveOptions.e_linearized);
					doc.Close();
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}
			PDFNet.Terminate();
		}
	}
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.pdftron.common.PDFNetException;
import com.pdftron.pdf.*;
import com.pdftron.sdf.SDFDoc;

// The following sample illustrates how to extract xlf from a PDF document for translation
// It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF 
public class TransPDFTest {

	public static void main(String[] args) 
	{
		PDFNet.initialize(PDFTronLicense.Key());

		// Relative path to the folder containing test files
		String input_path = "../../TestFiles/";
		String output_path = "../../TestFiles/Output/";

		// Open a PDF document to translate
		try (PDFDoc doc = new PDFDoc(input_path + "find-replace-test.pdf"))
		{
			TransPDFOptions options = new TransPDFOptions();

			// Set the source language in the options
			options.setSourceLanguage("en");

			// Set the number of pages to process in each batch
			options.setBatchSize(20);

			// Optionally, subset the pages to process
			// This PDF only has a single page, but you can specify a subset of pages like this
			// options.setPages("-2,5-6,9,11-");

			// Extract the xlf to file and field the PDF for translation
			TransPDF.extractXLIFF(doc, output_path + "find-replace-test.xlf", options);

			// Save the fielded PDF
			doc.save(output_path + "find-replace-test-fielded.pdf", SDFDoc.SaveMode.LINEARIZED, null);

			// The extracted xlf can be translated in a system of your choice
			// In this sample a pre-prepared translated file is used - find-replace-test_(en_to_fr).xlf

			// Perform the translation using the pre-prepared translated xliff
			TransPDF.applyXLIFF(doc, input_path + "find-replace-test_(en_to_fr).xlf", options);

			// Save the translated PDF
			doc.save(output_path + "find-replace-test-fr.pdf", SDFDoc.SaveMode.LINEARIZED, null);
			doc.close();
		}
		catch (PDFNetException e)
		{
			e.printStackTrace();
			System.out.println(e);
		}

		PDFNet.terminate();
	}
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#import <OBJC/PDFNetOBJC.h>
#import <Foundation/Foundation.h>

// The following sample illustrates how to extract xlf from a PDF document for translation
// It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int ret = 0;
        [PTPDFNet Initialize: 0];

		NSString *inputPath = @"../../TestFiles/";
		NSString *outputPath = @"../../TestFiles/Output/";

        @try
        {
			// Open the PDF to translate
			PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: [inputPath stringByAppendingString:@"find-replace-test.pdf"]];

			PTTransPDFOptions* options = [[PTTransPDFOptions alloc] init];

			// Set the source language in the options
			[options SetSourceLanguage: @"en"];

			// Set the number of pages to process in each batch
			[options SetBatchSize: 20];

			// Optionally, subset the pages to process
			// This PDF only has a single page, but you can specify a subset of pages like this
			// [options SetPages: "-2,5-6,9,11-"];

			// Extract the xlf to file and field the PDF for translation
			[PTTransPDF ExtractXLIFF: doc output_xliff: [outputPath stringByAppendingString:@"find-replace-test.xlf"] options: options];

			// Save the fielded PDF
			[doc SaveToFile : [outputPath stringByAppendingString:@"find-replace-test-fielded.pdf"] flags: e_ptlinearized];

			// The extracted xlf can be translated in a system of your choice
			// In this sample a pre-prepared translated file is used - find-replace-test_(en_to_fr).xlf

			// Perform the translation using the pre-prepared translated xliff
			[PTTransPDF ApplyXLIFF: doc incoming_xliff : [inputPath stringByAppendingString:@"find-replace-test_(en_to_fr).xlf"] options: options];

			// Save the translated PDF
			[doc SaveToFile : [outputPath stringByAppendingString:@"tagged-fr.pdf"] flags: e_ptlinearized];
			[doc Close];
        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }

        [PTPDFNet Terminate: 0];
        return ret;
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2025 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------



import sys
from apryse_sdk import *

import platform

sys.path.append("../../LicenseKey/PYTHON")
from LicenseKey import *

# ---------------------------------------------------------------------------------------
# The following sample illustrates how to extract xlf from a PDF document for translation.
# It then applies a pre-prepared translated xlf file to the PDF to produce a translated PDF.
# --------------------------------------------------------------------------------------

# Relative path to the folder containing the test files.
input_path = "../../TestFiles/"
output_path = "../../TestFiles/Output/"

def main():

    # The first step in every application using PDFNet is to initialize the
    # library and set the path to common PDF resources. The library is usually
    # initialized only once, but calling Initialize() multiple times is also fine.
    PDFNet.Initialize(LicenseKey)

    try:
        # Open a PDF document to translate
        doc = PDFDoc(input_path + "find-replace-test.pdf")
        options = TransPDFOptions()

        # Set the source language in the options
        options.SetSourceLanguage("en")

        # Set the number of pages to process in each batch
        options.SetBatchSize(20)

        # Optionally, subset the pages to process
        # This PDF only has a single page, but you can specify a subset of pages like this
        # options.SetPages("-2,5-6,9,11-")

        # Optionally, set the XLIFF exported version, default is 1.2
        # options.SetXLIFFVersion(TransPDFOptions.e_xliff_version_2)

        # Extract the xlf to file and field the PDF for translation
        TransPDF.ExtractXLIFF(doc, output_path + "find-replace-test.xlf", options)

        # Save the fielded PDF
        doc.Save(output_path + "find-replace-test-fielded.pdf", SDFDoc.e_linearized)

        # The extracted xlf can be translated in a system of your choice.
        # In this sample a pre-prepared translated file is used - find-replace-test_(en_to_fr).xlf

        # Perform the translation using the pre-prepared translated xliff
        TransPDF.ApplyXLIFF(doc, input_path + "find-replace-test_(en_to_fr).xlf", options)

        # Save the translated PDF
        doc.Save(output_path + "find-replace-test-fr.pdf", SDFDoc.e_linearized)
        doc.Close()
    except Exception as e:
        print("Unable to translate PDF document, error: " + str(e))

    PDFNet.Terminate()


if __name__ == '__main__':
    main()
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/get-started/samples/document-translation-reflow.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
