0

See the code for which I am getting this issue , My main concern is that after setting the font , I am getting the error in contentStream.showText() , so could anyone help me to set font for PDFBox which is supported in Ubuntu?

public class PDpage
{
        public static void main(String args[]) throws IOException
        {
            PDDocument searchableDocument = new PDDocument();
            String str2 = "/home/grid/Desktop/A.pdf";
            try 
            {
                String str = "/home/grid/Downloads/ubuntu-font-family-0.83/Ubuntu-C.ttf";
                String str1 = "/home/grid/Desktop/a.txt";
                File f = new File(str1);
                String s = FileUtils.readFileToString(f);
                PDPage page2 = new PDPage();
                searchableDocument.addPage(page2);
                PDPageContentStream contentStream = new PDPageContentStream(searchableDocument , page2);
                PDFont font = PDType0Font.load(searchableDocument, new FileInputStream(str));
                contentStream.beginText();
                contentStream.setFont(font, 12);
                contentStream.newLineAtOffset(20,750);
                contentStream.showText(s); 
                contentStream.endText();
                contentStream.close();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            searchableDocument.save(str2);
            searchableDocument.close();
        }
}

Code shows the following error while running :

Exception in thread "main" java.lang.IllegalArgumentException: No glyph for U+000A in font UbuntuCondensed-Regular
at org.apache.pdfbox.pdmodel.font.PDCIDFontType2.encode(PDCIDFontType2.java:404)
at org.apache.pdfbox.pdmodel.font.PDType0Font.encode(PDType0Font.java:342)
at org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:323)
at org.apache.pdfbox.pdmodel.PDPageContentStream.showText(PDPageContentStream.java:418)
at com.purpose.all.Strip.PDpage.main(PDpage.java:45)

How this code is similar to the question Can't insert Tabs and Spaces into PDBox PDF document ?

Rahul Dubey
  • 13
  • 1
  • 6
  • 2
    `U+000A` is `LF`, line feed, AKA newline. There shouldn't need to be a glyph for it. – Christoffer Hammarström Feb 20 '18 at 13:21
  • I think this is your answer: https://stackoverflow.com/a/34554902/233014 `showText` can't handle control characters like newlines. – Christoffer Hammarström Feb 20 '18 at 13:24
  • @ChristofferHammarström I have tried this but still error exist – Rahul Dubey Feb 20 '18 at 13:29
  • What is the "this" that you tried exactly. Update your question with what you tried and why it didn't work. – Christoffer Hammarström Feb 20 '18 at 13:31
  • @ChristofferHammarström the font that is provided by PDType1Font is not supported by ubuntu , so I have used PDTrueTypeFont and included the Ubuntu-C.tff but still Glyph issue occurs – Rahul Dubey Feb 20 '18 at 13:34
  • 1
    Have you removed the newlines from your text? Btw to convert a multiline text to a PDF, the easiest is to use the TextToPDF command line utility. – Tilman Hausherr Feb 20 '18 at 14:04
  • @TilmanHausherr Can you provide me the example to use TextToPDF in code as I am not been able to perform multiline to PDF by using PDPageContentStream. – Rahul Dubey Feb 21 '18 at 10:18
  • download the pdfbox-app from the download page, then call `java -jar pdfbox-app-2.0.8.jar TextToPDF outputfile textfile`. The source code is available in the source code download. PDPageContentStream can do multiline, you need to do several `showText` calls and change the positions. – Tilman Hausherr Feb 21 '18 at 10:21
  • @TilmanHausherr I want to use TextToPDF in eclipse not by using command in the ubuntu . Is there any example related to TextToPDF by where I can get the idea how to use it in my code . I have seen the api of TextToPdf but any example would be more beneficial. – Rahul Dubey Feb 21 '18 at 10:24
  • Ah, you want to use it programmatically. `TextToPDF app = new TextToPDF(); PDDocument doc = new PDDocument(); app.createPDFFromText( doc, new FileReader(...)); doc.save(...); doc.close();` – Tilman Hausherr Feb 21 '18 at 10:28
  • @TilmanHausherr Thanks.. – Rahul Dubey Feb 21 '18 at 10:40
  • @TilmanHausherr Is TextToPdf suitable for multipages creation in a pdf , I am not getting desired result for multipages by using TextToPdf . Could you suggest me the font of PDFBox PDPageContentStream which is suitable for UBUNTU as for every ubuntu font family I am getting the error No glyph for U+000A in font Ubuntu-condensed? – Rahul Dubey Feb 22 '18 at 10:09
  • Re "I am not getting desired result for multipages by using TextToPdf" then you should create a new question. Re "the error No glyph for U+000A in font Ubuntu-condensed" it seems you still haven't understood that U+000A is a linefeed and linefeed is not a glyph ("glyph" is the visual representation of a character). This applies for every font! Pick up a newspaper and scissors. Can you cut out a "newline" glyph? No you can't. Split your lines (e.g. with String.split()) and output the lines separately. Or use itext. – Tilman Hausherr Feb 22 '18 at 10:43
  • I had a simliar problem and it was not related to the question marked as duplicate. Because in my Application the User can insert characters and then I have this tab or newline in my Database (which is needed in other output options). Therefore I need to replace the Newlines before or while outputting it. This answer helped me: [https://stackoverflow.com/a/46644571/1856258] – leole Aug 01 '19 at 10:29

0 Answers0