2016-09-08 80 views
-5

以下是我的Java代碼,用於創建PDF並打印PDF文檔中的內容。如下面的代碼所示,在打印的行之間有空行。如果在打印的語句後面有兩個以上的空白行,只需寫一行空白行並省略其他空白行以寫入PDF文檔。因此,如果有一兩條空白行可以按原樣打印,但如果不止一行則刪除所有多餘的空白行,並使用Java代碼添加一行空白行。我正在使用Java版本5.請建議。使用java代碼刪除空白行

public static void main(String args[]) { 
     Document document = null; 
     FileOutputStream fos = null; 

     try { 
      final String prefix = "test"; 
      final File temporaryPDF = File.createTempFile(prefix, ".pdf"); 
      document = new Document(PageSize.LETTER); 

      fos = new FileOutputStream(temporaryPDF); 

      PdfWriter.getInstance(document, fos); 
      document.open(); 

      Font font = new Font(Font.FontFamily.COURIER, 10); 

      List<String> lines = new ArrayList<String>(); 
      lines.add("This is first line."); 
      lines.add("This is second line."); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add("This is third printed line."); 
      lines.add(" "); 
      lines.add("This is fourth printed line."); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add("#ACC004342-123"); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add(" "); 
      lines.add("More information:"); 
      lines.add("This is fifth printed line."); 
      lines.add("#ACC004342-123"); 
      lines.add(""); 
      lines.add("This is Sixth printed line."); 
      lines.add("Some information goes here."); 

      for (final String line : lines) { 
       document.add(new Paragraph(12, line, font)); 
      } 

      document.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
} 

PS:爲了更加清楚,我再次提出這個帖子。謝謝。

+2

嗯,不要*插入*空行首先?要麼,或者這個問題還不是很清楚...... – ppeterka

+0

如果有兩個以上的空行,只顯示兩個空行..如果有的話插入兩個以上的空行。 @ppeterka – javaUser

+0

如果一行只包含whiltespace,你可以用[String#trim()](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim() )'string.trim()。length == 0' – Jhonny007

回答

0

這是一個最小的答案,應該可以解決您的問題。我確實將代碼減少到了必要的部分,因爲我需要自己測試它,並且我沒有訪問您使用的類。

在提問時,您應該儘可能將問題中的代碼設置爲minimal,以便重新創建。

public static void main(String args[]) { 
    List<String> lines = new ArrayList<String>(); 
    lines.add("This is first line."); 
    lines.add("This is second line."); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add("This is third printed line."); 
    lines.add(" "); 
    lines.add("This is fourth printed line."); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add("#ACC004342-123"); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add(" "); 
    lines.add("More information:"); 
    lines.add("This is fifth printed line."); 
    lines.add("#ACC004342-123"); 
    lines.add(""); 
    lines.add("This is Sixth printed line."); 
    lines.add("Some information goes here."); 

    //count empty lines 
    int emptyLineCounter = 0; 

    for (final String line : lines) { 
     //when empty line increment counter, else reset counter 
     if(line.trim().length() == 0) 
      emptyLineCounter++; 
     else 
      emptyLineCounter = 0; 

     //when more then 2 empty lines are encountered move ignore line 
     if(emptyLineCounter <= 2) 
      System.out.println(line); //add line to document 
    } 
} 

編輯:寫過其他錯誤的答案是不提供minimal, complete and verifiable代碼的一個直接結果。