How to solve "No glyph for U+000A in font Helvetica-Bold" in pdfbox (android port)

10,407

Use the following code instead "\r\n"

   contentStream.moveTextPositionByAmount(100, 700);  
    contentStream.drawString("hello"); 
    contentStream.endText();  
    contentStream.beginText();  
    contentStream.moveTextPositionByAmount(100, 690);//the second parameter mast between 800.0 and 0.0

enter image description here

Share:
10,407

Related videos on Youtube

MD TAHMID HOSSAIN
Author by

MD TAHMID HOSSAIN

NullPointException

Updated on June 04, 2022

Comments

  • MD TAHMID HOSSAIN
    MD TAHMID HOSSAIN almost 2 years

    I want to write some text into a pdf. I have the data in "icrResultTxt". I am also writing the data to text.txt file before trying to write to a pdf. When I try to write to the pdf I get "No glyph for U+000A in font Helvetica-Bold". How to solve it? I dont have any fondness for "Helvetica-Bold". I am willing to change to any font.

      @Override
                    protected Boolean doInBackground(Void... params) {
                        Boolean ret = false;
                        PDDocument document = new PDDocument();
                        try {
                            float scale = 0.8f; // alter this value to set the image size
                            formPreference = getSharedPreferences(SHARD_PREFERENCES,
                                    MODE_PRIVATE);
    
                            PDPage page = new PDPage();
    
                            document.addPage(page);
    
    
                            PDPageContentStream contentStream = new PDPageContentStream(
                                    document, page, false, true);
    
                            PDFont pdfFont = PDType1Font.HELVETICA_BOLD;
                            float fontSize = 25;
                            float leading = 1.5f * fontSize;
    
                            PDRectangle mediabox = page.getMediaBox();
                            float margin = 72;
                            float width = mediabox.getWidth() - 2 * margin;
                            float startX = mediabox.getLowerLeftX() + margin;
                            float startY = mediabox.getUpperRightY() - margin;
                            // icrResultTxt;//
                            writeToFile(icrResultTxt);  
                            String text = icrResultTxt; // "I am trying to create a PDF file with a lot of text contents in the document. I am using PDFBox";
                            List<String> lines = new ArrayList<String>();
                            int lastSpace = -1;
                            while (text.length() > 0) {
                                int spaceIndex = text.indexOf(' ', lastSpace + 1);
                                if (spaceIndex < 0) {
                                    lines.add(text);
                                    text = "";
                                } else {
                                    String subString = text.substring(0, spaceIndex);
                                    float size = fontSize
                                            * pdfFont.getStringWidth(subString) / 1000;
                                    if (size > width) {
                                        if (lastSpace < 0) // So we have a word longer than
                                                            // the line... draw it anyways
                                            lastSpace = spaceIndex;
                                        subString = text.substring(0, lastSpace);
                                        lines.add(subString);
                                        text = text.substring(lastSpace).trim();
                                        lastSpace = -1;
                                    } else {
                                        lastSpace = spaceIndex;
                                    }
                                }
                            }
    
                            contentStream.beginText();
                            contentStream.setFont(pdfFont, fontSize);
                            contentStream.moveTextPositionByAmount(startX, startY);
                            for (String line : lines) {
                                contentStream.drawString(line);
                                contentStream.moveTextPositionByAmount(0, -leading);
                            }
                            contentStream.endText();
                            contentStream.close();
                            File fz = new File(
                                    Environment
                                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                                            + File.separator + "hello.pdf");
                            if (!fz.exists()) {
                                fz.createNewFile();
                            } else {
                                fz.delete();
                                fz.createNewFile();
                            }
                            document.save(fz);
    
                    } catch (Exception e) {
    
                        Log.v("mango", e.getMessage());
                    }
    private void writeToFile(String data) {
            try {
    
                File d = GetImageFile("test.txt");
                if (d.exists()) {
                    d.delete();
                    d.createNewFile();
                } else {
                    d.createNewFile();
                }
    
                FileOutputStream stream = new FileOutputStream(d);
                try {
                    stream.write(data.getBytes());
                } finally {
                    stream.close();
                }
    
            } catch (IOException e) {
                Log.e("Exception", "File write failed: " + e.toString());
            }
        }
    
    • Tilman Hausherr
      Tilman Hausherr almost 9 years
      Just don't use codes that are not supported in a font. U+000A is not a glyph, it is a linefeed. Remove it.
    • MD TAHMID HOSSAIN
      MD TAHMID HOSSAIN almost 9 years
      im getting the string from Abbyy Mobile Ocr SDK.. i cant remove anything :( , can you suggest me any other font that will not have problem with line feed .
    • Matter Cat
      Matter Cat almost 9 years
      Can you add custom fonts? google.com/get/noto covers most alphabets that you'd need.
    • Tilman Hausherr
      Tilman Hausherr almost 9 years
      They will all have trouble, because linefeeds are not glyphs. Linefeeds mean you should start a new line. What you should do is to fix the rest of the program, to split what you get and start a new line in the PDF. Or do this: line = line.replaceAll("\r\n","?") and you will get "?" instead of LF / CRs. This will of course look weird and won't make sense.
    • MD TAHMID HOSSAIN
      MD TAHMID HOSSAIN almost 9 years
      as prescribed i did: line=replaceAll("[\\t\\n\\r]","..."); but now getting "This font type only supports 8-bit code points". what to do bro ?
    • Tilman Hausherr
      Tilman Hausherr almost 9 years
      This is true; 16bit fonts are not supported in the 1.8 PDFBox versions. These are supported in the unreleased 2.0 version which has a different API, see pdfbox.apache.org/download.cgi#scm and pdfbox.apache.org/2.0/getting-started.html
    • MD TAHMID HOSSAIN
      MD TAHMID HOSSAIN almost 9 years
      i need a android port version !! is it available ? :( .if there any bypass to the problem?
    • Tilman Hausherr
      Tilman Hausherr almost 9 years
      Oops, sorry, I forgot that one. I don't know. Best would be to look at the issues there.