您好我试图使用pdfbox
在pdf中显示阿拉伯文字,但遗憾的是我无法获得所需的正确输出。我正在逆向而不是从右向左阅读它从左到右阅读。任何人都可以帮助我如何使用pdfbox从右到左阅读,因为我是pdf框的新手。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import com.ibm.icu.text.Transliterator;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.FontSelector;
import com.itextpdf.text.pdf.PdfWriter;
public class Main {
static File arialFile = new File(Main.class.getResource("/KacstOffice.ttf").getFile());
public static void main(String[] args) throws IOException, DocumentException {
Properties langProperties = new Properties();
InputStream langSelection = Main.class.getResourceAsStream("/i18n/messages_ar.properties");
langProperties.load(langSelection);
File arialFile = new File(Main.class.getResource("/arial.ttf").getFile());
String ENGLISH_TO_ARBIC = "Latin-Arabic";
String englishString = (String) base64Decode();
String unicodeCodes = StringEscapeUtils.escapeJava(englishString);
System.out.println("Unicode codes:" + unicodeCodes);
Transliterator chineseToLatinTrans = Transliterator.getInstance(ENGLISH_TO_ARBIC);
String result1 = chineseToLatinTrans.transliterate(englishString);
System.out.println("Chinese to Latin:" + result1);
PDDocument document = new PDDocument();
PDPage page1 = new PDPage(PDRectangle.A4);
document.addPage(page1);
PDPageContentStream contentStream = new PDPageContentStream(document, page1,true, true);
PDFont font = PDType0Font.load(document, arialFile);
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(25, 500);
contentStream.drawString(langProperties.getProperty("pdf.GeneratedOn"));
contentStream.endText();
System.out.println("Content added")
contentStream.close();
document.save(new File("my_doc1.pdf"));
document.close();
}
public static Object base64Decode() throws IOException {
Properties langProperties = new Properties();
InputStream langSelection = Main.class.getResourceAsStream("/i18n/messages_ar.properties");
langProperties.load(langSelection);
Object str ="تاريخ الانتهاء";
return str;
}
}