2017-03-24 1 views
1

J'ai des problèmes avec cette bibliothèque, aidez-moi s'il vous plaît, j'ai essayé avec roboto et arial ttf et ça ne marche pas, j'essaye d'écrire Sigma Σ symbole PDF et il se termine par une exeption, je dois savoir si je dois coder cela, et comment puis-je le faire, grâce à l'avancePDFbox Ce type de police prend uniquement en charge les points de code 8 bits

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

Je doute que cela fonctionne avec PDFBox pour android, qui est basé sur PDFBox 1.8. Vous pouvez essayer d'utiliser le PDType1Font.SYMBOL et vous encoder, le code octal pour sigma est hexadécimal 53 (voir la spécification pdf Annexe A). C'est à dire. utilisez cela, Tj et \ n comme votre commande brute. Les lignes 'robotoRegular.encode (" WinAnsiEncoding ");' ne sont pas pertinentes, vous n'en avez pas besoin. Sinon, créez votre fichier PDF sur le serveur et utilisez la version 2. *. –

+0

Il semble que PDFBox pour Android utilise des concepts 2.0. Voir https://github.com/TomRoush/PdfBox-Android/issues/100 en bas. Si cela fonctionne, veuillez répondre à la question vous-même. –

Répondre

1

Je l'ai testé, et en utilisant LiberationSans que votre police sera vous permet d'encoder le symbole sigma.

PDFont liberationSans = PDType0Font.load(document, getActivity().getAssets().open("com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf")); va charger la police, alors vous pouvez drawString comme d'habitude.

+0

Merci ça marche pour moi. –