2013-08-28 51 views
1

我想使用iText生成問題及其選項的PDF。我能夠生成PDF,但問題有時會在頁面末尾打印出問題,並且選項會轉到下一頁。將文本保存到下一頁

如何確定問題及其選項不適合同一頁?

這意味着如果問題和選項不適合在同一頁面上,那麼它們必須放在下一頁上。

更新

com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4,50,50,15,15);   
ByteArrayOutputStream OutputStream = new ByteArrayOutputStream(); 
PdfWriter writer = PdfWriter.getInstance(document, OutputStream); 
document.open(); 
Paragraph paragraph = new Paragraph("Paper Name Here",new Font(FontFamily.TIMES_ROMAN,15,Font.BOLD)); 
paragraph.setAlignment(Element.ALIGN_CENTER); 
document.add(paragraph); 
document.addTitle("Paper Name Here");  
document.addAuthor("corp"); 
com.itextpdf.text.List list = new com.itextpdf.text.List(true); 

for (long i = 1; i <= 20 ; i++) 
{ 
    List<MultipleChoiceSingleCorrect> multipleChoiceSingleCorrects = new MultipleChoiceSingleCorrectServicesImpl().getItemDetailsByItemID(i); 
    for (MultipleChoiceSingleCorrect multipleChoiceSingleCorrect : multipleChoiceSingleCorrects) { 
     list.add(multipleChoiceSingleCorrect.getItemText());      
     RomanList oplist = new RomanList();    
     oplist.setIndentationLeft(20);      
     for (OptionSingleCorrect optionSingleCorrect : multipleChoiceSingleCorrect.getOptionList()) { 
      oplist.add(optionSingleCorrect.getOptionText()); 
     } 
     list.add(oplist); 
    } 
}   
document.add(list); 
document.close(); 

在此之後我m到處異常頁剎車裝置有時問題是在頁的端部和選項跳到下一頁。

enter image description here(如在下面的圖像示出)

回答

1

您感興趣的是setKeepTogether(boolean)方法:

這將保持在一個頁面中的對象,迫使新頁面的創建如果內容不會在剩餘的頁面適應。

+0

我提到[這個鏈接](http://stackoverflow.com/questions/17610483/itextsharp-next-line-in-paragraph-coming-right-below-serial-number)並創建了列表。第一個列表將持有問題,另一個列表將保留其選項。我更新了有問題的代碼。正如你所說的'setKeepTogether(boolean)'適用於第n段PdfPtable現在我該如何申請列表。 – Amogh

+0

將該列表添加到「段落」或「PdfPCell」(在「PdfPTable」內),就完成了! –

+0

'@Alexis Pigeon'我嘗試了很多向段落或pdf表格添加列表。在段落中添加列表的問題是,在列表中,我可能有多個問題及其選項,因此如果我在段落中添加所有列表,然後生成帶有一些空白頁面的PDF,並且如果我列出一個問題及其選項的列表,則問題編號當列表重置時,它始終爲「1」。你能幫助我嗎? – Amogh

1

藉助Alexis Pigeon我完成了這段代碼。特別感謝他。

我在Paragraph之後添加了問題文本,所有選項都保存在列表中。 選項列表opListparagraph中加入,此paragraph加上ListItemListItem 加入主人list

這兩個頁面的方式分割問題得到解決,但我很老問題編號..我已經設置主列表,編號=真com.itextpdf.text.List list = new com.itextpdf.text.List(true);

代碼: -

try { 
     String Filename="PaperName.pdf"; 
     com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4,50,50,15,15);   
     ByteArrayOutputStream OutputStream = new ByteArrayOutputStream(); 
     PdfWriter writer = PdfWriter.getInstance(document, OutputStream);   
     document.open(); 
     Paragraph paragraph = new Paragraph("Paper Name Here",new Font(FontFamily.TIMES_ROMAN,15,Font.BOLD)); 
     paragraph.setAlignment(Element.ALIGN_CENTER);   
     paragraph.setSpacingAfter(20);  
     document.add(paragraph); 
     document.addTitle("Paper Name Here");  
     document.addAuthor("crop"); 
     document.addCreator("crop"); 
     com.itextpdf.text.List list = new com.itextpdf.text.List(true); 
     for (long i = 1; i <= 20 ; i++) 
     { 
      List<MultipleChoiceSingleCorrect> multipleChoiceSingleCorrects = new MultipleChoiceSingleCorrectServicesImpl().getItemDetailsByItemID(i); 
      for (MultipleChoiceSingleCorrect multipleChoiceSingleCorrect : multipleChoiceSingleCorrects) { 
       Paragraph paragraph2 =new Paragraph(); 
       paragraph2.setKeepTogether(true); 
       paragraph2.add(multipleChoiceSingleCorrect.getItemText()); 
       paragraph2.add(Chunk.NEWLINE); 
       RomanList oplist = new RomanList();    
       oplist.setIndentationLeft(20);      
       for (OptionSingleCorrect optionSingleCorrect : multipleChoiceSingleCorrect.getOptionList()) {      
        oplist.add(optionSingleCorrect.getOptionText()); 
       } 
       paragraph2.add(oplist); 
       paragraph2.setSpacingBefore(20);      
       ListItem listItem =new ListItem(); 
       listItem.setKeepTogether(true); 
       listItem.add(paragraph2); 
       list.add(listItem); 
      } 
     } 
     document.add(list); 
     document.close(); 
     response.setContentLength(OutputStream.size()); 
     response.setContentType("application/pdf"); 
     response.setHeader("Content-disposition", "attachment; filename=" + Filename); 
     ServletOutputStream out = response.getOutputStream(); 
     OutputStream.writeTo(out); 
     out.flush(); 
     } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

enter image description here

+0

爲什麼你創建一個'ListItem'?爲什麼不直接使用'list.add(paragraph2)'? –

+0

@AlexisPigeon以前我也這麼做過。如果我直接使用list.add(paragraph2);然後生成pdf只有空白頁面沒有問題n選項得到渲染。作爲list.add()返回true,如果添加操作成功;否則爲false,因此我使用sysout(list.add(paragraph2))進行了檢查,它讓我錯誤。所以我只用ListItem嘗試,然後所有問題n選項打印只有列表號碼沒有得到打印。 – Amogh