0

I currently using Apache POI to create a simple .docx file. The problem is that if a paragraph lands on a page break, I need move that whole paragraph to the next page. The only thing is that I'm not sure how to determine if a paragraph lands on a page break.

Therefore my question is:

Is there a way to determine if a paragraph lands on a page break in Apache POI?

For example:

This paragraph:

enter image description here

Will be recognized as on a page break and will be automatically changed to this:

enter image description here

Paul Warnick
  • 903
  • 2
  • 13
  • 26
  • You can determine the page break on a word document using `Apache POI`. Should that be ok for you. Than O can post an answer? – mustangDC Jun 15 '15 at 14:15
  • @mustangDC Sorry I'm slightly confused by your comment. How would I go about finding the paragraph with the page break in the document? – Paul Warnick Jun 15 '15 at 15:01
  • My previous comment meant that If u can find the page break then you can guide your text to the next page. That will me quite easy I guess. By the way please provide the code where you are trying to end the page. – mustangDC Jun 15 '15 at 15:03
  • Or the other way you can also put a page break before wherever you want. That way you can control you text – mustangDC Jun 15 '15 at 15:05
  • @mustangDC In response to your third comment. I understand that I can put a page break before what I want on a new page, the problem is that I need to determine when a paragraph is being split between pages. I'm going to post an example. – Paul Warnick Jun 15 '15 at 15:12
  • See for that you have to manually check every line or paragraph. Because I don't think POI has any method/component to bind a paragraph together – mustangDC Jun 15 '15 at 15:48
  • Well if I put a page break before the start of the paragraph it would move it all to the next page. So I wouldn't have to worry about actually binding the two separate parts together. I just need to determine which paragraphs are being split. How would I go about iterating through all the lines/paragraphs to check that? – Paul Warnick Jun 15 '15 at 15:53
  • In that case you can put a `Tab` or may be certain number of `spaces` before starting your paragraph. If you do so you can check for the `Tab` or `Space` every time POI is writing to your `.docx` and then put a page break to that – mustangDC Jun 15 '15 at 16:02
  • But would that not put every paragraph on a separate page? I need only the ones that land on page breaks to be moved down to the next. – Paul Warnick Jun 15 '15 at 16:08
  • 1
    Wait I'll posting in a code .. similar to what you . Let me try first – mustangDC Jun 15 '15 at 16:17
  • Go read this [post](http://stackoverflow.com/questions/7336340/how-to-print-excel-file-and-word-document-in-java) thoroughly. You will get a head start about all these things you are asking and also about **SO**, what this website is all about. How people can seek help here. Thanks!!! – mustangDC Jun 15 '15 at 17:58

1 Answers1

0

This is a sample code, may be not a 100%. Try once and let me know, if I can improve it according to your scenario then definitely will do.

Assuming that you are using an XWPFDocument document class

ExtendedProperties ep = document.getProperties().getExtendedProperties();
int numberOfLines = ep.getUnderlyingProperties().getLines());

The code mentioned above will give you a count of lines in the document, match it with the number of lines you are putting into the document.

For Example:-

  1. Suppose numberOfLines returns 50.

  2. Check the number of lines you are printing in the document using a counter, if you feel the paragraph has arrived and the line count is almost 40, put a break there.

    ExtendedProperties ep = document.getProperties().getExtendedProperties(); int numberOfLines = ep.getUnderlyingProperties().getLines()); XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); //create new run Page n run.setText("TITLE");
    //……………………………Your Logic here……………………………// run.addCarriageReturn(); //separate previous text from break run.addBreak(BreakType.PAGE); //Break the page run.addBreak(BreakType.WORD_WRAPPING); //cancels effect of page break WXPFRun run2 = paragraph.createRun(); //create a new run for pane n+1

Hope it helps to get close to what you need.

Thanks

mustangDC
  • 945
  • 1
  • 12
  • 33
  • Thank you, I'm going to try this out, but how would I go about checking the number of lines I'm printing using a counter? – Paul Warnick Jun 15 '15 at 17:21
  • 1
    How are you printing the text in the document. Please provide that code (the code you were working with previously) – mustangDC Jun 15 '15 at 17:23
  • Sorry I can't really provide any relevant code because I need my question answered before I can start actually coding what we're discussing. But essentially I'm just printing to the text document but changing the text of some runs. For example: run.setText("Title"), like you have above. – Paul Warnick Jun 15 '15 at 17:32
  • I hope you are accustomed with the similar stuff. What we are talking about, the code specially. So, try once and let me know – mustangDC Jun 15 '15 at 17:35
  • Well the thing is, from above how would I go about checking the number of lines I'm printing? If I'm able to keep a count then I think I can solve my problem. But I'm not sure how to actually check the my line count. – Paul Warnick Jun 15 '15 at 17:42
  • Also the code to get the number of lines returns 0 every time. I don't really know why and I wasn't able to find any solutions online about counting the number of lines. – Paul Warnick Jun 15 '15 at 17:51