Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ public boolean newPage() {
if (isPageEmpty()) {
setNewPageSizeAndMargins();
resetText(true);
initPage();
return false;
}
if (!open || close) {
Expand Down
39 changes: 36 additions & 3 deletions openpdf/src/test/java/com/lowagie/text/pdf/PdfDocumentTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.lowagie.text.pdf;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

class PdfDocumentTest {

private static final String PARAGRAPH_TEXT_1 = "Text above table";

private static final String PARAGRAPH_TEXT_2 = "Text below table";

@TestFactory
Expand Down Expand Up @@ -49,12 +55,39 @@ List<DynamicTest> testCreateWithAllElementsInOneCell() {
DynamicTest.dynamicTest("element text should be '" + PARAGRAPH_TEXT_2 + "'",
() -> assertEquals(paragraph2.toString(),
getCellElements(result).get(3).getChunks().toString()
)));
)));
}

private List<Element> getCellElements(PdfPTable result) {
PdfPCell firstCell = result.getRows().get(0).getCells()[0];
return firstCell.getColumn().compositeElements;
}

@Test
void createPdfFileWithAutoPageBreak() throws Exception {
final var document = new Document(PageSize.A4);
final var writer = PdfWriter.getInstance(document, new ByteArrayOutputStream());
document.setHeader(new HeaderFooter(false, new Phrase("Header")));
document.setFooter(new HeaderFooter(false, new Phrase("Footer")));
document.open();

for (int i = 0; i < 50; i++) {
if (i == 37) {
document.newPage();
}
final var pdf = writer.getPdfDocument();
final var headerFielt = PdfDocument.class.getDeclaredField("text");
headerFielt.setAccessible(true);
final var text = (PdfContentByte) headerFielt.get(pdf);
assertTrue(String.valueOf(text.getInternalBuffer()).contains("Header"),
"Header not found: %d".formatted(i));
assertTrue(String.valueOf(text.getInternalBuffer()).contains("Footer"),
"Footer not found: %d".formatted(i));
document.add(new Paragraph(
"This is line " + i + " of a long text to force automatic page breaks.",
new Font(Font.HELVETICA, 12)
));
}
document.close();
}
}