2012-07-11 56 views
0

以下代碼是針對打印到PDF的我的Java2D應用程序的簡化情況。它一些如何不打印'Dotted I in turkish language.'使用PDFGrahics2DPDF文檔中未出現土耳其字符(PDFGraphics2D)

import com.lowagie.text.pdf.PdfWriter; 
import com.lowagie.text.pdf.PdfContentByte; 
import com.lowagie.text.pdf.PdfTemplate; 
import com.lowagie.text.pdf.DefaultFontMapper; 
import com.lowagie.text.pdf.BaseFont; 
import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Rectangle; 

import java.io.FileOutputStream; 
import java.io.File; 
import java.io.IOException; 
import java.awt.Graphics2D; 
import java.awt.Font; 

public class DottedIExampleWithGraphics2D 
{ 

    private static final float WIDTH = 900; 
    private static final float HEIGHT = 500; 
    private DefaultFontMapper mapper = new DefaultFontMapper(); 

    public void testWriteOfStringWithFont() throws IOException, DocumentException 
    { 

     File fontFile = new File("c:/windows/fonts/arialuni.ttf"); 
     BaseFont bf = BaseFont.createFont(fontFile.getAbsolutePath(), 
       BaseFont.IDENTITY_H, 
       BaseFont.NOT_EMBEDDED); 
     Font theFont = mapper.pdfToAwt(bf, 18); 
     Document document = new Document(new Rectangle(WIDTH, HEIGHT)); 

     File testFile = new File("learning_withfont.pdf"); 
     FileOutputStream fos = new FileOutputStream(testFile); 
     PdfWriter writer = PdfWriter.getInstance(document, fos); 
     document.open(); 

     PdfContentByte cb = writer.getDirectContent(); 
     PdfTemplate tp = cb.createTemplate(WIDTH, HEIGHT); 
     cb.addTemplate(tp, 0, 0); 
     Graphics2D graphics = tp.createGraphics(WIDTH, HEIGHT, mapper); 

     graphics.setFont(theFont); 
     graphics.drawString(" \u0130 , \u0131 ", 25, 50); 

     graphics.drawString(bf.getPostscriptFontName(), 25, 100); 

     // 
     graphics.dispose(); 
     document.close(); 

     System.out.println("testFile.getAbsolutePath() = " + testFile.getAbsolutePath()); 
    } 

    /** 
    */ 
    public static void main(String[] args) throws IOException, DocumentException 
    { 
     new DottedIExampleWithGraphics2D().testWriteOfStringWithFont(); 
    } 
} 

輸出是這樣的。

Dotted I missing in this

的問題是如何將此類字符打印到PDFGraphics2D。正常創建PDF的Java代碼正常工作。代碼在這裏供快速參考。

import com.lowagie.text.Chunk; 
import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Font; 
import com.lowagie.text.Paragraph; 
import com.lowagie.text.pdf.BaseFont; 
import com.lowagie.text.pdf.PdfWriter; 

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

public class DottedIExample 
{ 

    /** 
    * Creates a PDF document. write a string and font name 
    */ 
    public void createPdf(String filename) throws IOException, DocumentException 
    { 

     Document document = new Document(); 
     PdfWriter.getInstance(document, new FileOutputStream(filename)); 

     document.open(); 
     File fontFile = new File("c:/windows/fonts/arialuni.ttf"); 
     BaseFont bf = BaseFont.createFont(fontFile.getAbsolutePath(), 
       BaseFont.IDENTITY_H, 
       BaseFont.NOT_EMBEDDED); 
     Font font = new Font(bf, 12); 
     document.add(new Paragraph(bf.getPostscriptFontName(), font)); 
     document.add(new Paragraph("\u0131 , \u0130", font)); 
     document.add(Chunk.NEWLINE); 

     document.close(); 
    } 

    /** 
    */ 
    public static void main(String[] args) throws IOException, DocumentException 
    { 
     File result = new File("dottedi.pdf"); 
     new DottedIExample().createPdf(result.getAbsolutePath()); 
     System.out.println("result at = " + result.getAbsolutePath()); 
    } 
} 

回答

0

問題在於mapper。它的awtToPdf()/pdf2awt()應該設置爲返回正確的字體。

固定碼,僅供參考..(映射器的實際的設置是更復雜)

import com.lowagie.text.pdf.PdfWriter; 
import com.lowagie.text.pdf.PdfContentByte; 
import com.lowagie.text.pdf.PdfTemplate; 
import com.lowagie.text.pdf.DefaultFontMapper; 
import com.lowagie.text.pdf.BaseFont; 
import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Rectangle; 

import java.awt.*; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.File; 
import java.io.IOException; 

public class DottedIExampleWithGraphics2D { 

    private static final File aFontFile = new File(("c:/windows/fonts/arialuni.ttf")); 
    private static final float WIDTH = 90; 
    private static final float HEIGHT = 50; 
    private DefaultFontMapper mapper = new DefaultFontMapper() { 
     @Override 
     public BaseFont awtToPdf(Font font) { 
      try { 
       return BaseFont.createFont(aFontFile.getAbsolutePath(), 
         BaseFont.IDENTITY_H, 
         BaseFont.NOT_EMBEDDED); 
      } catch (DocumentException e) { 
       e.printStackTrace(); //blah 
      } catch (IOException e) { 
       e.printStackTrace(); //blah 
      } 
      return null; 
     } 

     @Override 
     public Font pdfToAwt(BaseFont baseFont, int i) { 
      try { 
       return Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(aFontFile)); 
      } catch (FontFormatException e) { 
       e.printStackTrace(); //blah 
      } catch (IOException e) { 
       e.printStackTrace(); //blah 
      } 
      return null; 
     } 
    }; 


    public void testWriteOfStringWithFont() throws IOException, DocumentException { 


     Font theFont = mapper.pdfToAwt(null, 18); 
     Document document = new Document(new Rectangle(WIDTH, HEIGHT)); 

     File testFile = new File("learning_withfont.pdf"); 
     FileOutputStream fos = new FileOutputStream(testFile); 
     PdfWriter writer = PdfWriter.getInstance(document, fos); 
     document.open(); 

     PdfContentByte cb = writer.getDirectContent(); 
     PdfTemplate tp = cb.createTemplate(WIDTH, HEIGHT); 
     cb.addTemplate(tp, 0, 0); 
     Graphics2D graphics = tp.createGraphics(WIDTH, HEIGHT, mapper); 

     graphics.setColor(Color.blue); 
     graphics.setFont(theFont); 
     graphics.drawString(" \u0130 , \u0131 , \u0049, \u0069", 10, 10); 

     graphics.drawString("some", 10, 30); 

     // 
     graphics.dispose(); 
     document.close(); 

     System.out.println("testFile.getAbsolutePath() = " + testFile.getAbsolutePath()); 
    } 

    /** 
    */ 
    public static void main(String[] args) throws IOException, DocumentException { 
     new DottedIExampleWithGraphics2D().testWriteOfStringWithFont(); 
    } 
}