Skip to content

Commit e86e59f

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Add more clear exception for an attempt to place elements via Document on flushed pages
DEVSIX-2533
1 parent badd958 commit e86e59f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

kernel/src/main/java/com/itextpdf/kernel/PdfException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public class PdfException extends RuntimeException {
9090
public static final String CannotCreateLayoutImageByWmfImage = "Cannot create layout image by WmfImage instance. First convert the image into FormXObject and then use the corresponding layout image constructor.";
9191
public static final String CannotCreatePdfImageXObjectByWmfImage = "Cannot create PdfImageXObject instance by WmfImage. Use PdfFormXObject constructor instead.";
9292
public static final String CannotCreatePdfStreamByInputStreamWithoutPdfDocument = "Cannot create pdfstream by InputStream without PdfDocument.";
93+
public static final String CannotDrawElementsOnAlreadyFlushedPages = "Cannot draw elements on already flushed pages.";
9394
public static final String CannotGetContentBytes = "Cannot get content bytes.";
9495
public static final String CannotGetPdfStreamBytes = "Cannot get PdfStream bytes.";
9596
public static final String CannotOperateWithFlushedPdfStream = "Cannot operate with the flushed PdfStream.";

layout/src/main/java/com/itextpdf/layout/renderer/DocumentRenderer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343
*/
4444
package com.itextpdf.layout.renderer;
4545

46+
import com.itextpdf.kernel.PdfException;
4647
import com.itextpdf.kernel.geom.PageSize;
4748
import com.itextpdf.kernel.geom.Rectangle;
4849
import com.itextpdf.kernel.pdf.PdfDocument;
@@ -130,6 +131,9 @@ protected void flushSingleRenderer(IRenderer resultRenderer) {
130131
PdfDocument pdfDocument = document.getPdfDocument();
131132
ensureDocumentHasNPages(pageNum, null);
132133
PdfPage correspondingPage = pdfDocument.getPage(pageNum);
134+
if (correspondingPage.isFlushed()) {
135+
throw new PdfException(PdfException.CannotDrawElementsOnAlreadyFlushedPages);
136+
}
133137

134138
boolean wrapOldContent = pdfDocument.getReader() != null && pdfDocument.getWriter() != null &&
135139
correspondingPage.getContentStreamCount() > 0 && correspondingPage.getLastContentStream().getLength() > 0 &&

layout/src/test/java/com/itextpdf/layout/PositioningTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.io.LogMessageConstant;
4646
import com.itextpdf.io.image.ImageDataFactory;
47+
import com.itextpdf.io.util.MessageFormatUtil;
48+
import com.itextpdf.kernel.PdfException;
4749
import com.itextpdf.kernel.colors.ColorConstants;
4850
import com.itextpdf.kernel.colors.DeviceGray;
4951
import com.itextpdf.kernel.geom.PageSize;
@@ -70,8 +72,10 @@ This file is part of the iText (R) project.
7072
import com.itextpdf.test.annotations.type.IntegrationTest;
7173
import org.junit.Assert;
7274
import org.junit.BeforeClass;
75+
import org.junit.Rule;
7376
import org.junit.Test;
7477
import org.junit.experimental.categories.Category;
78+
import org.junit.rules.ExpectedException;
7579

7680
import java.io.IOException;
7781

@@ -86,6 +90,9 @@ public static void beforeClass() {
8690
createOrClearDestinationFolder(destinationFolder);
8791
}
8892

93+
@Rule
94+
public ExpectedException junitExpectedException = ExpectedException.none();
95+
8996
@Test
9097
public void relativePositioningTest01() throws IOException, InterruptedException {
9198
String outFileName = destinationFolder + "relativePositioningTest01.pdf";
@@ -359,6 +366,29 @@ public void showTextAlignedTest03() throws IOException, InterruptedException {
359366
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
360367
}
361368

369+
@Test
370+
public void showTextAlignedOnFlushedPageTest01() throws IOException {
371+
junitExpectedException.expect(PdfException.class);
372+
junitExpectedException.expectMessage(PdfException.CannotDrawElementsOnAlreadyFlushedPages);
373+
374+
String outFileName = destinationFolder + "showTextAlignedOnFlushedPageTest01.pdf";
375+
376+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
377+
Document doc = new Document(pdfDoc);
378+
379+
Paragraph p = new Paragraph();
380+
for (int i = 0; i < 1000; ++i) {
381+
p.add("abcdefghijklkmnopqrstuvwxyz");
382+
}
383+
384+
doc.add(p);
385+
// First page will be flushed by now, because immediateFlush is set to false by default.
386+
int pageNumberToDrawTextOn = 1;
387+
doc.showTextAligned(new Paragraph("Hello Bruno on page 1!"), 36, 36, pageNumberToDrawTextOn, TextAlignment.LEFT, VerticalAlignment.TOP, 0);
388+
389+
doc.close();
390+
}
391+
362392

363393
private void drawCross(PdfCanvas canvas, float x, float y) {
364394
drawLine(canvas, x - 50, y, x + 50, y);

0 commit comments

Comments
 (0)