简体   繁体   中英

How can I write Persian text to PDF programmatically?

I write a program that creates a PDF file to SD-Card. Then it writes some text to it. I want to write some Persian text to that but the text isn't shown in the PDF. Here is my android codes :

// Create a document and set it's properties
Document objDocument = new Document();

objDocument.setCreator("اپلیکیشن فرهنگیان");
objDocument.setAuthor("فرهنگیان");
objDocument.setTitle("تاریخچه اپلیکیشن فرهنگیان");

// Create a page to add to the document
Page objPage = new Page(PageSize.LETTER, PageOrientation.PORTRAIT, 54.0f);

printstr = "این یک متن تستی است ." ;
Label objLabel = new Label(printstr, 0, 0, 504, 100, 
    Font.getHelvetica(), 18, TextAlign.RIGHT);

// Add label to page
objPage.getElements().add(objLabel);

// Add page to document
objDocument.getPages().add(objPage);

try {
    // Outputs the document to file
    objDocument.draw(FILE);
    Toast.makeText(this, "فایل متنی در مسیر  :" + FILE + " ذخیره شد .",
            Toast.LENGTH_LONG).show();
} catch (Exception e) {
    Toast.makeText(this, "فایل ساخته نمیشود\n" + e.getMessage(), Toast.LENGTH_LONG).show();
}

What is problem ?

Can't see where you specify PDF format, try implementing to your code

// open a new document
PrintedPdfDocument document = new PrintedPdfDocument(context,
     printAttributes);

// start a page
Page page = document.startPage(0);

// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());

// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());

//close the document
document.close();

SRC: AndroidDevelopers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM