簡體   English   中英

使用iText Java將文本文件數據轉換為PDF文件

[英]Text File Data into PDF File using iText Java

我已經成功創建了文本文件。 我需要將這些文本添加到PDF中。 就像我的文本文件中包含日常細節一樣。 月底,我需要生成包含所有數據的pdf文件。

我在這里所做的是

          try
              {
                 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("MonthlyReport.pdf"));
                 document.open();
                 document.add(new Paragraph("Month End Report."));
                 //Text file Data
                 try {
                        FileReader reader = new FileReader("MyFile.txt");
                        int character;

                        while ((character = reader.read()) != -1) {
                            document.add(new Paragraph((char) character));
                        }
                        reader.close();

                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }


                 //Text File Data


                 document.close();
                 writer.close();
              } catch (DocumentException exception)
              {
                 exception.printStackTrace();
              } catch (FileNotFoundException exception)
              {
                 exception.printStackTrace();
              }

但是問題是它僅創建了Month End Report部分。 我已經檢查過FileReader正常工作,並且工作正常。 我不明白為什么它不能與document.add

不知道是什么問題。 但是在我用BufferReader替換FileReader之后。 工作正常

try (BufferedReader br = new BufferedReader(new FileReader("MyFile.txt")))
                {

                    String sCurrentLine;

                    while ((sCurrentLine = br.readLine()) != null) {
                        document.add(new Paragraph(sCurrentLine));
                    }

                } catch (IOException exception) {
                    exception.printStackTrace();
                }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM