#427 API for getting the last y position and more#666
Conversation
Including the page positions of all layers. By popular demand!
|
I got a try against my test case (previously implemented for #427) and it seems to work well, thanks. Just a suggestion: won't it be smoother if |
As kindly suggested by @stechio. Also: + Make PagePosition immutable. + Rename getPagePositions to getAllLayerPagePositions in case we later want to add methods for boxes, etc.
|
That's super-handy, kudos! Recapping, now these are the available position-related methods of Some impressions:
|
|
Another suggestion related to package com.openhtmltopdf.pdfboxout;
import com.openhtmltopdf.layout.Layer;
import com.openhtmltopdf.render.Box;
public class PagePosition<T> {
. . .
private final T _element; // instead of 'private final Layer _layer';
. . .
public PagePosition(T element, String id, int pageNo, float x, float y, float width, float height) {
. . .
_element = element;
. . .
}
. . .
/**
* Element associated to this position.
*
* @return {@link Box} or {@link Layer}
*/
public T getElement() {
return _element;
}
}Consequently: public class PdfBoxRenderer implements Closeable, PageSupplier {
. . .
public List<PagePosition<Box>> findPagePositionsByID(Pattern pattern) {
. . .
}
public List<PagePosition<Layer>> getAllLayerPagePositions() {
. . .
}That solution could be enhanced subclassing public abstract class PagePosition<T> {
. . .
private final T _element; // instead of 'private final Layer _layer';
. . .
protected PagePosition(T element, String id, int pageNo, float x, float y, float width, float height) {
. . .
_element = element;
. . .
}
. . .
/**
* Element associated to this position.
*/
public T getElement() {
return _element;
}
}
public class LayerPosition extends PagePosition<Layer> {
public LayerPosition(Layer element, int pageNo, float x, float y, float width, float height) {
super(element, null, pageNo, x, y, width, height);
}
}
public class BoxPosition extends PagePosition<Box> {
public BoxPosition(Box element, int pageNo, float x, float y, float width, float height) {
super(element, element.getElement().getAttribute("id"), pageNo, x, y, width, height);
}
} |
|
It feels more solid now, thanks. About public class PdfBoxRenderer implements Closeable, PageSupplier {
. . .
// ** OLD method **
/**
* @deprecated Use {@link #getBoxPositionsByID(Pattern)} instead.
*/
@Deprecated
public List<PagePosition<Box>> findPagePositionsByID(Pattern pattern) {
return getBoxPositionsByID(pattern);
}
// ** NEW method **
public List<PagePosition<Box>> getBoxPositionsByID(Pattern pattern) {
/* NOTE: The same naming has to be mirrored to PdfBoxOutputDevice and its implementations. */
return _outputDevice.getBoxPositionsByID(newLayoutContext(), pattern);
}
. . .
public List<PagePosition<Layer>> getLayerPositions(Layer layer) {
. . .
}Extending further the filter analogy to the Layer-related methods, they could be considered just overloads of the same retrieval functionality (with Layer filter, gets the positions of that layer; without filters, gets the positions of all the layers): public class PdfBoxRenderer implements Closeable, PageSupplier {
. . .
/**
* Returns a list of page positions for all layers in the document.
* The page positions are sorted from first page to last and then top to bottom.
* The page position values are in bottom-up PDF units.
*
* <strong>WARNING:</strong> NOT transform aware. A transformed layer will return page
* positions that are not correct.
*/
// ** This is the previously-named getLayersPositions() **
public List<PagePosition<Layer>> getLayerPositions() {
return getLayerPositions(null);
}
/**
* Returns a list of page positions for the given layer (or all layers in the document if unspecified).
* The page positions are sorted from first page to last and then top to bottom.
* The page position values are in bottom-up PDF units.
*
* <strong>WARNING:</strong> NOT transform aware. A transformed layer will return page
* positions that are not correct.
*/
public List<PagePosition<Layer>> getLayerPositions(Layer layer) {
if (layer != null) {
// ** HERE the original getLayerPositions(Layer) implementation.
} else {
// ** HERE the original getLayersPositions() implementation.
}
}These would be the ultimately polished names: |
|
Hi @stechio, I have to do a long promised release and I think we are getting into diminishing returns, so I'm going to merge now. I'd like to give you a huge thanks for reviewing my code (and hopefully making me a slightly better coder). I hope you stay involved with this project, be it code review, finding issues and possibly PRs. Anyway, thank you! |
|
Sorry that I'm late in the party, but today I discovered the current implementation of #666 doesn't work with absolutely-positioned elements -- please take a look at my comment (it includes a fully-reproducible case). |
#427
Including the page positions of all layers. By popular demand! With test.
Adds the following on
PdfBoxRenderer:PagePositionis an object containing x, y, width, height, page number and in this caseLayerunder element.