当前位置: 首页>>代码示例>>Java>>正文


Java TextPosition.getWidth方法代码示例

本文整理汇总了Java中org.apache.pdfbox.text.TextPosition.getWidth方法的典型用法代码示例。如果您正苦于以下问题:Java TextPosition.getWidth方法的具体用法?Java TextPosition.getWidth怎么用?Java TextPosition.getWidth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.pdfbox.text.TextPosition的用法示例。


在下文中一共展示了TextPosition.getWidth方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: newFor

import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
public static Text newFor(TextPosition tp, PDGraphicsState gs,String text) {
		Text t = new Text();
		t.x = tp.getXDirAdj();
		t.baseline = tp.getYDirAdj();
		t.font = tp.getFont();
		t.strokeColor = gs.getStrokingColor();
		t.nonStrokeColor = gs.getNonStrokingColor();
		t.run = tp.getUnicode();
		t.width = tp.getWidth();
		t.height = tp.getHeight();
		t.pointSize = tp.getFontSizeInPt();
		t.fontSize = tp.getYScale();
		t.tempRun = t.run;

		// Bump the width by the word spacing for each space in tp.
/*		for (int i=0; i<tp.getCharacter().length(); i++) {
		    Character c = tp.getCharacter().charAt(i);
		    if (c.equals(" ")) {
		      t.width -= tp.getWidthOfSpace();
		        t.width += tp.getWordSpacing();
		    }
		}
*/		
		return t;
	}
 
开发者ID:TekstoSense,项目名称:pdf-segmenter,代码行数:26,代码来源:Text.java

示例2: deleteCharsInPath

import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
void deleteCharsInPath() {
    for (List<TextPosition> list : charactersByArticle) {
        List<TextPosition> toRemove = new ArrayList<>();
        for (TextPosition text : list) {
            Matrix textMatrix = text.getTextMatrix();
            Vector start = textMatrix.transform(new Vector(0, 0));
            Vector end = new Vector(start.getX() + text.getWidth(), start.getY());
            if (linePath.contains(lowerLeftX + start.getX(), lowerLeftY + start.getY()) ||
                    (checkEndPointToo && linePath.contains(lowerLeftX + end.getX(), lowerLeftY + end.getY()))) {
                toRemove.add(text);
            }
        }
        if (toRemove.size() != 0) {
            System.out.println(toRemove.size());
            list.removeAll(toRemove);
        }
    }
}
 
开发者ID:mkl-public,项目名称:testarea-pdfbox2,代码行数:19,代码来源:PDFVisibleTextStripper.java

示例3: 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();
    }
}
 
开发者ID:mkl-public,项目名称:testarea-pdfbox2,代码行数:69,代码来源:RectanglesOverText.java

示例4: checkForValue

import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
void checkForValue(List<TextPosition> textPositions)
{
    for (TextPosition textPosition : textPositions)
    {
        if (inField(textPosition))
        {
            float textX = textPosition.getTextMatrix().getTranslateX();
            if (textX > lastX + textPosition.getWidthOfSpace() / 2 && value.length() > 0)
                value += " ";
            value += textPosition.getUnicode();
            lastX = textX + textPosition.getWidth();
        }
    }
}
 
开发者ID:mkl-public,项目名称:testarea-pdfbox2,代码行数:15,代码来源:HelloSignAnalyzer.java

示例5: processTextPosition

import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
@Override
protected void processTextPosition(TextPosition text) {
    Matrix textMatrix = text.getTextMatrix();
    Vector start = textMatrix.transform(new Vector(0, 0));
    Vector end = new Vector(start.getX() + text.getWidth(), start.getY());

    PDGraphicsState gs = getGraphicsState();
    Area area = gs.getCurrentClippingPath();
    if (area == null ||
            (area.contains(lowerLeftX + start.getX(), lowerLeftY + start.getY()) &&
                    ((!checkEndPointToo) || area.contains(lowerLeftX + end.getX(), lowerLeftY + end.getY()))))
        super.processTextPosition(text);
}
 
