Hello Aspose!
Could you please help me with finding out height of content on the page?
My task is add TextStamp on each stamp. In the same time stamp should not overlap content.
- First option to do it is calculate stamp size and shrink initial contents size via PdfFileEditor.resizeContents().
User of my system can set text any length, any font and any font size. As consequence height of stamp could be any. So I have to calculate stamp height before rendering new PDF. How to do it I don’t know.
textStamp.getHeight() / textStamp.getTextState().getTextHeight() will return font size of one line, independent of stamp's width, independent of text legth.
- Second option is to restrict user to use only one line. But font size anyway can be any.
So I have to get Content height to understand which top / bottom margins already exist on page and how many margin point I have to add.
page.getPageInfo().getMargin().getTop() - doesn’t help because it says margin is 70 while in real life it is 0.
Example of PDF in attachment.
Thank you in advance!
Best wishes,
Artem Fokin
SamplePDF.pdf (556.7 KB)
@artemrfokin
We tried to achieve the required values while using the below code snippet but did not get much success:
Document doc = new Document(dataDir + "SamplePDF.pdf");
Rectangle mediabox = doc.getPages().get_Item(1).getMediaBox();
Rectangle artbox = doc.getPages().get_Item(1).getArtBox();
System.out.println("Page 1 MediaBox, width/heigt=" + mediabox.getWidth() + "/" + mediabox.getHeight());
System.out.println("Page 1 ArtBox, width/heigt=" + artbox.getWidth() + "/" + artbox.getHeight());
mediabox = doc.getPages().get_Item(2).getMediaBox();
artbox = doc.getPages().get_Item(2).getArtBox();
System.out.println("Page 2 MediaBox, width/heigt=" + mediabox.getWidth() + "/" + mediabox.getHeight());
System.out.println("Page 2 ArtBox, width/heigt=" + artbox.getWidth() + "/" + artbox.getHeight());
We have logged an investigation ticket as PDFJAVA-40364 in our issue tracking system. We will further look into details of its and let you know once the ticket is resolved. Please be patient and spare us some time.
We are sorry for the inconvenience.
Thank you for quick response.
@artemrfokin
Unlike word processors (like MS Word), PDF is not a layout model — it’s a final-page description format.
This means:
- The PDF file does not store any concept of margins, padding, or flow.
- Content is absolutely positioned — every text fragment, image, or vector has fixed coordinates.
- The specification (ISO 32000) intentionally leaves “layout logic” out of scope.
So, there’s no built-in property like “page.getContentMargins()”, because the PDF model simply doesn’t know or care where “content starts or ends.”
These methods are used only for generation stage:
page.getCropBox();
page.getMediaBox();
page.getTrimBox();
and others, but not for already generated and loaded pdf.
We can determine actual visual content frames using conversion into image and check all visible pixels. Notice also, that invisible objects still can be out of this frame.
Example how to determine the content height and width for visible content:
Document pdf = new Document(dataDir+"source.pdf");
ByteArrayOutputStream output;
try {
Page page = pdf.getPages().get_Item(1);
Resolution resolution = new Resolution(150);
PngDevice device = new PngDevice(resolution);
output = new ByteArrayOutputStream();
device.process(page, output);
output.flush();
} finally {
pdf.close();
}
InputStream input = new ByteArrayInputStream(output.toByteArray());
BufferedImage image = ImageIO.read(input);
int width = image.getWidth();
int height = image.getHeight();
int minX = width, minY = height, maxX = 0, maxY = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = image.getRGB(x, y);
java.awt.Color color = new java.awt.Color(rgb, true);
if (color.getAlpha() > 0 && !(color.getRed() > 240 && color.getGreen() > 240 && color.getBlue() > 240)) {
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
}
}
int contentWidthPx = maxX - minX;
int contentHeightPx = maxY - minY;
double dpi = 150.0;
double pointsPerInch = 72.0;
double contentWidthPoints = contentWidthPx * pointsPerInch / dpi;
double contentHeightPoints = contentHeightPx * pointsPerInch / dpi;
System.out.printf("Detected visual content area: %.2f x %.2f points%n", contentWidthPoints, contentHeightPoints);