使用列表和特定字体时,iText 5.5.5及更早版本中出现意外结果

时间:2015-02-13 16:55:36

标签: java pdf itext

当我使用下面示例代码中的字体时,生成的pdf不会显示我预期的结果(请参阅源代码中的注释):

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfListTest {

    private File file;
    private Font font1;
    private Font font2;

    public PdfListTest() {
        file = new File("out.pdf");
        font1 = FontFactory.getFont(FontFactory.HELVETICA, BaseFont.WINANSI,
                BaseFont.NOT_EMBEDDED, 12, Font.UNDERLINE, BaseColor.GRAY, true);
        font2 = FontFactory.getFont(FontFactory.HELVETICA, BaseFont.WINANSI,
                BaseFont.NOT_EMBEDDED, 12, Font.STRIKETHRU, BaseColor.BLACK, true);
    }

    public void run() {
        try (
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos)
            )
        {
            Document document = new Document(PageSize.A4);
            PdfWriter.getInstance(document, bos);
            document.addTitle("itext list test");
            document.addCreator("test");
            document.addAuthor("test");
            document.addSubject("test");

            document.open();

            com.itextpdf.text.List list = new com.itextpdf.text.List(true, 36);

            // 1. single line list item => shows expected behavior
            list.add(new ListItem("This is a test string", font1));
            list.add(new ListItem("This is a second test string", font2));

            // 2. double line list item => differs from expected behavior
            list.add(new ListItem("This is a test string\nThis is a test string", font1));
            list.add(new ListItem("This is a second test string\nThis is a second test string", font2));

            // 3. long line list item which gets wrapped => shows expected behavior
            list.add(new ListItem("This is a test string. This is a test string. This is a test string. This is a test string. This is a test string.", font1));
            list.add(new ListItem("This is a second test string. This is a second test string. This is a second test string. This is a second test string.", font2));

            document.add(list);
            document.close();

        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        PdfListTest pdfTest = new PdfListTest();
        pdfTest.run();
    }

}

列表项3.和4.在列表项文本的最后一行中只有下划线或删除线。我期望与列表项目5和6相同的结果显示(两行加下划线或删除线)。

这是预期的行为还是错误?

0 个答案:

没有答案