Skip to content

Commit

Permalink
add PdfPageEvent.onBeforePageStart
Browse files Browse the repository at this point in the history
  • Loading branch information
gered committed Mar 30, 2017
1 parent 418b34b commit 9c27450
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/java/cljpdf/text/pdf/PdfDocument.java
Expand Up @@ -1109,6 +1109,8 @@ public void resetFooter() {
* @throws DocumentException on error
*/
protected void initPage() throws DocumentException {
PdfPageEvent pageEvent = writer.getPageEvent();

// the pagenumber is incremented
pageN++;

Expand All @@ -1123,6 +1125,20 @@ protected void initPage() throws DocumentException {
text.beginText();
textEmptySize = text.size();

// the whole point of the onBeforeStartPage event is to make it so that it's possible to react
// to each new page at a point where it's still possible to change the page size and margins
// (among other settings) and have it take effect for the very next page that is being started.
// onStartPage and onEndPage are too late for this (though onEndPage would be the next best spot
// for this kind of code).
// however, in order to not break compatibility with existing code, i've opted to not move when
// onOpenDocument is triggered. as a result it's a little bit weird in that onBeforeStartPage
// will be triggered before onOpenDocument.
// (i personally do feel that onOpenDocument is being triggered too late... but whatever)
// - Gered King, March 2017
if (pageEvent != null) {
pageEvent.onBeforeStartPage(writer, this);
}

markPoint = 0;
setNewPageSizeAndMargins();
imageEnd = -1;
Expand Down Expand Up @@ -1162,7 +1178,6 @@ protected void initPage() throws DocumentException {
alignment = oldAlignment;
carriageReturn();

PdfPageEvent pageEvent = writer.getPageEvent();
if (pageEvent != null) {
if (firstPageEvent) {
pageEvent.onOpenDocument(writer, this);
Expand Down
13 changes: 13 additions & 0 deletions src/java/cljpdf/text/pdf/PdfPageEvent.java
Expand Up @@ -71,6 +71,19 @@ public interface PdfPageEvent {
*/
public void onOpenDocument(PdfWriter writer, Document document);


/**
* Called before a new page is about to be initialized.
* <P>
* This event allows for doing things such as resetting page margins
* or size, etc between pages, as it is too late to do certain adjustments
* in <CODE>onStartPage</CODE> and <CODE>onEndPage</CODE>.
*
* @param writer the <CODE>PdfWriter</CODE> for this document
* @param document the document
*/
public void onBeforeStartPage(PdfWriter writer, Document document);

/**
* Called when a page is initialized.
* <P>
Expand Down
13 changes: 13 additions & 0 deletions src/java/cljpdf/text/pdf/PdfPageEventHelper.java
Expand Up @@ -73,6 +73,19 @@ public class PdfPageEventHelper implements PdfPageEvent {
public void onOpenDocument(PdfWriter writer,Document document) {
}

/**
* Called before a new page is about to be initialized.
* <P>
* This event allows for doing things such as resetting page margins
* or size, etc between pages, as it is too late to do certain adjustments
* in <CODE>onStartPage</CODE> and <CODE>onEndPage</CODE>.
*
* @param writer the <CODE>PdfWriter</CODE> for this document
* @param document the document
*/
public void onBeforeStartPage(PdfWriter writer, Document document) {
}

/**
* Called when a page is initialized.
* <P>
Expand Down
18 changes: 18 additions & 0 deletions src/java/cljpdf/text/pdf/events/PdfPageEventForwarder.java
Expand Up @@ -94,6 +94,24 @@ public void onOpenDocument(PdfWriter writer, Document document) {
}
}

/**
* Called before a new page is about to be initialized.
* <P>
* This event allows for doing things such as resetting page margins
* or size, etc between pages, as it is too late to do certain adjustments
* in <CODE>onStartPage</CODE> and <CODE>onEndPage</CODE>.
*
* @param writer the <CODE>PdfWriter</CODE> for this document
* @param document the document
*/
public void onBeforeStartPage(PdfWriter writer, Document document) {
PdfPageEvent event;
for (Iterator i = events.iterator(); i.hasNext(); ) {
event = (PdfPageEvent)i.next();
event.onBeforeStartPage(writer, document);
}
}

/**
* Called when a page is initialized.
* <P>
Expand Down

0 comments on commit 9c27450

Please sign in to comment.