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