Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty pages being added with dynamic paragraphs. #1125

Open
DanielMcBride550 opened this issue Apr 1, 2024 · 6 comments
Open

Empty pages being added with dynamic paragraphs. #1125

DanielMcBride550 opened this issue Apr 1, 2024 · 6 comments
Labels

Comments

@DanielMcBride550
Copy link

DanielMcBride550 commented Apr 1, 2024

(v. 1.4.2)
I would like to be able to add a dynamic number of paragraph objects. After which, I would like to start the next section on a new page.

The issue is that sometimes there will be a blank page after my dynamic paragraphs and the next section. Just an entirely blank page with a page number on it.

I believe this happens because, in some cases, the paragraphs run RIGHT up to the end of the page and then the additional \n that gets added to Paragraphs ends up on the next page. I believe this is not being seen as "Empty" by the newPage() call. This is particularly visible with nested Paragraphs (which might be bad practice, but the issue still stands).

Is there a way I can check the contents that would be on the current page?

Is there something in the documentation that I missed that allows me to define the behavior of isPageEmpty()?

I also can't feasibly override this functionality because of the large amount of package-protected inner classes and amount of dependencies in the add method.

Does anyone have a solution to this issue?

@asturio
Copy link
Member

asturio commented Apr 2, 2024

Can you provide example code to reproduce the problem?

@DanielMcBride550
Copy link
Author

DanielMcBride550 commented Apr 10, 2024

Here is a chunk that recreates it:

public static void main(String... args) throws Exception
	{
		Document document = new Document();
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream);

		document.open();

		PdfContentByte cb = writer.getDirectContent();
		cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);

		int loops = 24;

		Paragraph outerParagraph = new Paragraph();
		for (int i = 0; i < loops; i++)
		{
			Paragraph paragraph = new Paragraph();
			paragraph.setFont(new Font(Font.HELVETICA, 8));
			paragraph.setSpacingBefore(2.5f);
			paragraph.setKeepTogether(true);
			
			StringReader strReader = new StringReader("arbitrary text");
			ArrayList<Element> htmlObjects = HTMLWorker.parseToList(strReader, null);

			for (Element htmlObject : htmlObjects)
			{
				paragraph.add(htmlObject);
			}
			outerParagraph.add(paragraph);			
		}
		document.add(outerParagraph);
		
		document.newPage();

		Paragraph outerParagraph2 = new Paragraph();
		for (int i = 0; i < loops; i++)
		{
			Paragraph paragraph = new Paragraph();
			paragraph.setFont(new Font(Font.HELVETICA, 8));
			paragraph.setSpacingBefore(2.5f);
			paragraph.setKeepTogether(true);
			
			StringReader strReader = new StringReader("arbitrary text");
			ArrayList<Element> htmlObjects = HTMLWorker.parseToList(strReader, null);

			for (Element htmlObject : htmlObjects)
			{
				paragraph.add(htmlObject);
			}
			outerParagraph2.add(paragraph);			
		}
		document.add(outerParagraph2);

		document.close();
		writer.close();

		FileOutputStream fos = new FileOutputStream("example.pdf");
		fos.write(byteArrayOutputStream.toByteArray());

		System.out.println("Complete");

	}

@DanielMcBride550
Copy link
Author

It seems to be related to the nested paragraphs sections, but I think this should work, yeah?

@asturio
Copy link
Member

asturio commented Apr 14, 2024

Having some code in the issues are always a good starting point for me or any other contributor. Maybe this could be avoided, if before creating a new page, we check if the content of the actual page is "empty". If the paragrapha text is rendered in the previous page and the new page only contains the "new line", than maybe the newPage() should do nothing?

@DanielMcBride550
Copy link
Author

I currently do that. I check to see if current page is empty and if it is, I skip the newpage call. I think something is causing it to not be considered empty.

@DanielMcBride550
Copy link
Author

Is there a known workaround for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants