2012-02-09 73 views
1

我正在評估iText作爲JavaGenerator for java swing應用程序。輸出應該在「Marathi」中,這是一種與印地語相似的印度當地語言,但不同。itext Marathi(印度)語言顯示問題

爲了進行評估,這是我試圖打印文本:

मराठीग्रीटींग्स,मराठीशुभेच्छापत्रे

這裏是源代碼:

package pdftest; 

import java.io.FileOutputStream; 

import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Font; 
import com.itextpdf.text.FontFactory; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.pdf.BaseFont; 
import com.itextpdf.text.pdf.PdfWriter; 


public class CPDFTest 
{ 
    private static String FILE = "c:/will/FirstPdf.pdf"; 

    public static void main(String[] args) 
     { 
     try 
       { 
      Document document = new Document(); 
      PdfWriter.getInstance(document, new FileOutputStream(FILE)); 
      document.open(); 
      addMetaData(document); 
      addTitlePage(document); 
      document.close(); 
     } 
       catch (Exception e) 
       { 
     } 
    } 

    private static void addMetaData(Document document) 
     { 
     document.addTitle("My first PDF"); 
    } 

    private static void addTitlePage(Document document) 
      throws DocumentException 
     { 

      Paragraph preface = new Paragraph(); 

      FontFactory.registerDirectory("C:\\WINDOWS\\Fonts"); 

      Font marFont = FontFactory.getFont("arial unicode ms",BaseFont.IDENTITY_H,true); 

      // Lets write a big header 
      preface.add(new Paragraph("मराठी ग्रीटींग्स, मराठी शुभेच्छापत्रे", marFont)); 

      document.add(preface); 
    } 
} 

請檢查下面的圖片對於錯誤的詳細信息:

enter image description here

我認爲這個問題可能與編碼或東西,但我無法弄清楚截至目前。任何幫助將不勝感激。

+0

怎麼樣,如果你在給定的[示例](HTTP將每個字符馬拉成等價的Unicode代碼點,如://itextpdf.c OM /示例/ iia.php?ID = 201)? – ecle 2012-02-09 12:51:31

回答

2

除非包含在最新的版本之一,iText的不支持梵文書寫系統。

在一些書寫系統中,有實際的字母和正確的字形之間沒有人對一關係,但字形形狀不同,取決於例如周圍的字形或它在單詞中的位置。爲了正確渲染文本,類型設置軟件需要實現這些規則和AFAIK,iText僅對阿拉伯語實現這些規則。

+0

感謝您的回覆jarnbjo。你知道支持Devanāgarī劇本的pdf創作者嗎? – Will 2012-02-09 13:34:06

0

以下爲我工作。

import java.awt.Graphics2D; 
import java.io.*; 
import com.lowagie.text.*; 

public class Test { 
    /** Path to the resulting PDF file. */ 
    public static final String RESULT 
    = "/home/test.pdf"; 
    /** 
    * Creates a PDF file: test.pdf 
    * @param args no arguments needed 
    */ 
    public static void main(String[] args) 
     throws DocumentException, IOException { 
     Document document = new Document();  
    PdfWriter writer =  
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));  
    document.open();  
    int w = 400; 
    int h = 150; 

    PdfContentByte cb = writer.getDirectContent(); 
    PdfTemplate tp = cb.createTemplate(w, h); 
    Graphics2D g2 = tp.createGraphicsShapes(w, h);   
    g2.drawString("मराठी ग्रीटींग्स, मराठी शुभेच्छापत्रे", 20, 100);     
    g2.dispose(); 
    cb.addTemplate(tp, 50, 400); 
    document.close();   

    } 
} 
0

作爲iText的是不支持方言,將文本轉換爲位圖,併爲轉換設定爲image.Use下面方法:

步驟1:

public Bitmap textAsBitmap(String text, float textSize, float stroke, int color) { 

    TextPaint paint = new TextPaint(); 
    paint.setTextSize(textSize); 
    paint.setAntiAlias(true); 
    // paint.setTextAlign(Paint.Align.LEFT); 

    paint.setColor(Color.BLACK); 
    // paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeJoin(Paint.Join.ROUND); 
    paint.setStrokeWidth(20f); 
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)); 
    float baseline = (int) (-paint.ascent() + 3f); // ascent() is negative 

    StaticLayout staticLayout = new StaticLayout(text, 0, text.length(), 
      paint, 435, android.text.Layout.Alignment.ALIGN_NORMAL, 1.0f, 
      1.0f, false); 


    Bitmap image = Bitmap.createBitmap(staticLayout.getWidth(), 
      staticLayout.getHeight(), Bitmap.Config.ARGB_8888); 

    Canvas canvas = new Canvas(image); 
    canvas.drawColor(Color.WHITE); 
    canvas.drawBitmap(image, 5, 5, null); 

    staticLayout.draw(canvas); 

    return image; 
} 

步驟2:

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
Bitmap bitmap = Bitmap.createBitmap(Utils.textAsBitmap(""+yourString,14,2,200)); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream); 
Image myImg = Image.getInstance(stream.toByteArray()); 
document.add(myImg);