Skip to content

Commit c68337c

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Add more clear exception for an attempt to place elements via Document on flushed pages
DEVSIX-2533 Autoported commit. Original commit hash: [e86e59f65]
1 parent 5d7bbf9 commit c68337c

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

itext.tests/itext.layout.tests/itext/layout/PositioningTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ source product.
4242
*/
4343
using System;
4444
using iText.IO.Image;
45+
using iText.Kernel;
4546
using iText.Kernel.Colors;
4647
using iText.Kernel.Geom;
4748
using iText.Kernel.Pdf;
@@ -313,6 +314,28 @@ public virtual void ShowTextAlignedTest03() {
313314
, "diff"));
314315
}
315316

317+
/// <exception cref="System.IO.IOException"/>
318+
[NUnit.Framework.Test]
319+
public virtual void ShowTextAlignedOnFlushedPageTest01() {
320+
NUnit.Framework.Assert.That(() => {
321+
String outFileName = destinationFolder + "showTextAlignedOnFlushedPageTest01.pdf";
322+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
323+
Document doc = new Document(pdfDoc);
324+
Paragraph p = new Paragraph();
325+
for (int i = 0; i < 1000; ++i) {
326+
p.Add("abcdefghijklkmnopqrstuvwxyz");
327+
}
328+
doc.Add(p);
329+
// First page will be flushed by now, because immediateFlush is set to false by default.
330+
int pageNumberToDrawTextOn = 1;
331+
doc.ShowTextAligned(new Paragraph("Hello Bruno on page 1!"), 36, 36, pageNumberToDrawTextOn, TextAlignment
332+
.LEFT, VerticalAlignment.TOP, 0);
333+
doc.Close();
334+
}
335+
, NUnit.Framework.Throws.InstanceOf<PdfException>().With.Message.EqualTo(PdfException.CannotDrawElementsOnAlreadyFlushedPages))
336+
;
337+
}
338+
316339
private void DrawCross(PdfCanvas canvas, float x, float y) {
317340
DrawLine(canvas, x - 50, y, x + 50, y);
318341
DrawLine(canvas, x, y - 50, x, y + 50);

itext/itext.kernel/itext/kernel/PdfException.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public class PdfException : Exception {
116116

117117
public const String CannotCreatePdfStreamByInputStreamWithoutPdfDocument = "Cannot create pdfstream by InputStream without PdfDocument.";
118118

119+
public const String CannotDrawElementsOnAlreadyFlushedPages = "Cannot draw elements on already flushed pages.";
120+
119121
public const String CannotGetContentBytes = "Cannot get content bytes.";
120122

121123
public const String CannotGetPdfStreamBytes = "Cannot get PdfStream bytes.";

itext/itext.layout/itext/layout/renderer/DocumentRenderer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ source product.
4343
*/
4444
using System;
4545
using System.Collections.Generic;
46+
using iText.Kernel;
4647
using iText.Kernel.Geom;
4748
using iText.Kernel.Pdf;
4849
using iText.Kernel.Pdf.Canvas;
@@ -124,6 +125,9 @@ protected internal override void FlushSingleRenderer(IRenderer resultRenderer) {
124125
PdfDocument pdfDocument = document.GetPdfDocument();
125126
EnsureDocumentHasNPages(pageNum, null);
126127
PdfPage correspondingPage = pdfDocument.GetPage(pageNum);
128+
if (correspondingPage.IsFlushed()) {
129+
throw new PdfException(PdfException.CannotDrawElementsOnAlreadyFlushedPages);
130+
}
127131
bool wrapOldContent = pdfDocument.GetReader() != null && pdfDocument.GetWriter() != null && correspondingPage
128132
.GetContentStreamCount() > 0 && correspondingPage.GetLastContentStream().GetLength() > 0 && !wrappedContentPage
129133
.Contains(pageNum) && pdfDocument.GetNumberOfPages() >= pageNum;

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
badd958c6688332e7e47b8f3d687e4a4377dfbc6
1+
e86e59f65f536fe9aba17be6711990dad3a0adf8

0 commit comments

Comments
 (0)