本文整理匯總了Java中org.apache.pdfbox.pdmodel.common.PDRectangle類的典型用法代碼示例。如果您正苦於以下問題:Java PDRectangle類的具體用法?Java PDRectangle怎麽用?Java PDRectangle使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PDRectangle類屬於org.apache.pdfbox.pdmodel.common包,在下文中一共展示了PDRectangle類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createAlternateRowsDocument
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的package包/類
@Test
public void createAlternateRowsDocument() throws Exception {
final PDDocument document = new PDDocument();
final PDPage page = new PDPage(PDRectangle.A4);
page.setRotation(90);
document.addPage(page);
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
// TODO replace deprecated method call
contentStream.concatenate2CTM(0, 1, -1, 0, page.getMediaBox().getWidth(), 0);
final float startY = page.getMediaBox().getWidth() - 30;
(new TableDrawer(contentStream, createAndGetTableWithAlternatingColors(), 30, startY)).draw();
contentStream.close();
document.save("target/alternateRows.pdf");
document.close();
}
示例2: createRingManagerDocument
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的package包/類
@Test
public void createRingManagerDocument() throws Exception {
final PDDocument document = new PDDocument();
final PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
final float startY = page.getMediaBox().getHeight() - 150;
final int startX = 56;
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
Table table = getRingManagerTable();
(new TableDrawer(contentStream, table, startX, startY)).draw();
contentStream.setFont(PDType1Font.HELVETICA, 8.0f);
contentStream.beginText();
contentStream.newLineAtOffset(startX, startY - (table.getHeight() + 22));
contentStream.showText("Dieser Kampf muss der WB nicht entsprechen, da als Sparringskampf angesetzt.");
contentStream.endText();
contentStream.close();
document.save("target/ringmanager.pdf");
document.close();
}
示例3: testRenderSdnList
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/42032729/render-type3-font-character-as-image-using-pdfbox">
* Render Type3 font character as image using PDFBox
* </a>
* <br/>
* <a href="https://drive.google.com/file/d/0B0f6X4SAMh2KRDJTbm4tb3E1a1U/view">
* 4700198773.pdf
* </a>
* from
* <a href="http://stackoverflow.com/questions/37754112/extract-text-with-custom-font-result-non-readble">
* extract text with custom font result non readble
* </a>
* <p>
* This test shows how one can render individual Type 3 font glyphs as bitmaps.
* Unfortunately PDFBox out-of-the-box does not provide a class to render contents
* of arbitrary XObjects, merely for rendering pages; thus, we simply create a page
* with the glyph in question and render that page.
* </p>
* <p>
* As the OP did not provide a sample PDF, we simply use one from another
* stackoverflow question. There obviously might remain issues with the
* OP's files.
* </p>
*/
@Test
public void testRenderSdnList() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("sdnlist.pdf"))
{
PDDocument document = PDDocument.load(resource);
PDPage page = document.getPage(1);
PDResources pageResources = page.getResources();
COSName f1Name = COSName.getPDFName("R144");
PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name);
Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap();
COSDictionary charProcsDictionary = fontF1.getCharProcs();
for (COSName key : charProcsDictionary.keySet())
{
COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key);
PDType3CharProc charProc = new PDType3CharProc(fontF1, stream);
PDRectangle bbox = charProc.getGlyphBBox();
if (bbox == null)
bbox = charProc.getBBox();
Integer code = f1NameToCode.get(key.getName());
if (code != null)
{
PDDocument charDocument = new PDDocument();
PDPage charPage = new PDPage(bbox);
charDocument.addPage(charPage);
charPage.setResources(pageResources);
PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage);
charContentStream.beginText();
charContentStream.setFont(fontF1, bbox.getHeight());
charContentStream.getOutput().write(String.format("<%2X> Tj\n", code).getBytes());
charContentStream.endText();
charContentStream.close();
File result = new File(RESULT_FOLDER, String.format("sdnlist-%s-%s.png", key.getName(), code));
PDFRenderer renderer = new PDFRenderer(charDocument);
BufferedImage image = renderer.renderImageWithDPI(0, 96);
ImageIO.write(image, "PNG", result);
charDocument.save(new File(RESULT_FOLDER, String.format("sdnlist-%s-%s.pdf", key.getName(), code)));
charDocument.close();
}
}
}
}
示例4: generatePage
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的package包/類
private PDPage generatePage(String[] lines, int startAt, PDDocument document) throws IOException {
PDPage page = new PDPage(new PDRectangle(612f, 396f));
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Positions are measured from the bottom left corner of the page at 72 DPI
// Add report text to page
contentStream.beginText();
contentStream.moveTextPositionByAmount(36+xOffset, 360+yOffset);
contentStream.setFont(font, fontSize);
int lineNumber = startAt;
while (lineNumber < startAt + linesPerPage && lineNumber < lines.length) {
contentStream.drawString(lines[lineNumber]);
contentStream.moveTextPositionByAmount(0, (-1*fontSize));
lineNumber += 1;
}
contentStream.endText();
contentStream.close();
return page;
}
示例5: 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;
}
示例6: testRotateMoveBox
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的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"));
}
}
示例7: render
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的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;
}
示例8: testMultiPageJFreeChart
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的package包/類
@Test
public void testMultiPageJFreeChart() throws IOException {
File parentDir = new File("target/test/multipage");
// noinspection ResultOfMethodCallIgnored
parentDir.mkdirs();
File targetPDF = new File(parentDir, "multipage.pdf");
PDDocument document = new PDDocument();
for (int i = 0; i < 4; i++) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 800, 400);
drawOnGraphics(pdfBoxGraphics2D, i);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 30);
matrix.scale(0.7f, 1f);
contentStream.saveGraphicsState();
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.restoreGraphicsState();
contentStream.close();
}
document.save(targetPDF);
document.close();
}
示例9: 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);
}
示例10: testRender4700198773
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/42032729/render-type3-font-character-as-image-using-pdfbox">
* Render Type3 font character as image using PDFBox
* </a>
* <br/>
* <a href="https://drive.google.com/file/d/0B0f6X4SAMh2KRDJTbm4tb3E1a1U/view">
* 4700198773.pdf
* </a>
* from
* <a href="http://stackoverflow.com/questions/37754112/extract-text-with-custom-font-result-non-readble">
* extract text with custom font result non readble
* </a>
* <p>
* This test shows how one can render individual Type 3 font glyphs as bitmaps.
* Unfortunately PDFBox out-of-the-box does not provide a class to render contents
* of arbitrary XObjects, merely for rendering pages; thus, we simply create a page
* with the glyph in question and render that page.
* </p>
* <p>
* As the OP did not provide a sample PDF, we simply use one from another
* stackoverflow question. There obviously might remain issues with the
* OP's files.
* </p>
*/
@Test
public void testRender4700198773() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("4700198773.pdf"))
{
PDDocument document = PDDocument.load(resource);
PDPage page = document.getPage(0);
PDResources pageResources = page.getResources();
COSName f1Name = COSName.getPDFName("F1");
PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name);
Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap();
COSDictionary charProcsDictionary = fontF1.getCharProcs();
for (COSName key : charProcsDictionary.keySet())
{
COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key);
PDType3CharProc charProc = new PDType3CharProc(fontF1, stream);
PDRectangle bbox = charProc.getGlyphBBox();
if (bbox == null)
bbox = charProc.getBBox();
Integer code = f1NameToCode.get(key.getName());
if (code != null)
{
PDDocument charDocument = new PDDocument();
PDPage charPage = new PDPage(bbox);
charDocument.addPage(charPage);
charPage.setResources(pageResources);
PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage);
charContentStream.beginText();
charContentStream.setFont(fontF1, bbox.getHeight());
charContentStream.getOutput().write(String.format("<%2X> Tj\n", code).getBytes());
charContentStream.endText();
charContentStream.close();
File result = new File(RESULT_FOLDER, String.format("4700198773-%s-%s.png", key.getName(), code));
PDFRenderer renderer = new PDFRenderer(charDocument);
BufferedImage image = renderer.renderImageWithDPI(0, 96);
ImageIO.write(image, "PNG", result);
charDocument.save(new File(RESULT_FOLDER, String.format("4700198773-%s-%s.pdf", key.getName(), code)));
charDocument.close();
}
}
}
}
示例11: testRotateCenter
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的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 around the center of its crop box.
* </p>
*/
@Test
public void testRotateCenter() 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);
PDRectangle cropBox = page.getCropBox();
float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
cs.transform(Matrix.getTranslateInstance(tx, ty));
cs.transform(Matrix.getRotateInstance(Math.toRadians(45), 0, 0));
cs.transform(Matrix.getTranslateInstance(-tx, -ty));
cs.close();
document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-center.pdf"));
}
}
示例12: testRotateCenterScale
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的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 around the center of its crop box
* and then crop it to make all previously visible content fit.
* </p>
*/
@Test
public void testRotateCenterScale() 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);
PDRectangle cropBox = page.getCropBox();
float tx = (cropBox.getLowerLeftX() + cropBox.getUpperRightX()) / 2;
float ty = (cropBox.getLowerLeftY() + cropBox.getUpperRightY()) / 2;
Rectangle rectangle = cropBox.transform(matrix).getBounds();
float scale = Math.min(cropBox.getWidth() / (float)rectangle.getWidth(), cropBox.getHeight() / (float)rectangle.getHeight());
cs.transform(Matrix.getTranslateInstance(tx, ty));
cs.transform(matrix);
cs.transform(Matrix.getScaleInstance(scale, scale));
cs.transform(Matrix.getTranslateInstance(-tx, -ty));
cs.close();
document.save(new File(RESULT_FOLDER, "IRJET_Copy_Right_form-rotated-center-scale.pdf"));
}
}
示例13: processY
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的package包/類
private static float processY(int rotate, ImageAndResolution ires, BufferedImage visualImageSignature, PDPage pdPage, SignatureImageParameters signatureImageParameters) {
float y;
PDRectangle mediaBox = pdPage.getMediaBox();
switch (rotate) {
case ANGLE_90:
y = processYAngle90(mediaBox, ires, signatureImageParameters, visualImageSignature);
break;
case ANGLE_180:
y = processYAngle180(mediaBox, ires, signatureImageParameters, visualImageSignature);
break;
case ANGLE_270:
y = processYAngle270(mediaBox, ires, signatureImageParameters, visualImageSignature);
break;
case ANGLE_360:
y = processYAngle360(mediaBox, ires, signatureImageParameters, visualImageSignature);
break;
default:
throw new IllegalStateException(SUPPORTED_ANGLES_ERROR_MESSAGE);
}
return y;
}
示例14: addHyperlink
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的package包/類
private void addHyperlink(
final float x, final float y,
final String hyperlink,
final PDPage pdPage) throws IOException {
PDAnnotationLink txtLink = new PDAnnotationLink();
PDRectangle position = new PDRectangle();
PDBorderStyleDictionary underline = new PDBorderStyleDictionary();
underline.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
txtLink.setBorderStyle(underline);
position.setLowerLeftX(x);
position.setLowerLeftY(y);
position.setUpperRightX(X_MARGIN_LEFT + BOX_WIDTH);
position.setUpperRightY(y + TEXT_LINE_HEIGHT);
txtLink.setRectangle(position);
PDActionURI action = new PDActionURI();
action.setURI(hyperlink);
txtLink.setAction(action);
pdPage.getAnnotations().add(txtLink);
}
示例15: createSampleDocument
import org.apache.pdfbox.pdmodel.common.PDRectangle; //導入依賴的package包/類
@Test
public void createSampleDocument() throws Exception {
// Define the table structure first
TableBuilder tableBuilder = new TableBuilder()
.addColumnOfWidth(300)
.addColumnOfWidth(120)
.addColumnOfWidth(70)
.setFontSize(8)
.setFont(PDType1Font.HELVETICA);
// Header ...
tableBuilder.addRow(new RowBuilder()
.add(Cell.withText("This is right aligned without a border").setHorizontalAlignment(RIGHT))
.add(Cell.withText("And this is another cell"))
.add(Cell.withText("Sum").setBackgroundColor(Color.ORANGE))
.setBackgroundColor(Color.BLUE)
.build());
// ... and some cells
for (int i = 0; i < 10; i++) {
tableBuilder.addRow(new RowBuilder()
.add(Cell.withText(i).withAllBorders())
.add(Cell.withText(i * i).withAllBorders())
.add(Cell.withText(i + (i * i)).withAllBorders())
.setBackgroundColor(i % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE)
.build());
}
final PDDocument document = new PDDocument();
final PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
final PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Define the starting point
final float startY = page.getMediaBox().getHeight() - 50;
final int startX = 50;
// Draw!
(new TableDrawer(contentStream, tableBuilder.build(), startX, startY)).draw();
contentStream.close();
document.save("target/sampleWithColorsAndBorders.pdf");
document.close();
}