本文整理汇总了Java中org.apache.pdfbox.pdmodel.edit.PDPageContentStream.moveTextPositionByAmount方法的典型用法代码示例。如果您正苦于以下问题:Java PDPageContentStream.moveTextPositionByAmount方法的具体用法?Java PDPageContentStream.moveTextPositionByAmount怎么用?Java PDPageContentStream.moveTextPositionByAmount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.edit.PDPageContentStream
的用法示例。
在下文中一共展示了PDPageContentStream.moveTextPositionByAmount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create100Pages
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
public PDDocument create100Pages() throws IOException
{
PDDocument document = new PDDocument();
for (int i = 0; i < 100; i++)
{
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream content = new PDPageContentStream(document, page);
content.beginText();
content.setFont(PDType1Font.HELVETICA_BOLD, 100);
content.moveTextPositionByAmount(100, 300);
content.drawString(String.format("-%s-", i + 1));
content.endText();
content.close();
}
return document;
}
示例2: createTestDocument
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
byte[] createTestDocument() throws IOException, COSVisitorException
{
try ( ByteArrayOutputStream baos = new ByteArrayOutputStream();
PDDocument doc = new PDDocument() )
{
PDPage page = new PDPage(new PDRectangle(792, 612));
doc.addPage(page);
PDFont font = PDType1Font.COURIER;
PDPageContentStream contents = new PDPageContentStream(doc, page);
contents.beginText();
contents.setFont(font, 9);
contents.moveTextPositionByAmount(100, 500);
contents.drawString(" 2 Netto 5,00 EUR 3,00");
contents.moveTextPositionByAmount(0, 0);
contents.drawString(" 2882892 ENERGIZE LR6 Industrial 2,50 EUR 1");
contents.endText();
contents.close();
doc.save(baos);
return baos.toByteArray();
}
}
示例3: create
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的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();
}
示例4: addTitleRow
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
public void addTitleRow(String[] titles) throws IOException {
if (hideTable)
return;
int size = 16;
float cellSize = (getWidth()/titles.length);
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream cs = new PDPageContentStream(document, page, true, false);
cs.setFont(font, size);
float leftX = getLeftX()+30;
for (String title : titles) {
cs.beginText();
cs.moveTextPositionByAmount(leftX, page.getYCursor());
cs.drawString(title);
cs.endText();
if (cellSize != -1)
leftX += cellSize;
else
leftX += (font.getStringWidth(title) / 1000 * size)+5;
}
cs.drawLine(getLeftX(), page.getYCursor() - 5, getRightX(), page.getYCursor() - 5);
cs.close();
scrolldown();
}
示例5: createRollover
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
void createRollover(PDAnnotation annotation, String filename) throws IOException, COSVisitorException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
List<PDAnnotation> annotations = page.getAnnotations();
float x = 100;
float y = 500;
String text = "PDFBox";
PDFont font = PDType1Font.HELVETICA_BOLD;
float textWidth = font.getStringWidth(text) / 1000 * 18;
PDPageContentStream contents = new PDPageContentStream(document, page);
contents.beginText();
contents.setFont(font, 18);
contents.moveTextPositionByAmount(x, y);
contents.drawString(text);
contents.endText();
contents.close();
PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
PDAppearanceStream normal = createAppearanceStream(document, textWidth, font, "0.5 0.5 0.5 rg");
PDAppearanceStream rollover = createAppearanceStream(document, textWidth, font, "1 0.7 0.5 rg");
PDAppearanceStream down = createAppearanceStream(document, textWidth, font, "0 0 0 rg");
appearanceDictionary.setNormalAppearance(normal);
appearanceDictionary.setRolloverAppearance(rollover);
appearanceDictionary.setDownAppearance(down);
annotation.setAppearance(appearanceDictionary);
PDRectangle position = new PDRectangle();
position.setLowerLeftX(x);
position.setLowerLeftY(y - 5);
position.setUpperRightX(x + textWidth);
position.setUpperRightY(y + 20);
annotation.setRectangle(position);
annotations.add(annotation);
document.save(new File(RESULT_FOLDER, filename));
document.close();
}
示例6: testDrawEuro
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/22260344/pdfbox-encode-symbol-currency-euro">
* PdfBox encode symbol currency euro
* </a>
* <p>
* Three ways of trying to draw a '�' symbol, the first one fails.
* </p>
*/
@Test
public void testDrawEuro() throws IOException, COSVisitorException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contents = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA_BOLD;
contents.beginText();
contents.setFont(font, 18);
contents.moveTextPositionByAmount(30, 600);
contents.drawString("�");
contents.moveTextPositionByAmount(0, -30);
contents.drawString(String.valueOf(Character.toChars(EncodingManager.INSTANCE.getEncoding(COSName.WIN_ANSI_ENCODING).getCode("Euro"))));
contents.moveTextPositionByAmount(0, -30);
byte[] commands = "(x) Tj ".getBytes();
commands[1] = (byte) 128;
contents.appendRawCommands(commands);
contents.endText();
contents.close();
document.save(new File(RESULT_FOLDER, "Euro.pdf"));
document.close();
}
示例7: testDrawTmSignBroken
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/30619974/pdfbox-unable-to-write-superscripted-characters">
* PDFBox unable to write superscripted characters
* </a>
* <p>
* {@link #testDrawTmSignBroken()} represents the observation the OP made,
* {@link #testDrawTmSignCustomDraw()} is a solution using custom drawing operations, and
* {@link #testDrawTmSignLetters()} is a solution drawing the trademark symbol using smaller letters with text rise.
* </p>
*/
@Test
public void testDrawTmSignBroken() throws IOException, COSVisitorException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contents = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA_BOLD;
contents.beginText();
contents.setFont(font, 18);
contents.moveTextPositionByAmount(30, 600);
contents.drawString("90000039-PREDISOL � C YELLOW 13 SNDOT�M");
contents.endText();
contents.close();
document.save(new File(RESULT_FOLDER, "TM_naive.pdf"));
document.close();
}
示例8: testDrawTmSignCustomDraw
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
@Test
public void testDrawTmSignCustomDraw() throws IOException, COSVisitorException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contents = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA_BOLD;
contents.beginText();
contents.setFont(font, 18);
contents.moveTextPositionByAmount(30, 600);
contents.drawString("90000039-PREDISOL � C YELLOW 13 SNDOT");
byte[] commands = "(x) Tj ".getBytes();
commands[1] = (byte) 0231;
contents.appendRawCommands(commands);
contents.drawString("M");
contents.endText();
contents.close();
document.save(new File(RESULT_FOLDER, "TM_customDraw.pdf"));
document.close();
}
示例9: testDrawTmSignLetters
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
@Test
public void testDrawTmSignLetters() throws IOException, COSVisitorException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contents = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA_BOLD;
contents.beginText();
contents.setFont(font, 18);
contents.moveTextPositionByAmount(30, 600);
contents.drawString("90000039-PREDISOL � C YELLOW 13 SNDOT");
contents.appendRawCommands("\n6 Ts\n".getBytes());
contents.setFont(font, 10);
contents.drawString("TM");
contents.appendRawCommands("\n0 Ts\n".getBytes());
contents.setFont(font, 18);
contents.drawString("M");
contents.endText();
contents.close();
document.save(new File(RESULT_FOLDER, "TM_letters.pdf"));
document.close();
}
示例10: create1Page
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
public PDDocument create1Page(String mark) throws IOException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream content = new PDPageContentStream(document, page);
content.beginText();
content.setFont(PDType1Font.HELVETICA_BOLD, 100);
content.moveTextPositionByAmount(100, 300);
content.drawString(mark);
content.endText();
content.close();
return document;
}
示例11: addTitleAndSubtitle
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
private void addTitleAndSubtitle(int offset) throws IOException {
PDPageContentStream cs = new PDPageContentStream(document, this, true, false);
cs.beginText();
cs.setFont(PDType1Font.HELVETICA_BOLD, 30);
float xPosition = getLeftX()+50;
float yPosition = ((getTopY()/2)-borders-offset);
cs.moveTextPositionByAmount(xPosition, yPosition);
cs.drawString(title);
cs.endText();
cs.beginText();
cs.setFont(PDType1Font.HELVETICA_OBLIQUE, 20);
yPosition -= 40;
cs.moveTextPositionByAmount(xPosition, yPosition);
cs.drawString(subtitle);
cs.endText();
cs.close();
}
示例12: flushTableRow
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
protected void flushTableRow(String[] elements, PDFont font, int size, float cellSize) throws IOException {
PDPageContentStream cs = new PDPageContentStream(document, page, true, false);
cs.setFont(font, size);
float leftX = getLeftX();
for (String element : elements) {
cs.beginText();
cs.moveTextPositionByAmount(leftX, page.getYCursor());
cs.drawString(element);
cs.endText();
if (cellSize != -1)
leftX += cellSize;
else
leftX += (font.getStringWidth(element) / 1000 * size)+5;
}
cs.drawLine(getLeftX(), page.getYCursor() - 5, getRightX(), page.getYCursor() - 5);
cs.close();
}
示例13: testExtractText
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
@Test
public void testExtractText() throws Exception {
final String expectedText = "Test string";
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(20, 400);
contentStream.drawString(expectedText);
contentStream.endText();
contentStream.close();
template.sendBody("direct:start", document);
resultEndpoint.setExpectedMessageCount(1);
resultEndpoint.expectedMessagesMatches(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
Object body = exchange.getIn().getBody();
assertThat(body, instanceOf(String.class));
assertThat((String) body, containsString(expectedText));
return true;
}
});
resultEndpoint.assertIsSatisfied();
}
示例14: testAppend
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
@Test
public void testAppend() throws Exception {
final String originalText = "Test";
final String textToAppend = "Append";
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(20, 400);
contentStream.drawString(originalText);
contentStream.endText();
contentStream.close();
template.sendBodyAndHeader("direct:start", textToAppend, PdfHeaderConstants.PDF_DOCUMENT_HEADER_NAME, document);
resultEndpoint.setExpectedMessageCount(1);
resultEndpoint.expectedMessagesMatches(new Predicate() {
@Override
public boolean matches(Exchange exchange) {
Object body = exchange.getIn().getBody();
assertThat(body, instanceOf(ByteArrayOutputStream.class));
try {
PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray()));
PDFTextStripper pdfTextStripper = new PDFTextStripper();
String text = pdfTextStripper.getText(doc);
assertEquals(2, doc.getNumberOfPages());
assertThat(text, containsString(originalText));
assertThat(text, containsString(textToAppend));
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
});
resultEndpoint.assertIsSatisfied();
}
示例15: drawTable
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; //导入方法依赖的package包/类
public static void drawTable(PDPage page,
PDPageContentStream contentStream, float y, float margin,
String[][] content) throws IOException {
final int rows = content.length;
final int cols = content[0].length;
final float rowHeight = 20f;
final float tableWidth = page.findMediaBox().getWidth() - (2 * margin);
final float tableHeight = rowHeight * rows;
final float colWidth = tableWidth / (float) cols;
final float cellMargin = 5f;
float nexty = y;
for (int i = 0; i <= rows; i++) {
contentStream.drawLine(margin, nexty, margin + tableWidth, nexty);
nexty -= rowHeight;
}
// draw the columns
float nextx = margin;
for (int i = 0; i <= cols; i++) {
contentStream.drawLine(nextx, y, nextx, y - tableHeight);
nextx += colWidth;
}
// now add the text
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
float textx = margin + cellMargin;
float texty = y - 15;
for (int i = 0; i < content.length; i++) {
for (int j = 0; j < content[i].length; j++) {
String text = content[i][j];
contentStream.beginText();
if(i == 0) {
contentStream.setNonStrokingColor(Color.BLUE);
} else {
contentStream.setNonStrokingColor(Color.BLACK);
}
contentStream.moveTextPositionByAmount(textx, texty);
contentStream.drawString(text);
contentStream.endText();
textx += colWidth;
}
texty -= rowHeight;
textx = margin + cellMargin;
}
}