Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue May 24, 2022 3:58 am

Dear E-Iceblue Support Team,

May I know there is any way to convert txt files to pdf files by using java?

elizabethC
 
Posts: 10
Joined: Thu Apr 28, 2022 3:15 am

Tue May 24, 2022 7:01 am

Hi,

Thank you for your inquiry.
Please refer to the code below to convert TXT to PDF.
Code: Select all
         //Get text from .txt file
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new FileReader(inputName));
        String content = null;
        while ((content = br.readLine()) != null) {
            sb.append(content);
        }
        String text = sb.toString();

        //Create a pdf document
        PdfDocument doc = new PdfDocument();
        PdfSection section = doc.getSections().add();
        PdfPageBase page = section.getPages().add();
        //Create a PdfFont
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Calibri", Font.PLAIN, 20), true);

        //Set string format
        PdfStringFormat format = new PdfStringFormat();
        format.setLineSpacing(20f);
        PdfBrush brush = PdfBrushes.getBlack();

        //Set text layout
        PdfTextLayout textLayout = new PdfTextLayout();
        textLayout.setBreak(PdfLayoutBreakType.Fit_Page);
        textLayout.setLayout(PdfLayoutType.Paginate);

        Rectangle2D.Float bounds = new Rectangle2D.Float();
        bounds.setRect(10, 20, page.getCanvas().getClientSize().getWidth(), page.getCanvas().getClientSize().getHeight());

        PdfTextWidget textWidget = new PdfTextWidget(text, font, brush);
        textWidget.setStringFormat(format);
        textWidget.draw(page, bounds, textLayout);

        //Save
        doc.saveToFile(outputName, FileFormat.PDF);
        doc.close();


Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Tue May 24, 2022 7:21 am

Hi Kylie,

Thanks for your reply.
May I know is possible for me to set the PDF output same formatting as the text file?
If yes, may I know how to set it?

elizabethC
 
Posts: 10
Joined: Thu Apr 28, 2022 3:15 am

Wed May 25, 2022 5:43 am

Hi,

Thank you for your reply.
Please add newline "\n" when reading the text. Sample code is shown as below. If there is still any question, please provide your text file as well as your expected output for a better reference.
Code: Select all
         while ((content = br.readLine()) != null) {
            sb.append(content+"\n");
        }


Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Mon Aug 08, 2022 7:06 am

Hi,

Greetings from E-iceblue!
Has your problem been effectively solved now? Could you give us some feedback at your convenience?

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Return to Spire.PDF