Convert TXT file to PDF using iText (keep formatting)

19,750

You need to use a Monospaced Font e.g. Courier.

http://en.wikipedia.org/wiki/Monospaced_font

http://itextpdf.com/examples/iia.php?id=208

Share:
19,750
Marius Manastireanu
Author by

Marius Manastireanu

Updated on June 04, 2022

Comments

  • Marius Manastireanu
    Marius Manastireanu almost 2 years

    I am trying to convert a .txt file into a .pdf file using iText library. The problem that I am facing is the following:

    I have a clear formatting in the txt file, something similar with this:

    TEXT                                   *******************
    Other text here                        * SOME_CODE_HERE_ *
    Other text                             *******************
    

    And in the output the formatting is gone and looks like this:

    TEXT           ******************
    Other text here         * SOME_CODE_HERE_ *
    Other text          ******************
    

    The code looks like this:

    public static boolean convertTextToPDF(File file) throws Exception {
    
        BufferedReader br = null;
    
        try {
    
            Document pdfDoc = new Document(PageSize.A4);
            String output_file = file.getName().replace(".txt", ".pdf");
            System.out.println("## writing to: " + output_file);
            PdfWriter.getInstance(pdfDoc, new FileOutputStream(output_file)).setPdfVersion(PdfWriter.VERSION_1_7);;
    
            pdfDoc.open();
    
            Font myfont = new Font();
            myfont.setStyle(Font.NORMAL);
            myfont.setSize(11);
    
            pdfDoc.add(new Paragraph("\n"));
    
            if (file.exists()) {
    
                br = new BufferedReader(new FileReader(file));
                String strLine;
    
                while ((strLine = br.readLine()) != null) {
                    Paragraph para = new Paragraph(strLine + "\n", myfont);
                    para.setAlignment(Element.ALIGN_JUSTIFIED);
                    pdfDoc.add(para);
                }
            } else {
                System.out.println("no such file exists!");
                return false;
            }
            pdfDoc.close();
        }
    
        catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) 
                br.close();
        }
    
        return true;
    }
    

    I also tried to create a BaseFont with IDENTITY_H but it doesn't work. I guess it's about the encoding or something like that. What do you think? I run out of solutions...

    Thanks

    LE: As suggested by Alan, and by the tutorial from iText's page, I used this part in addition with my existing code and it works fine.

            BaseFont courier = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.EMBEDDED);
            Font myfont = new Font(courier);
    
  • Marius Manastireanu
    Marius Manastireanu over 9 years
    It works with BaseFont courier = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.EMBEDDED); myfont = new Font(courier), inspired by the second link you posted.. I tried before with IDENTITY_H encoding and didn't work.. Thanks!!
  • Jongware
    Jongware over 9 years
    @Marius: the encoding should not make a difference; it's just a map from byte values to character indexes. It has absolutely nothing to do with the width of these characters.
  • mkl
    mkl about 2 years
    The question is clearly about using iText for the task at hand. Thus, am I right to assume that PDFFlow is a mere wrapper around iText?
  • DotNet Developer
    DotNet Developer about 2 years
    No, PDFFlow is NOT a wrapper around iText. PDFFlow is a separate C# library written from scratch with a convenient API and its own automatic layouter, using well-known open-source low-level C library libharu at the rendering stage.
  • mkl
    mkl about 2 years
    "No, PDFFlow is NOT a wrapper around iText." - In that case your answer may be an answer to another question but not this one.
  • DotNet Developer
    DotNet Developer about 2 years
    Main goal is to get a solution. The correct solution for such formatting is word-like tabulation (IT specialists know this). If the library he tried cannot provide a solution, there are libraries who do.
  • mkl
    mkl about 2 years
    Please read the question again. The OP has a text file to convert into a PDF. And he wants the appearance to be like in the text file. Here one admittedly has to deduce that the OP means like in the text file as shown on the console or like in the text file displayed in a mono spaced font. In such a situation simply using a mono spaced font in the PDF, too, (as proposed by the accepted answer) is the easiest solution. Working with tabs is good for other use cases, yes, but not really for this one.
  • mkl
    mkl about 2 years
    "The correct solution for such formatting is word-like tabulation (IT specialists know this)" - first off all there more often than not is not the correct solution but multiple possible solutions with different advantages and disadvantages. And (IT specialists know this) is no argument at all, at best an attempt at condescension.