本文整理汇总了Java中org.apache.pdfbox.pdmodel.PDPage.getMediaBox方法的典型用法代码示例。如果您正苦于以下问题:Java PDPage.getMediaBox方法的具体用法?Java PDPage.getMediaBox怎么用?Java PDPage.getMediaBox使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.PDPage
的用法示例。
在下文中一共展示了PDPage.getMediaBox方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRotateMoveBox
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/40611736/rotate-pdf-around-its-center-using-pdfbox-in-java">
* Rotate PDF around its center using PDFBox in java
* </a>
* <p>
* This test shows how to rotate the page content and then move its crop box and
* media box accordingly to make it appear as if the content was rotated around
* the center of the crop box.
* </p>
*/
@Test
public void testRotateMoveBox() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("IRJET_Copy_Right_form.pdf") )
{
PDDocument document = PDDocument.load(resource);
PDPage page = document.getDocumentCatalog().getPages().get(0);
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false, false);
Matrix matrix = Matrix.getRotateInstance(Math.toRadians(45), 0, 0);
cs.transform(matrix);
cs.close();
PDRectangle cropBox = page.getCropBox();
float cx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float cy = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
Point2D.Float newC = matrix.transformPoint(cx, cy);
float tx = (float)newC.getX() - cx;
float ty = (float)newC.getY() - cy;
page.setCropBox(new PDRectangle(cropBox.getLowerLeftX() + tx, cropBox.getLowerLeftY() + ty, cropBox.getWidth(), cropBox.getHeight()));
PDRectangle mediaBox = page.getMediaBox();
page.setMediaBox(new PDRectangle(mediaBox.getLowerLeftX() + tx, mediaBox.getLowerLeftY() + ty, mediaBox.getWidth(), mediaBox.getHeight()));
document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-move-box.pdf"));
}
}
示例2: render
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
/**
* Renders this table to a document
*
* @param document
* The document this table will be rendered to
* @param width
* The width of the table
* @param left
* The left edge of the table
* @param top
* The top edge of the table
* @param paddingTop
* The amount of free space at the top of a new page (if a page break is necessary)
* @param paddingBottom
* The minimal amount of free space at the bottom of the page before inserting a page break
* @return The bottom edge of the last rendered table part
* @throws IOException
* If writing to the document fails
*/
@SuppressWarnings("resource")
public float render(final PDDocument document, final float width, final float left, float top, final float paddingTop, final float paddingBottom)
throws IOException {
float yPos = top;
final PDPage page = document.getPage(document.getNumberOfPages() - 1);
final PDRectangle pageSize = page.getMediaBox();
PDPageContentStream stream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
float height = getHeight(width);
if (height > pageSize.getHeight() - paddingTop - paddingBottom) {
final float[] colWidths = getColumnWidths(width);
for (int i = 0; i < rows.size(); ++i) {
if (rows.get(i).getHeight(colWidths) > yPos - paddingBottom) {
drawBorder(stream, left, top, width, top - yPos);
stream = newPage(document, stream);
top = pageSize.getHeight() - paddingTop;
yPos = top;
yPos = renderRows(document, stream, 0, getNumHeaderRows(), width, left, yPos);
i = Math.max(i, getNumHeaderRows());
}
yPos = renderRows(document, stream, i, i + 1, width, left, yPos);
}
drawBorder(stream, left, top, width, top - yPos);
handleEvent(EventType.AFTER_TABLE, document, stream, left, top, width, top - yPos);
} else {
if (height > top - paddingBottom) {
stream = newPage(document, stream);
top = pageSize.getHeight() - paddingTop;
yPos = top;
}
yPos = renderRows(document, stream, 0, -1, width, left, yPos);
drawBorder(stream, left, top, width, top - yPos);
handleEvent(EventType.AFTER_TABLE, document, stream, left, top, width, top - yPos);
}
stream.close();
return yPos;
}
示例3: addWrappedParagraph
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
@Override
public void addWrappedParagraph(String text, PDFont font, float fontSize, Color textColor, TextAlignment align, float xCoordinate, float topYCoordinate, float width, PDPage page, PDPageContentStream contentStream) throws IOException {
Paragraph paragraph = new Paragraph(text, width, font, fontSize);
final float lineSpacing = 1.2f * fontSize;
PDRectangle region = page.getMediaBox();
contentStream.beginText();
contentStream.setFont(font, fontSize);
contentStream.setNonStrokingColor(textColor);
for (String line : paragraph.getLines()) {
if (align == TextAlignment.CENTER) {
float stringWidth = PdfBoxHandler.targetedStringWidth(line, font, fontSize);
float centerXPos = (region.getWidth() - stringWidth) / 2f;
contentStream.setTextTranslation(centerXPos, region.getHeight() - topYCoordinate);
} else {
contentStream.setTextTranslation(xCoordinate, region.getHeight() - topYCoordinate);
}
contentStream.showText(line);
topYCoordinate += lineSpacing;
}
contentStream.endText();
resetChangedColorToDefault(contentStream);
}
示例4: getCenteredTextXPos
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
private float getCenteredTextXPos(PDPage page, String text, PDFont font, int fontSize)
throws IOException {
float textWidth = getStringWidth(text, font, fontSize);
PDRectangle pageSize = page.getMediaBox();
float pageCenterX = pageSize.getWidth() / 2F;
float textX = pageCenterX - textWidth/2F;
return textX;
}
示例5: drawText
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
public void drawText(PDPage page, PDPageContentStream contentStream, String _text) throws IOException {
String text = _text;
// contentStream.beginText();
// contentStream.setFont(getDefaultFont(), 12);
// contentStream.newLine();
// contentStream.newLineAtOffset(100, 700);
// contentStream.showText(this.text);
// contentStream.endText();
float fontSize = 25;
float leading = 1.5f * fontSize;
PDRectangle mediabox = page.getMediaBox();
float margin = 72;
float width = mediabox.getWidth() - 2 * margin;
float startX = mediabox.getLowerLeftX() + margin;
float startY = mediabox.getUpperRightY() - margin;
int lastSpace = -1;
while (text.length() > 0) {
int spaceIndex = text.indexOf(' ', lastSpace + 1);//text.indexOf("\n", lastSpace + 1);
if (spaceIndex < 0) {
spaceIndex = text.length();
}
String subString = text.substring(0, spaceIndex);
float size = fontSize * getDefaultFont().getStringWidth(subString) / 1000;
LOGGER.debug("'{}' - {} of {}\n", subString, size, width);
if (size > width) {
if (lastSpace < 0)
lastSpace = spaceIndex;
subString = text.substring(0, lastSpace);
lines.add(subString);
text = text.substring(lastSpace).trim();
LOGGER.debug("'{}' is line\n", subString);
lastSpace = -1;
} else if (spaceIndex == text.length()) {
lines.add(text);
LOGGER.debug("'{}' is line\n", text);
text = "";
} else {
lastSpace = spaceIndex;
}
}
contentStream.beginText();
contentStream.setFont(getDefaultFont(), fontSize);
contentStream.newLineAtOffset(startX, startY);
for (String line : lines) {
contentStream.showText(line);
contentStream.newLineAtOffset(0, -leading);
}
contentStream.endText();
}
示例6: testPlaceByBoundingBox
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/47383506/pdfbox-obtain-bounding-box-of-rotated-image">
* PDFBox - obtain bounding box of rotated image
* </a>
* <p>
* This test demonstrates how to position images given their dimensions,
* rotation angle, and the coordinates of the lower left corner of their
* bounding box. The work horse is {@link #placeImage(PDDocument, PDPage,
* PDImageXObject, float, float, float, float, float)}.
* </p>
*/
@Test
public void testPlaceByBoundingBox() throws IOException {
try ( InputStream resource = getClass().getResourceAsStream("Willi-1.jpg");
PDDocument document = new PDDocument() ) {
PDPage page = new PDPage();
document.addPage(page);
PDRectangle mediaBox = page.getMediaBox();
float bbLowerLeftX = 50;
float bbLowerLeftY = 100;
try ( PDPageContentStream contentStream = new PDPageContentStream(document, page) ) {
contentStream.moveTo(bbLowerLeftX, mediaBox.getLowerLeftY());
contentStream.lineTo(bbLowerLeftX, mediaBox.getUpperRightY());
contentStream.moveTo(mediaBox.getLowerLeftX(), bbLowerLeftY);
contentStream.lineTo(mediaBox.getUpperRightX(), bbLowerLeftY);
contentStream.stroke();
}
PDImageXObject image = PDImageXObject.createFromByteArray(document, IOUtils.toByteArray(resource), "Image");
placeImage(document, page, image, bbLowerLeftX, bbLowerLeftY, image.getWidth(), image.getHeight(), (float)(Math.PI/4));
placeImage(document, page, image, bbLowerLeftX, bbLowerLeftY, .5f*image.getWidth(), .5f*image.getHeight(), 0);
placeImage(document, page, image, bbLowerLeftX, bbLowerLeftY, .25f*image.getWidth(), .25f*image.getHeight(), (float)(9*Math.PI/8));
document.save(new File(RESULT_FOLDER, "rotatedImagesByBoundingBox.pdf"));
}
}
示例7: writeString
import org.apache.pdfbox.pdmodel.PDPage; //导入方法依赖的package包/类
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
text = text.toLowerCase();
int index = text.indexOf(mTextToHighlight);
if (index != -1) {
PDPage currentPage = getCurrentPage();
PDRectangle pageBoundingBox = currentPage.getBBox();
AffineTransform flip = new AffineTransform();
flip.translate(0, pageBoundingBox.getHeight());
flip.scale(1, -1);
PDRectangle mediaBox = currentPage.getMediaBox();
float mediaHeight = mediaBox.getHeight();
float mediaWidth = mediaBox.getWidth();
int size = textPositions.size();
while (index != -1) {
int last = index + mTextToHighlight.length() - 1;
for (int i = index; i <= last; i++) {
TextPosition pos = textPositions.get(i);
PDFont font = pos.getFont();
BoundingBox bbox = font.getBoundingBox();
Rectangle2D.Float rect = new Rectangle2D.Float(0, bbox.getLowerLeftY(), font.getWidth(pos.getCharacterCodes()[0]), bbox.getHeight());
AffineTransform at = pos.getTextMatrix().createAffineTransform();
if (font instanceof PDType3Font) {
at.concatenate(font.getFontMatrix().createAffineTransform());
} else {
at.scale(1 / 1000f, 1 / 1000f);
}
Shape shape = flip.createTransformedShape(at.createTransformedShape(rect));
AffineTransform transform = mGC.getTransform();
int rotation = currentPage.getRotation();
if (rotation != 0) {
switch (rotation) {
case 90:
mGC.translate(mediaHeight, 0);
break;
case 270:
mGC.translate(0, mediaWidth);
break;
case 180:
mGC.translate(mediaWidth, mediaHeight);
break;
default:
break;
}
mGC.rotate(Math.toRadians(rotation));
}
mGC.fill(shape);
if (rotation != 0) {
mGC.setTransform(transform);
}
}
index = last < size - 1 ? text.indexOf(mTextToHighlight, last + 1) : -1;
}
}
}