【问题标题】:Creates .pdf file with 0kb and no contents via iText通过 iText 创建 0kb 且无内容的 .pdf 文件
【发布时间】:2013-09-27 14:00:45
【问题描述】:

我希望下面的代码可以创建一个包含正确元数据的空白 PDF。相反,我最终得到了一个 0kb 的 pdf 文件,当然 Acrobat 不会打开。我浏览过http://www.avajava.com/tutorials/lessons/how-do-i-write-to-a-pdf-file-using-itext.htmlhttp://www.java4s.com/core-java/creating-pdf-with-java-and-itext-generating-pdf-using-java-example/

我似乎这样做是正确的......但......不是。

public class BuildSheet {
    JobSetEntity jobSetEntity;

    public BuildSheet(JobSetEntity jobSetEntity) {
        this.jobSetEntity = jobSetEntity;
    }

    public boolean generate(File destinationFile) {
        try {
            Document document = new Document();
            FileOutputStream stream = new FileOutputStream(destinationFile);
            PdfWriter.getInstance(document, stream);
            document.open();
            addMetaData(document);
            document.close();
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    private void addMetaData(Document document) {
        document.addAuthor(ApplicationContextProvider.getProperty("application.name"));
        document.addCreator(ApplicationContextProvider.getProperty("application.name"));
        document.addTitle("Build sheet for JobSet #"+jobSetEntity.getId());
        document.addLanguage("EN");
    }
}

【问题讨论】:

    标签: java itext


    【解决方案1】:

    您的代码似乎没有任何问题。您只需要在您的 pdf 中使用一些页面、内容,然后再使用 Adob​​e Reader 打开它

    我也尝试使用此代码并从 http://www.vogella.com/articles/JavaPDF/article.html 为我工作

        public boolean generate(File destinationFile) {
                try {
                    Document document = new Document();
                    FileOutputStream stream = new FileOutputStream(destinationFile);
                    PdfWriter.getInstance(document, stream);
                    document.open();
                    addMetaData(document);
    
                    addTitlePage(document);
                    document.close();
                    stream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
    
                return true;
            }
    
    
      private  Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
          Font.BOLD);
      private  Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
          Font.NORMAL, BaseColor.RED);
      private  Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
          Font.BOLD);
      private  Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
          Font.BOLD);
    
     private  void addTitlePage(Document document)
          throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 1);
        // Lets write a big header
        preface.add(new Paragraph("Title of the document", catFont));
    
        addEmptyLine(preface, 1);
        // Will create: Report generated by: _name, _date
        preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            smallBold));
        addEmptyLine(preface, 3);
        preface.add(new Paragraph("This document describes something which is very important ",
            smallBold));
    
        addEmptyLine(preface, 8);
    
        preface.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
            redFont));
    
        document.add(preface);
        // Start a new page
        document.newPage();
      }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2013-01-13
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 2013-12-28
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多