本文整理汇总了Java中org.apache.pdfbox.text.TextPosition.getHeightDir方法的典型用法代码示例。如果您正苦于以下问题:Java TextPosition.getHeightDir方法的具体用法?Java TextPosition.getHeightDir怎么用?Java TextPosition.getHeightDir使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.text.TextPosition
的用法示例。
在下文中一共展示了TextPosition.getHeightDir方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCoverTextByRectanglesMwbI201711
import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/46080131/text-coordinates-when-stripping-from-pdfbox">
* Text coordinates when stripping from PDFBox
* </a>
* <br/>
* <a href="https://download-a.akamaihd.net/files/media_mwb/b7/mwb_I_201711.pdf">
* mwb_I_201711.pdf
* </a>
* <p>
* This test applies the OP's code to his example PDF file and indeed, there is an offset!
* This is due to the <code>LegacyPDFStreamEngine</code> method <code>showGlyph</code>
* which manipulates the text rendering matrix to make the lower left corner of the
* crop box the origin. In the current version of this test, that offset is corrected,
* see below.
* </p>
*/
@Test
public void testCoverTextByRectanglesMwbI201711() throws IOException {
try ( InputStream resource = getClass().getResourceAsStream("mwb_I_201711.pdf") ) {
PDDocument doc = PDDocument.load(resource);
myStripper stripper = new myStripper();
stripper.setStartPage(1); // fix it to first page just to test it
stripper.setEndPage(1);
stripper.getText(doc);
TextLine line = stripper.lines.get(1); // the line i want to paint on
float minx = -1;
float maxx = -1;
for (TextPosition pos: line.textPositions)
{
if (pos == null)
continue;
if (minx == -1 || pos.getTextMatrix().getTranslateX() < minx) {
minx = pos.getTextMatrix().getTranslateX();
}
if (maxx == -1 || pos.getTextMatrix().getTranslateX() > maxx) {
maxx = pos.getTextMatrix().getTranslateX();
}
}
TextPosition firstPosition = line.textPositions.get(0);
TextPosition lastPosition = line.textPositions.get(line.textPositions.size() - 1);
// corrected x and y
PDRectangle cropBox = doc.getPage(0).getCropBox();
float x = minx + cropBox.getLowerLeftX();
float y = firstPosition.getTextMatrix().getTranslateY() + cropBox.getLowerLeftY();
float w = (maxx - minx) + lastPosition.getWidth();
float h = lastPosition.getHeightDir();
PDPageContentStream contentStream = new PDPageContentStream(doc, doc.getPage(0), PDPageContentStream.AppendMode.APPEND, false, true);
contentStream.setNonStrokingColor(Color.RED);
contentStream.addRect(x, y, w, h);
contentStream.fill();
contentStream.close();
File fileout = new File(RESULT_FOLDER, "mwb_I_201711-withRectangles.pdf");
doc.save(fileout);
doc.close();
}
}
示例2: writeString
import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
@Override
protected void writeString(String string, List<TextPosition> textPositions) throws IOException
{
for (TextPosition textPosition: textPositions)
{
if (textPosition == null) {
continue;
}
String c = textPosition.getUnicode();
// if c not printable, return
if (!isPrintable(c)) {
continue;
}
Float h = textPosition.getHeightDir();
if (c.equals(NBSP)) { // replace non-breaking space for space
c = " ";
}
float wos = textPosition.getWidthOfSpace();
TextElement te = new TextElement(Utils.round(textPosition.getYDirAdj() - h, 2),
Utils.round(textPosition.getXDirAdj(), 2), Utils.round(textPosition.getWidthDirAdj(), 2),
Utils.round(textPosition.getHeightDir(), 2), textPosition.getFont(), textPosition.getFontSize(), c,
// workaround a possible bug in PDFBox:
// https://issues.apache.org/jira/browse/PDFBOX-1755
wos, textPosition.getDir());
this.minCharWidth = (float) Math.min(this.minCharWidth, te.getWidth());
this.minCharHeight = (float) Math.min(this.minCharHeight, te.getHeight());
this.spatialIndex.add(te);
this.textElements.add(te);
}
}
示例3: testCoverTextByRectanglesInput
import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/46080131/text-coordinates-when-stripping-from-pdfbox">
* Text coordinates when stripping from PDFBox
* </a>
* <p>
* This test applies the OP's code to an arbitrary PDF file and it did work properly
* (well, it did only cover the text from the baseline upwards but that is to be expected).
* </p>
*/
@Test
public void testCoverTextByRectanglesInput() throws IOException {
try ( InputStream resource = getClass().getResourceAsStream("input.pdf") ) {
PDDocument doc = PDDocument.load(resource);
myStripper stripper = new myStripper();
stripper.setStartPage(1); // fix it to first page just to test it
stripper.setEndPage(1);
stripper.getText(doc);
TextLine line = stripper.lines.get(1); // the line i want to paint on
float minx = -1;
float maxx = -1;
for (TextPosition pos: line.textPositions)
{
if (pos == null)
continue;
if (minx == -1 || pos.getTextMatrix().getTranslateX() < minx) {
minx = pos.getTextMatrix().getTranslateX();
}
if (maxx == -1 || pos.getTextMatrix().getTranslateX() > maxx) {
maxx = pos.getTextMatrix().getTranslateX();
}
}
TextPosition firstPosition = line.textPositions.get(0);
TextPosition lastPosition = line.textPositions.get(line.textPositions.size() - 1);
float x = minx;
float y = firstPosition.getTextMatrix().getTranslateY();
float w = (maxx - minx) + lastPosition.getWidth();
float h = lastPosition.getHeightDir();
PDPageContentStream contentStream = new PDPageContentStream(doc, doc.getPage(0), PDPageContentStream.AppendMode.APPEND, false);
contentStream.setNonStrokingColor(Color.RED);
contentStream.addRect(x, y, w, h);
contentStream.fill();
contentStream.close();
File fileout = new File(RESULT_FOLDER, "input-withRectangles.pdf");
doc.save(fileout);
doc.close();
}
}
示例4: addRect
import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
private void addRect(TextPosition tp, List<Rectangle> rects) {
Rectangle rect = new Rectangle();
float xdelta = tp.getWidthDirAdj() * .2F;
float ydelta = tp.getHeightDir() * .2F;
rect.x = (int) ((tp.getXDirAdj() - xdelta) * xFactor);
rect.y = (int) ((tp.getYDirAdj() - (tp.getHeightDir() + ydelta)) * yFactor);
rect.width = (int) ((tp.getWidthDirAdj() + xdelta * 2) * xFactor);
rect.height = (int) ((tp.getHeightDir() + ydelta * 2) * yFactor);
rects.add(rect);
}