本文整理汇总了Java中org.apache.pdfbox.pdmodel.common.PDRectangle.getWidth方法的典型用法代码示例。如果您正苦于以下问题:Java PDRectangle.getWidth方法的具体用法?Java PDRectangle.getWidth怎么用?Java PDRectangle.getWidth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.common.PDRectangle
的用法示例。
在下文中一共展示了PDRectangle.getWidth方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addHeaderPJ
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
/**
* @param textHeader
* @param font
* @param PAGE_SIZE_A4
* @param contentStream
* @return ajoute un header a la piece
* @throws IOException
*/
private Float addHeaderPJ(final String textHeader, final PDFont font, final PDRectangle PAGE_SIZE_A4,
final PDPageContentStream contentStream) throws IOException {
Float marginTop = 0f;
// si font Ok, on ajoute le text
if (font != null && ConstanteUtils.DOSSIER_ADD_HEADER_IMG) {
// calcul de la largeur et hauteur du txt
Float titleWidth = font.getStringWidth(textHeader) / 1000 * ConstanteUtils.DOSSIER_FONT_SIZE;
Float titleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000
* ConstanteUtils.DOSSIER_FONT_SIZE;
// calcul de la marge du haut : hauteur du text + marge
marginTop = titleHeight + ConstanteUtils.DOSSIER_MARGIN;
// calcul de la position du text
Float xText = (PAGE_SIZE_A4.getWidth() - 2 * ConstanteUtils.DOSSIER_MARGIN - titleWidth) / 2;
Float yText = PAGE_SIZE_A4.getHeight() - marginTop;
// ecriture du text
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, ConstanteUtils.DOSSIER_FONT_SIZE);
contentStream.newLineAtOffset(xText, yText);
contentStream.showText(textHeader);
contentStream.endText();
}
return marginTop;
}
示例2: addWrappedParagraph
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的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);
}
示例3: createBookPage
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
/**
* Creates a pdf page from two pages from another 'original' pdf document
* @param doc original pdf from which the pages will be taken
* @param leftPage page number of the page to go on the left side
* @param rightPage page number of the page to go on the right side
* @return generated page containing the left and right pages from the original document side-by-side.
*/
private static PDPage createBookPage(PDDocument doc, int leftPage, int rightPage) {
// double the width of a normal page to create the booklet
PDRectangle baseSize = doc.getPage(0).getMediaBox();
PDRectangle box = new PDRectangle(baseSize.getWidth()*2, baseSize.getHeight());
if(sizeOverride != null) {
box = sizeOverride.asPDRectangle();
}
PDPage page = new PDPage(box);
try {
PDImageXObject leftImg = PrintDF.pageToImage(doc, leftPage, scale);
PDImageXObject rightImg = PrintDF.pageToImage(doc, rightPage, scale);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
if(leftImg != null)
contentStream.drawImage(leftImg, 0, 0, box.getWidth()/2, box.getHeight());
if(rightImg != null)
contentStream.drawImage(rightImg, box.getWidth()/2, 0, box.getWidth()/2, box.getHeight());
contentStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return page;
}
示例4: getCenteredTextXPos
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的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.common.PDRectangle; //导入方法依赖的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: findMaxFontSize
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
/**
* Calculates the maximum font size that can be used to fit in to a given bounding box.
* Assumes that landscape (wider than taller) boxes are using horizontal text and
* portrait (taller than wider) boxes are using text rotated 90 degrees
* @param font Font text will be drawn in
* @param text Lines of text to be drawn. Must contain at least one line
* @param boundingBox Bounds to fit text in
* @return font size
* @throws IOException Error working with font bubbled up from PDFBox
*/
static int findMaxFontSize(PDFont font, List<String> text, PDRectangle boundingBox) throws IOException {
if (text.size() == 0) {
throw new RuntimeException("findMaxFontSize called with empty text argument");
}
float maxTextWidth;
float maxTextHeight;
if (boundingBox.getWidth() > boundingBox.getHeight()) {
maxTextWidth = boundingBox.getWidth();
maxTextHeight = boundingBox.getHeight();
} else {
maxTextWidth = boundingBox.getHeight();
maxTextHeight = boundingBox.getWidth();
}
Float maxLineSize = Float.MAX_VALUE;
for (String line : text) {
float lineSize = findMaxLineSize(font, line, maxTextWidth, maxTextHeight);
if (lineSize < maxLineSize) {
maxLineSize = lineSize;
}
}
if (maxLineSize * text.size() > maxTextHeight ) {
maxLineSize = maxLineSize / text.size();
}
return maxLineSize.intValue();
}
示例7: toImages
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public List<BufferedImage> toImages(PDDocument pdDocument, int startPage, int endPage, int resolution, int imageType) throws Exception {
final List<BufferedImage> result = new ArrayList<BufferedImage>();
final List<PDPage> pages = pdDocument.getDocumentCatalog().getAllPages();
final int pagesSize = pages.size();
for (int i = startPage - 1; i < endPage && i < pagesSize; i++) {
PDPage page = pages.get(i);
PDRectangle cropBox = page.findCropBox();
float width = cropBox.getWidth();
float height = cropBox.getHeight();
int currResolution = calculateResolution(resolution, width, height);
BufferedImage image = page.convertToImage(imageType, currResolution);
if (image != null) {
result.add(image);
}
}
return result;
}
示例8: create
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
public void create(PDDocument document, PDFPage page) throws IOException {
PDPageContentStream cs = new PDPageContentStream(document, page, true, false);
PDRectangle pageSize = page.getBleedBox();
float pageWidth = pageSize.getWidth();
page.translateAndRotateIfNecessary(cs, pageWidth, 0);
cs.beginText();
PDFont font = PDType1Font.HELVETICA_BOLD;
cs.setFont(font, 20);
cs.moveTextPositionByAmount(page.getLeftX(), page.getTopY());
cs.drawString(title);
cs.endText();
cs.drawLine(page.getLeftX(), page.getTopY()-height, page.getRightX(), page.getTopY()-height);
cs.close();
}
示例9: drawStringRotatedWithResizing
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
/**
* Draws the given string, optionally supports scaling to fit.
* @param x Left side of text, or center point of text if centered (1/72 inch)
* @param y Bottom of text, in points (1/72 inch)
* @param text Text to draw
* @param optOrig Resize Options
* @throws IOException Error generating PDF
*/
void drawStringRotatedWithResizing(PDPageContentStream stream, float x, float y, int width, int height, String text, ResizeOptions optOrig) throws IOException {
stream.setNonStrokingColor(Color.gray);
PDRectangle boundingBox = new PDRectangle(x, y, width, height);
stream.fillRect(x, y, width, height);
ResizeOptions opt = new ResizeOptions(optOrig);
float textSize = opt.font.getStringWidth(text); // in thousandths of font pt size.
float size = opt.size;
float centeredXPosition = x + (boundingBox.getWidth()/2f);
float stringWidth = opt.font.getStringWidth( text );
float centeredYPosition = y + (boundingBox.getHeight() /2f);
stream.setNonStrokingColor(Color.GREEN);
stream.fillRect(centeredXPosition, centeredYPosition, 3, 3);
Matrix offset = Matrix.getRotateInstance(90 * Math.PI * 0.25,
centeredXPosition, centeredYPosition-(stringWidth/1000/2));
stream.beginText();
stream.setTextMatrix(offset);
stream.setStrokingColor(Color.black);
stream.setNonStrokingColor(Color.black);
stream.setFont(opt.font, size);
stream.showText(text);
stream.endText();
//
// // If text size is greater than maximum width, recalculate the correct font size, based on our restrictions
// if (textSize * (size/1000.0f) > opt.maxTextWidth) {
// size = opt.maxTextWidth * 1000.0f / textSize;
// if (size < opt.minFontSize) {
// // We have utterly failed to fit the text with the minimum font size,
// // So we're forced to use that.
// size = opt.minFontSize;
// }
// }
//
// if (opt.centered) {
// y -= textSize * (size/(2*1000.0f));
// }
////
//// Matrix offset = Matrix.getRotateInstance(i * Math.PI * 0.25,
//// centered XPosition, pageSize.getHeight() - centeredYPosition)
// Matrix offset = new Matrix();
// offset.rotate(Math.toRadians(5));
// offset.translate(100, 100);
// // Actually draw the text
}
示例10: processXAngle270
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
private static float processXAngle270(PDRectangle mediaBox, ImageAndResolution ires, SignatureImageParameters signatureImageParameters, BufferedImage visualImageSignature) {
float x;
SignatureImageParameters.VisualSignatureAlignmentVertical alignmentVertical = getVisualSignatureAlignmentVertical(signatureImageParameters);
switch (alignmentVertical) {
case TOP:
case NONE:
x = signatureImageParameters.getyAxis();
break;
case MIDDLE:
x = (mediaBox.getWidth() - ires.toXPoint(visualImageSignature.getWidth())) / 2;
break;
case BOTTON:
x = mediaBox.getWidth() - ires.toXPoint(visualImageSignature.getWidth()) - signatureImageParameters.getyAxis();
break;
default:
throw new IllegalStateException(SUPPORTED_VERTICAL_ALIGNMENT_ERROR_MESSAGE + alignmentVertical.name());
}
return x;
}
示例11: processXAngle360
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
private static float processXAngle360(PDRectangle mediaBox, ImageAndResolution ires, SignatureImageParameters signatureImageParameters, BufferedImage visualImageSignature) {
float x;
SignatureImageParameters.VisualSignatureAlignmentHorizontal alignmentHorizontal = getVisualSignatureAlignmentHorizontal(signatureImageParameters);
switch (alignmentHorizontal) {
case LEFT:
case NONE:
x = signatureImageParameters.getxAxis();
break;
case CENTER:
x = (mediaBox.getWidth() - ires.toXPoint(visualImageSignature.getWidth())) / 2;
break;
case RIGHT:
x = mediaBox.getWidth() -ires.toXPoint(visualImageSignature.getWidth()) - signatureImageParameters.getxAxis();
break;
default:
throw new IllegalStateException(SUPPORTED_HORIZONTAL_ALIGNMENT_ERROR_MESSAGE + alignmentHorizontal.name());
}
return x;
}
示例12: createPageElement
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
/**
* Creates an element that represents a single page.
* @return the resulting DOM element
*/
protected Element createPageElement()
{
String pstyle = "";
PDRectangle layout = getCurrentMediaBox();
if (layout != null)
{
/*System.out.println("x1 " + layout.getLowerLeftX());
System.out.println("y1 " + layout.getLowerLeftY());
System.out.println("x2 " + layout.getUpperRightX());
System.out.println("y2 " + layout.getUpperRightY());
System.out.println("rot " + pdpage.findRotation());*/
float w = layout.getWidth();
float h = layout.getHeight();
final int rot = pdpage.getRotation();
if (rot == 90 || rot == 270)
{
float x = w; w = h; h = x;
}
pstyle = "width:" + w + UNIT + ";" + "height:" + h + UNIT + ";";
pstyle += "overflow:hidden;";
}
else
log.warn("No media box found");
Element el = doc.createElement("div");
el.setAttribute("id", "page_" + (pagecnt++));
el.setAttribute("class", "page");
el.setAttribute("style", pstyle);
return el;
}
示例13: createPageStyle
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
/**
* Creates a style definition used for pages.
* @return The page style definition.
*/
protected NodeData createPageStyle()
{
NodeData ret = createBlockStyle();
TermFactory tf = CSSFactory.getTermFactory();
ret.push(createDeclaration("position", tf.createIdent("relative")));
ret.push(createDeclaration("border-width", tf.createLength(1f, Unit.px)));
ret.push(createDeclaration("border-style", tf.createIdent("solid")));
ret.push(createDeclaration("border-color", tf.createColor(0, 0, 255)));
ret.push(createDeclaration("margin", tf.createLength(0.5f, Unit.em)));
PDRectangle layout = getCurrentMediaBox();
if (layout != null)
{
float w = layout.getWidth();
float h = layout.getHeight();
final int rot = pdpage.getRotation();
if (rot == 90 || rot == 270)
{
float x = w; w = h; h = x;
}
ret.push(createDeclaration("width", tf.createLength(w, unit)));
ret.push(createDeclaration("height", tf.createLength(h, unit)));
}
else
log.warn("No media box found");
return ret;
}
示例14: startPage
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
@Override
protected void startPage(PDPage page) throws IOException {
super.startPage(page);
final PDRectangle rect = page.getCropBox();
xFactor = imageDimension.width / rect.getWidth();
yFactor = imageDimension.height / rect.getHeight();
}
示例15: startPage
import org.apache.pdfbox.pdmodel.common.PDRectangle; //导入方法依赖的package包/类
@Override
protected void startPage(PDPage page) throws IOException {
PDRectangle currentMediaBox = page.findMediaBox();
float mediaBoxWidth = currentMediaBox.getWidth();
float boxMean = mediaBoxWidth / 2;
minBoxMean = boxMean - getAverangeFontSize() * DELTA;
maxBoxMean = boxMean + getAverangeFontSize() * DELTA;
prevLineY = -1f;
pageImages = images.row(page);
}