开发者ID:mkl-public,项目名称:testarea-pdfbox2,代码行数:14,代码来源:PDFVisibleTextStripper.java

示例6: 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();
    }
}
 
开发者ID:mkl-public,项目名称:testarea-pdfbox2,代码行数:59,代码来源:RectanglesOverText.java

示例7: TextMetrics

import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
public TextMetrics(TextPosition tp)
{
    x = tp.getX();
    baseline = tp.getY();
    font = tp.getFont();
    width = tp.getWidth();
    height = tp.getHeight();
    pointSize = tp.getFontSizeInPt();
    fontSize = tp.getYScale();
    ascent = getAscent();
    descent = getDescent();
}
 
开发者ID:radkovo,项目名称:Pdf2Dom,代码行数:13,代码来源:TextMetrics.java

示例8: append

import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
public void append(TextPosition tp)
{
    width += tp.getX() - (x + width) + tp.getWidth();
    height = Math.max(height, tp.getHeight());
    ascent = Math.max(ascent, getAscent(tp.getFont(), tp.getYScale()));
    descent = Math.min(descent, getDescent(tp.getFont(), tp.getYScale()));
}
 
开发者ID:radkovo,项目名称:Pdf2Dom,代码行数:8,代码来源:TextMetrics.java

示例9: toPDFToken

import org.apache.pdfbox.text.TextPosition; //导入方法依赖的package包/类
public PDFToken toPDFToken() {
  val builder = PDFToken.builder();
  String tokenText = textPositions.stream().map(TextPosition::getUnicode).collect(Collectors.joining(""));
  // HACK(aria42) assumes left-to-right text
  TextPosition firstTP = textPositions.get(0);
  PDFont pdFont = firstTP.getFont();
  val desc = pdFont.getFontDescriptor();
  String fontFamily = desc == null ? PDFFontMetrics.UNKNWON_FONT_FAMILY : desc.getFontName();
  float ptSize = firstTP.getFontSizeInPt();
  //HACK(ddowney): it appears that sometimes (maybe when half-pt font sizes are used), pdfbox 2.0 will multiply
  //  all of the true font sizes by 10.  If we detect this is likely, we divide font size by ten:
  if(ptSize > 45.0f)
    ptSize /= 10.0f;
  //HACK(ddowney): ensure unique sizes get unique names/objects:
  fontFamily += "_" + ptSize + "_" + firstTP.getWidthOfSpace();
  val fontMetrics = PDFFontMetrics.of(fontFamily, ptSize, firstTP.getWidthOfSpace());
  builder.fontMetrics(fontMetrics);

  float minX = Float.POSITIVE_INFINITY;
  float maxX = Float.NEGATIVE_INFINITY;
  float minY = Float.POSITIVE_INFINITY;
  float maxY = Float.NEGATIVE_INFINITY;
  for (TextPosition tp : textPositions) {
    float x0 = tp.getX();
    if (x0 < minX) {
      minX = x0;
    }
    float x1 = x0 + tp.getWidth();
    if (x1 > maxX) {
      maxX = x1;
    }
    float y0 = tp.getY() - tp.getHeight(); //getY returns the bottom-left
    if (y0 < minY) {
      minY = y0;
    }
    float y1 = tp.getY();
    if (y1 > maxY) {
      maxY = y1;
    }
  }
  FloatList bounds = FloatArrayList.newListWith(minX, minY, maxX, maxY);
  builder.bounds(bounds);
  tokenText = discardSuperscripts(tokenText, bounds);
  // separate ligands
  tokenText = Normalizer.normalize(tokenText, Normalizer.Form.NFKC);
  builder.token(tokenText);
  return builder.build();
}
 
开发者ID:allenai,项目名称:science-parse,代码行数:49,代码来源:PDFExtractor.java


注:本文中的org.apache.pdfbox.text.TextPosition.getWidth方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。