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();

在此之后我遇到异常的页面刹车意味着有时问题在页面末尾并且选项跳转到下一页。(如下图所示)

在此处输入图像描述

4

2 回答 2

1

Alexis Pigeon在我完成这段代码的帮助下。所以特别感谢他。

之后我添加了问题文本Paragraph,所有选项都保存在列表中。选项列表中opList添加paragraph,这个paragraph添加到一个ListItem和这个ListItem 添加到一个主list

这样解决了两页上的问题拆分,但我没有得到问题编号..我已经将主列表设置为 numbered=truecom.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();
    }   

在此处输入图像描述

于 2013-08-30T04:29:52.133 回答
1

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

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

于 2013-08-28T07:56:24.393 回答