2017-03-24 7 views
1

Ich habe einige Probleme mit dieser Bibliothek, bitte hilf mir, ich habe versucht mit Roboto und Arial TTF und es funktioniert nicht, ich versuche Sigma Σ zu schreiben Symbol in PDF und es endet mit einer Ausnahme, muß ich wissen, ob ich diese codieren muß, und wie kann ich es tun, Vielen dank im VorausPDFbox Diese Schriftart unterstützt nur 8-bit Codepunkte

public void drawTable(PDPage page, PDPageContentStream contentStream, 
         float y, float margin, 
         String[][] content) throws IOException { 
    final int rows = content.length; 
    final int cols = content[0].length; 
    final float rowHeight = 20f; 
    final float tableWidth = page.getBBox().getWidth()-(2*margin); 
    final float tableHeight = rowHeight * rows; 
    final float colWidth = tableWidth/(float)cols; 
    final float cellMargin=5f; 

    //draw the rows 
    float nexty = y ; 
    for (int i = 0; i <= rows; i++) { 
     contentStream.drawLine(margin,nexty,margin+tableWidth,nexty); 
     nexty-= rowHeight; 
    } 

    //draw the columns 
    float nextx = margin; 
    for (int i = 0; i <= cols; i++) { 
     contentStream.drawLine(nextx,y,nextx,y-tableHeight); 
     nextx += colWidth; 
    } 


    //now add the text 
    contentStream.setFont(PDType1Font.COURIER,12); 
    PDTrueTypeFont robotoRegular = PDTrueTypeFont.loadTTF(document, getActivity().getAssets().open("arial.ttf")); 
    robotoRegular.encode("WinAnsiEncoding"); 
    PDTrueTypeFont robotBold = PDTrueTypeFont.loadTTF(document, getActivity().getAssets().open("ariblk.ttf")); 
    robotBold.encode("WinAnsiEncoding"); 
    float textx = margin+cellMargin; 
    float texty = y-15; 
    for(int i = 0; i < content.length; i++){ 
     for(int j = 0 ; j < content[i].length; j++){ 
      String text = content[i][j]; 
      contentStream.beginText(); 
      if (j==0){ 
       contentStream.setFont(robotBold,12); 
      }else { 
       contentStream.setFont(robotoRegular,12); 
      } 
//    byte[] commands = "Σ".getBytes(); 
//    commands[1] = (byte) 128; 
//    contentStream.appendRawCommands(commands); 
      contentStream.moveTextPositionByAmount(textx,texty); 
      contentStream.drawString(text); 
      contentStream.endText(); 
      textx += colWidth; 
     } 
     texty-=rowHeight; 
     textx = margin+cellMargin; 
    } 
} 
+1

Ich bezweifle, dass dies mit PDFBox für Android, die auf PDFBox 1.8 basiert, funktioniert. Sie könnten versuchen, das PDType1Font.SYMBOL zu verwenden, und codieren Sie sich, der oktale Code für Sigma ist Hex 53 (siehe pdf-Spezifikation Anhang A). I.e. benutze das, Tj und \ n als deinen rohen Befehl. Die 'robotoRegular.encode (" WinAnsiEncoding ");' Zeilen sind irrelevant, Sie brauchen sie nicht. Alternativ erstellen Sie Ihre PDF auf dem Server und verwenden Sie 2. * Version. –

+0

Scheint, dass PDFBox für Android einige 2.0 Konzepte verwendet. Siehe https://github.com/TomRoush/PdfBox-Android/issues/100 am unteren Rand. Wenn es funktioniert, beantworten Sie die Frage bitte selbst. –

Antwort

1

ich getestet habe, und mit Liberationweise der Schrift wird erlauben Ihnen, das Sigma-Symbol zu kodieren.

PDFont liberationSans = PDType0Font.load(document, getActivity().getAssets().open("com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf")); wird die Schriftart geladen, dann können Sie Zeichenkette wie gewohnt zeichnen.

+0

Danke, es funktioniert für mich. –

Verwandte Themen