2013-09-27 2 views
0

Я бы ожидал, что следующий код создаст пустой PDF с соответствующими метаданными. Вместо этого я получаю файл в формате 0kb, который, конечно же, Acrobat не откроется. Я просмотрел http://www.avajava.com/tutorials/lessons/how-do-i-write-to-a-pdf-file-using-itext.html и http://www.java4s.com/core-java/creating-pdf-with-java-and-itext-generating-pdf-using-java-example/.Создает .pdf-файл с 0kb и без содержимого через iText

Кажется, я делаю это правильно ... но ... нет.

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"); 
    } 
} 

ответ

0

Кажется, нет никаких проблем в вашем code.You просто нужна страница, содержание в вашем PDF, прежде чем открыть его с помощью программы Adobe 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(); 
    } 

Я надеюсь, что это поможет вам

Смежные вопросы