To support Sinhala fonts in PDFs, you need a library that allows embedding custom fonts. Both **iText** and **Apache PDFBox** support this functionality, but you must explicitly load and embed the required Sinhala font (e.g., `FM-Abhaya`, `Iskoola Pota`, or any other Unicode-compliant font). Below are examples for both libraries.
---
### **1. Using iText with Sinhala Fonts**
iText allows embedding custom fonts, including Sinhala.
#### Maven Dependency:
```xml
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.2.5</version> <!-- Check for the latest version -->
</dependency>
```
#### Example Code:
```java
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.io.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFont;
public class SinhalaPdfGenerator {
public static void main(String[] args) {
String dest = "sinhala_output.pdf";
String fontPath = "path/to/IskoolaPota.ttf"; // Path to Sinhala font file
try {
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
// Load Sinhala font
PdfFont sinhalaFont = PdfFontFactory.createFont(fontPath, "Identity-H");
// Add Sinhala text
String sinhalaText = "සිංහල අකුරු PDF එකේ";
document.add(new Paragraph(sinhalaText).setFont(sinhalaFont).setFontSize(14));
document.close();
System.out.println("PDF with Sinhala text created: " + dest);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
---
### **2. Using Apache PDFBox with Sinhala Fonts**
Apache PDFBox also supports embedding Sinhala fonts for rendering custom text.
#### Maven Dependency:
```xml
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.29</version> <!-- Check for the latest version -->
</dependency>
```
#### Example Code:
```java
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import java.io.File;
public class SinhalaPdfBoxGenerator {
public static void main(String[] args) {
String outputFile = "sinhala_output.pdf";
String fontPath = "path/to/IskoolaPota.ttf"; // Path to Sinhala font file
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage();
document.addPage(page);
// Load Sinhala font
PDType0Font sinhalaFont = PDType0Font.load(document, new File(fontPath));
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
contentStream.setFont(sinhalaFont, 14);
contentStream.beginText();
contentStream.newLineAtOffset(100, 700);
contentStream.showText("සිංහල අකුරු PDF එකේ");
contentStream.endText();
}
document.save(outputFile);
System.out.println("PDF with Sinhala text created: " + outputFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
---
### **3. Notes on Sinhala Fonts**
1. **Use Unicode-Compliant Fonts**: Ensure the font supports Sinhala characters (e.g., `Iskoola Pota`, `FM-Abhaya`).
2. **Embed the Font**: Always embed the font using the provided methods to ensure compatibility across devices.
3. **Path to Font File**: Replace `"path/to/IskoolaPota.ttf"` with the actual path to your Sinhala font file.
### Comparison:
- **iText**: Offers better support for advanced PDF features, making it ideal for complex projects.
- **PDFBox**: Lightweight and simple, suitable for straightforward PDFs.
Let me know if you need further assistance!