继续下一页的文字

时间:2013-08-28 03:12:48

标签: java itext

我想使用iText生成一系列问题及其选项。我能够生成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();

在此之后我得到异常页面制动意味着有时候问题在页面末尾并且选项跳到下一页。(如下图所示)

enter image description here

2 个答案:

答案 0 :(得分:1)

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

这会将对象保留在一个页面中,如果内容不适合剩余页面,则会强制创建新页面。

答案 1 :(得分:1)

Alexis Pigeon的帮助下,我完成了这段代码。特别感谢他。

我已将问题文本添加到Paragraph,之后所有选项都保留在列表中。 在opList中添加了选项列表paragraph,此paragraph添加到ListItem和此ListItem 添加到主人list

这样解决两个页面上的问题就解决了,但是我没有得到问题编号..我已经将主列表设置为编号= true 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