|
1 | 1 | package com.lowagie.text.pdf; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
4 | 5 |
|
| 6 | +import com.lowagie.text.Document; |
5 | 7 | import com.lowagie.text.Element; |
| 8 | +import com.lowagie.text.Font; |
| 9 | +import com.lowagie.text.HeaderFooter; |
| 10 | +import com.lowagie.text.PageSize; |
6 | 11 | import com.lowagie.text.Paragraph; |
| 12 | +import com.lowagie.text.Phrase; |
| 13 | +import java.io.FileOutputStream; |
| 14 | +import java.nio.file.Path; |
7 | 15 | import java.util.Arrays; |
8 | 16 | import java.util.List; |
9 | 17 | import org.junit.jupiter.api.DynamicTest; |
| 18 | +import org.junit.jupiter.api.Test; |
10 | 19 | import org.junit.jupiter.api.TestFactory; |
11 | 20 |
|
12 | 21 | class PdfDocumentTest { |
@@ -57,4 +66,39 @@ private List<Element> getCellElements(PdfPTable result) { |
57 | 66 | return firstCell.getColumn().compositeElements; |
58 | 67 | } |
59 | 68 |
|
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + @Test |
| 73 | + void createPdfFileWithAutoPageBreak() throws Exception { |
| 74 | + Path output = Path.of("openpdf-test.pdf"); |
| 75 | + Document document = new Document(PageSize.A4); |
| 76 | + PdfWriter writer = PdfWriter.getInstance( |
| 77 | + document, |
| 78 | + new FileOutputStream(output.toFile()) |
| 79 | + ); |
| 80 | + document.setHeader(new HeaderFooter(false, new Phrase("Header"))); |
| 81 | + document.setFooter(new HeaderFooter(false, new Phrase("Footer"))); |
| 82 | + document.open(); |
| 83 | + Font font = new Font(Font.HELVETICA, 12); |
| 84 | + |
| 85 | + for (int i = 0; i < 50; i++) { |
| 86 | + if (i == 37) { |
| 87 | + document.newPage(); |
| 88 | + } |
| 89 | + var pdf = writer.getPdfDocument(); |
| 90 | + var headerFielt = PdfDocument.class.getDeclaredField("text"); |
| 91 | + headerFielt.setAccessible(true); |
| 92 | + var text = (PdfContentByte) headerFielt.get(pdf); |
| 93 | + assertTrue(String.valueOf(text.getInternalBuffer()).contains("Header"), |
| 94 | + "Header not found: %d".formatted(i)); |
| 95 | + assertTrue(String.valueOf(text.getInternalBuffer()).contains("Footer"), |
| 96 | + "Footer not found: %d".formatted(i)); |
| 97 | + document.add(new Paragraph( |
| 98 | + "This is line " + i + " of a long text to force automatic page breaks.", |
| 99 | + font |
| 100 | + )); |
| 101 | + } |
| 102 | + document.close(); |
| 103 | + } |
60 | 104 | } |
0 commit comments