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これ は マスター に追加されました。paragraphListItemListItemlist

このようにして、2 つのページに分割された質問は解決されますが、質問番号が表示されません。すでにマスター リストを numbered=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();
    }   

ここに画像の説明を入力

于 2013-08-30T04:29:52.133 に答える
1

あなたが興味を持っているのはsetKeepTogether(boolean)メソッドです:

これにより、オブジェクトが 1 つのページに保持され、コンテンツが残りのページに収まらない場合は、新しいページが強制的に作成されます。

于 2013-08-28T07:56:24.393 に答える