本文整理汇总了Java中org.apache.pdfbox.pdmodel.PDPageContentStream.newLineAtOffset方法的典型用法代码示例。如果您正苦于以下问题:Java PDPageContentStream.newLineAtOffset方法的具体用法?Java PDPageContentStream.newLineAtOffset怎么用?Java PDPageContentStream.newLineAtOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.PDPageContentStream
的用法示例。
在下文中一共展示了PDPageContentStream.newLineAtOffset方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createRingManagerDocument
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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();
}
示例2: addHeaderPJ
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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;
}
示例3: generateBillHeader
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
private void generateBillHeader(PDPage firstPage, PDPageContentStream contentStream)
throws IOException {
// Add header text
PDFont currentFont;
int currentFontSize;
String headerLine1 = "Git Rekt Resort";
String headerLine2 = "Customer Bill";
contentStream.setLeading(10);
currentFont = BOLD;
currentFontSize = 14;
contentStream.setFont(currentFont, currentFontSize);
contentStream.beginText();
float offsetX = getCenteredTextXPos(firstPage, headerLine1, currentFont, currentFontSize);
contentStream.newLineAtOffset(offsetX, 750f);
contentStream.showText(headerLine1);
currentFont = PDType1Font.COURIER_BOLD;
currentFontSize = 12;
contentStream.setFont(currentFont, currentFontSize);
float offsetX2 = getCenteredTextXPos(firstPage, headerLine2, currentFont, currentFontSize);
contentStream.newLineAtOffset(-offsetX + offsetX2, -5f);
contentStream.newLine();
contentStream.showText(headerLine2);
contentStream.endText();
}
示例4: test001
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
@Test
public void test001() throws IOException {
//E:\Repository\Git\melon\melon-sample-pdf\src\main\resources\HTTP权威指南.pdf
System.out.println("Hello World!");
PDDocument pdDocument = new PDDocument();
PDPage pdPage = new PDPage();
pdDocument.addPage(pdPage);
PDFont pdFont = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contentStream = new PDPageContentStream(pdDocument, pdPage);
contentStream.beginText();
contentStream.setFont(pdFont, 14);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello World");
contentStream.endText();
contentStream.close();
String directory = PdfDemo.class.getClassLoader().getResource("").getPath();
String fileName = "text.pdf";
pdDocument.save(directory + fileName);
pdDocument.close();
}
示例5: drawText
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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: addTextAtOffset
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
@Override
public void addTextAtOffset(String text, PDFont font, float fontSize, Color textColor, float xCoordinate, float yCoordinate, PDPageContentStream contentStream) throws IOException {
if (text.isEmpty()) {
log.warn("The inputs are empty string start from the position: ".concat(xCoordinate + ", " + yCoordinate));
}
if (textColor == null) {
textColor = Color.BLACK;
}
contentStream.setNonStrokingColor(textColor);
contentStream.setFont(font, fontSize);
contentStream.beginText();
contentStream.newLineAtOffset(xCoordinate, yCoordinate);
contentStream.showText(text);
contentStream.endText();
// Reset changed color
resetChangedColorToDefault(contentStream);
}
示例7: addWrappedParagraphByLineBreaks
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
@Override
public void addWrappedParagraphByLineBreaks(String content, PDFont font, float fontSize, Color textColor, float yCoordinate, float leftRightMargin, PDPage page, PDPageContentStream contentStream) throws IOException {
final float lineSpacing = 1.4f * fontSize;
float width = page.getMediaBox().getWidth() - 2 * leftRightMargin;
float startX = page.getMediaBox().getLowerLeftX() + leftRightMargin;
List<String> lines = calculateLinesToWrap(content, font, fontSize, width);
contentStream.beginText();
contentStream.setFont(font, fontSize);
contentStream.setNonStrokingColor(textColor);
contentStream.newLineAtOffset(startX, yCoordinate);
for (String line : lines) {
contentStream.showText(line.replaceAll(PdfBoxHandler.TAB_REGEX, PdfBoxHandler.SPACE_STRING));
contentStream.newLineAtOffset(0, -lineSpacing);
}
contentStream.endText();
// Reset changed color
resetChangedColorToDefault(contentStream);
}
示例8: fillTextToTable
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
private void fillTextToTable(PDPageContentStream contentStream, TableAttribute tableAttribute, String[][] tableContent) throws IOException {
//Set text font and font size
contentStream.setFont(tableAttribute.getContentFont(), tableAttribute.getContentFontSize());
final float cellMargin = tableAttribute.getCellMargin();
// Define to start drawing content at horizontal position
float nextTextX = tableAttribute.getXCoordinate() + cellMargin;
// Define to start drawing content at vertical position
float nextTextY = calculateDrawPositionInVertical(tableAttribute);
for (String[] aContent : tableContent) {
int index = 0;
for (String text : aContent) {
contentStream.beginText();
contentStream.newLineAtOffset(nextTextX, nextTextY);
contentStream.showText(text != null ? text : "");
contentStream.endText();
nextTextX += tableAttribute.getColumns().get(index).getCellWidth();
index++;
}
// Update new position cursor after writing the content for one row
nextTextY -= tableAttribute.getRowHeight();
nextTextX = tableAttribute.getXCoordinate() + cellMargin;
}
}
示例9: prepareSmallPdf
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
/**
* @see #testJoinSmallAndBig()
*/
PDDocument prepareSmallPdf() throws IOException {
PDDocument document = new PDDocument();
PDPage page = new PDPage(new PDRectangle(72, 72));
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setNonStrokingColor(Color.YELLOW);
contentStream.addRect(0, 0, 72, 72);
contentStream.fill();
contentStream.setNonStrokingColor(Color.BLACK);
PDFont font = PDFontFactory.createDefaultFont();
contentStream.beginText();
contentStream.setFont(font, 18);
contentStream.newLineAtOffset(2, 54);
contentStream.showText("small");
contentStream.newLineAtOffset(0, -48);
contentStream.showText("page");
contentStream.endText();
contentStream.close();
return document;
}
示例10: prepareBiggerPdf
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
/**
* @see #testJoinSmallAndBig()
*/
PDDocument prepareBiggerPdf() throws IOException {
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A5);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.setNonStrokingColor(Color.GREEN);
contentStream.addRect(0, 0, PDRectangle.A5.getWidth(), PDRectangle.A5.getHeight());
contentStream.fill();
contentStream.setNonStrokingColor(Color.BLACK);
PDFont font = PDFontFactory.createDefaultFont();
contentStream.beginText();
contentStream.setFont(font, 18);
contentStream.newLineAtOffset(2, PDRectangle.A5.getHeight() - 24);
contentStream.showText("This is the Bigger page");
contentStream.newLineAtOffset(0, -48);
contentStream.showText("BIGGER!");
contentStream.endText();
contentStream.close();
return document;
}
示例11: getBillAsPdf
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
public PDDocument getBillAsPdf() throws IOException {
// Add blank page to document
PDPage firstPage = new PDPage();
document.addPage(firstPage);
PDPageContentStream contentStream = new PDPageContentStream(document, firstPage);
generateBillHeader(firstPage, contentStream);
generateBillInfoText(firstPage, contentStream);
contentStream.beginText();
contentStream.newLineAtOffset(60f, 640f);
// Add the column line item header line
contentStream.setFont(PDType1Font.COURIER_BOLD, NORMAL_FONT_SIZE);
contentStream.newLine();
contentStream.showText(lineItemHeader);
// Add bill line items
contentStream.setFont(NORMAL_FONT, NORMAL_FONT_SIZE);
for(String lineItem : lineItems) {
contentStream.newLine();
contentStream.showText(lineItem);
}
// Display the bill total at the end
contentStream.newLine();
String billTotalLine = getBillTotalLine();
contentStream.setFont(PDType1Font.COURIER_BOLD, NORMAL_FONT_SIZE);
contentStream.newLine();
contentStream.showText(billTotalLine);
// Display important bill information
contentStream.newLine();
contentStream.newLine();
contentStream.showText("WARNING: Failure to pay bill may result in broken legs.");
contentStream.endText();
contentStream.close();
return document;
}
示例12: drawBadgeNumber
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
private void drawBadgeNumber(PDPage page, PDFont font, Attendee attendee) throws IOException {
String badgeNumber = attendee.getBadgeNumber();
if (badgeNumber == null) {
return; // no text, don't draw anything
}
List<String> badgeNumberParts = BadgeLib.splitBadgeNumber(badgeNumber);
PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);
String stripeColor = attendee.getCurrentAgeRange().getStripeColor();
stream.setNonStrokingColor(BadgeLib.getForegroundColor(stripeColor));
// Bounding box:
// stream.fillRect(163, 95, 40, 30);
PDRectangle boundingBox = new PDRectangle(163, 95, 40, 30);
stream.setLineWidth(0.25f);
stream.beginText();
int fontSize = BadgeLib.findMaxFontSize(font, badgeNumberParts,boundingBox);
stream.setFont(font, fontSize);
float textWidth = font.getStringWidth(badgeNumberParts.get(0));
Float offset = textWidth * (fontSize/(2*1000.0f));
stream.newLineAtOffset(185-offset, 105+fontSize); // First character position
stream.showText(badgeNumberParts.get(0));
if (badgeNumberParts.size() > 1) {
textWidth = font.getStringWidth(badgeNumberParts.get(1));
Float newOffset = textWidth * (fontSize/(2*1000.0f));
stream.newLineAtOffset(offset-newOffset, -1*fontSize); // First character position
stream.showText(badgeNumberParts.get(1));
}
stream.close();
}
示例13: drawBadgeTypeText
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
private void drawBadgeTypeText(PDPage page, PDFont font, Attendee attendee) throws IOException {
if (attendee.getBadge() == null || attendee.getBadge().getBadgeTypeText() == null) {
return; // no text, don't draw anything
}
String badgeTypeText = attendee.getBadge().getBadgeTypeText();
PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);
// PDRectangle boundingBox = new PDRectangle(206, 85, 253, 44);
stream.setLineWidth(0.5f);
stream.beginText();
int fontSize = BadgeLib.findMaxLineSize(font, badgeTypeText,240, 40);
stream.setFont(font, fontSize);
if (attendee.getBadge() != null &&
(attendee.getBadge().getBadgeTypeText().equals("Friday") ||
attendee.getBadge().getBadgeTypeText().equals("Saturday") ||
attendee.getBadge().getBadgeTypeText().equals("Sunday"))) {
if (attendee.getBadge() != null && attendee.getBadge().getBadgeTypeBackgroundColor() != null) {
stream.setNonStrokingColor(BadgeLib.getForegroundColor(attendee.getCurrentAgeRange().getStripeColor()));
} else {
stream.setNonStrokingColor(Color.WHITE);
stream.setStrokingColor(Color.black);
}
} else {
if (attendee.getBadge() != null && attendee.getBadge().getBadgeTypeBackgroundColor() != null) {
stream.setNonStrokingColor(BadgeLib.getForegroundColor(attendee.getBadge().getBadgeTypeBackgroundColor()));
} else {
stream.setNonStrokingColor(Color.WHITE);
stream.setStrokingColor(Color.black);
}
}
float textWidth = font.getStringWidth(badgeTypeText);
Float offset = textWidth * (fontSize/(2*1000.0f));
stream.newLineAtOffset(330-offset, 100); // First character position
stream.showText(badgeTypeText);
stream.close();
}
示例14: createOverlayDocument
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
private static void createOverlayDocument(File overlayPdf) throws IOException
{
PDDocument overlayDoc = new PDDocument();
try
{
PDPage page=new PDPage();
overlayDoc.addPage(page);
PDFont font=PDType1Font.COURIER_OBLIQUE;
PDPageContentStream contentStream=new PDPageContentStream(overlayDoc, page);
contentStream.setFont(font, 50);
contentStream.setNonStrokingColor(0);
contentStream.beginText();
contentStream.newLineAtOffset(100, 500);
contentStream.setNonStrokingColor(255, 0, 0);
contentStream.showText("CLASSIFIED");
contentStream.endText();
contentStream.close();
overlayDoc.save(overlayPdf);
}
finally
{
overlayDoc.close();
}
}
示例15: main
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException
{
PDDocument doc = new PDDocument();
File pdfFile = File.createTempFile("devict-example", ".pdf");
try
{
PDPage page = new PDPage();
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contents = new PDPageContentStream(doc, page);
contents.beginText();
contents.setFont(font, 12);
contents.newLineAtOffset(100, 700);
contents.showText("Hello DevICT PDFBox the time is " + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
contents.endText();
contents.close();
doc.save(pdfFile);
}
finally
{
System.out.printf("PDF created and saved at %s\n", pdfFile.getAbsolutePath());
doc.close();
}
Desktop.getDesktop().open(pdfFile);
}