本文整理汇总了Java中org.apache.pdfbox.pdmodel.PDPageContentStream.close方法的典型用法代码示例。如果您正苦于以下问题:Java PDPageContentStream.close方法的具体用法?Java PDPageContentStream.close怎么用?Java PDPageContentStream.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.PDPageContentStream
的用法示例。
在下文中一共展示了PDPageContentStream.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createAlternateRowsDocument
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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.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();
}
示例3: testRenderSdnList
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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: render
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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;
}
示例5: testMultiPageJFreeChart
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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();
}
示例6: testRender4700198773
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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();
}
}
}
}
示例7: testRotateCenter
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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"));
}
}
示例8: createSampleDocument
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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();
}
示例9: createBookPage
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
/**
* Creates a pdf page from two pages from another 'original' pdf document
* @param doc original pdf from which the pages will be taken
* @param leftPage page number of the page to go on the left side
* @param rightPage page number of the page to go on the right side
* @return generated page containing the left and right pages from the original document side-by-side.
*/
private static PDPage createBookPage(PDDocument doc, int leftPage, int rightPage) {
// double the width of a normal page to create the booklet
PDRectangle baseSize = doc.getPage(0).getMediaBox();
PDRectangle box = new PDRectangle(baseSize.getWidth()*2, baseSize.getHeight());
if(sizeOverride != null) {
box = sizeOverride.asPDRectangle();
}
PDPage page = new PDPage(box);
try {
PDImageXObject leftImg = PrintDF.pageToImage(doc, leftPage, scale);
PDImageXObject rightImg = PrintDF.pageToImage(doc, rightPage, scale);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
if(leftImg != null)
contentStream.drawImage(leftImg, 0, 0, box.getWidth()/2, box.getHeight());
if(rightImg != null)
contentStream.drawImage(rightImg, box.getWidth()/2, 0, box.getWidth()/2, box.getHeight());
contentStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return page;
}
示例10: printPDFFile
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
@FXML
public void printPDFFile(ActionEvent event) throws IOException {
try {
String fileName = "PDFoutput.pdf";
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
content.beginText();
content.setFont(PDType1Font.TIMES_ROMAN, 26);
content.moveTextPositionByAmount(220, 750);
content.drawString("Titel");
content.endText();
content.beginText();
content.setFont(PDType1Font.TIMES_ROMAN, 16);
content.moveTextPositionByAmount(80, 700);
content.drawString("Inhoud");
content.endText();
content.close();
doc.save(fileName);
doc.close();
System.out.println("your file was saved in: " + System.getProperty("user.dir"));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
示例11: 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;
}
示例12: testCoverTextByRectanglesMwbI201711
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的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();
}
}
示例13: applyTexturePaint
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
private void applyTexturePaint(TexturePaint texturePaint) throws IOException {
Rectangle2D anchorRect = texturePaint.getAnchorRect();
PDTilingPattern pattern = new PDTilingPattern();
pattern.setPaintType(PDTilingPattern.PAINT_COLORED);
pattern.setTilingType(PDTilingPattern.TILING_CONSTANT_SPACING_FASTER_TILING);
pattern.setBBox(new PDRectangle((float) anchorRect.getX(), (float) anchorRect.getY(),
(float) anchorRect.getWidth(), (float) anchorRect.getHeight()));
pattern.setXStep((float) anchorRect.getWidth());
pattern.setYStep((float) anchorRect.getHeight());
AffineTransform patternTransform = new AffineTransform();
patternTransform.translate(0, anchorRect.getHeight());
patternTransform.scale(1f, -1f);
pattern.setMatrix(patternTransform);
PDAppearanceStream appearance = new PDAppearanceStream(document);
appearance.setResources(pattern.getResources());
appearance.setBBox(pattern.getBBox());
PDPageContentStream imageContentStream = new PDPageContentStream(document, appearance,
((COSStream) pattern.getCOSObject()).createOutputStream());
BufferedImage texturePaintImage = texturePaint.getImage();
PDImageXObject imageXObject = imageEncoder.encodeImage(document, imageContentStream, texturePaintImage);
float ratioW = (float) ((anchorRect.getWidth()) / texturePaintImage.getWidth());
float ratioH = (float) ((anchorRect.getHeight()) / texturePaintImage.getHeight());
float paintHeight = (texturePaintImage.getHeight()) * ratioH;
imageContentStream.drawImage(imageXObject, (float) anchorRect.getX(), (float) (paintHeight + anchorRect.getY()),
texturePaintImage.getWidth() * ratioW, -paintHeight);
imageContentStream.close();
PDColorSpace patternCS1 = new PDPattern(null, imageXObject.getColorSpace());
COSName tilingPatternName = resources.add(pattern);
PDColor patternColor = new PDColor(tilingPatternName, patternCS1);
contentStream.setNonStrokingColor(patternColor);
contentStream.setStrokingColor(patternColor);
}
示例14: exportGraphic
import org.apache.pdfbox.pdmodel.PDPageContentStream; //导入方法依赖的package包/类
@SuppressWarnings("SpellCheckingInspection")
void exportGraphic(String dir, String name, GraphicsExporter exporter) {
try {
PDDocument document = new PDDocument();
PDFont pdArial = PDFontFactory.createDefaultFont();
File parentDir = new File("target/test/" + dir);
// noinspection ResultOfMethodCallIgnored
parentDir.mkdirs();
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D imageGraphics = image.createGraphics();
exporter.draw(imageGraphics);
imageGraphics.dispose();
ImageIO.write(image, "PNG", new File(parentDir, name + ".png"));
for (Mode m : Mode.values()) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 400, 400);
PdfBoxGraphics2DFontTextDrawer fontTextDrawer = null;
contentStream.beginText();
contentStream.setStrokingColor(0, 0, 0);
contentStream.setNonStrokingColor(0, 0, 0);
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15);
contentStream.setTextMatrix(Matrix.getTranslateInstance(10, 800));
contentStream.showText("Mode " + m);
contentStream.endText();
switch (m) {
case FontTextIfPossible:
fontTextDrawer = new PdfBoxGraphics2DFontTextDrawer();
fontTextDrawer.registerFont(
new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
break;
case DefaultFontText: {
fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
fontTextDrawer.registerFont(
new File("src/test/resources/de/rototor/pdfbox/graphics2d/DejaVuSerifCondensed.ttf"));
break;
}
case ForceFontText:
fontTextDrawer = new PdfBoxGraphics2DFontTextForcedDrawer();
fontTextDrawer.registerFont(
PdfBoxGraphics2DTestBase.class.getResourceAsStream("DejaVuSerifCondensed.ttf"));
fontTextDrawer.registerFont("Arial", pdArial);
break;
case DefaultVectorized:
default:
break;
}
if (fontTextDrawer != null) {
pdfBoxGraphics2D.setFontTextDrawer(fontTextDrawer);
}
exporter.draw(pdfBoxGraphics2D);
pdfBoxGraphics2D.dispose();
PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
Matrix matrix = new Matrix();
matrix.translate(0, 20);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
matrix.scale(1.5f, 1.5f);
matrix.translate(0, 100);
contentStream.transform(matrix);
contentStream.drawForm(appearanceStream);
contentStream.close();
}
document.save(new File(parentDir, name + ".pdf"));
document.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例15: 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;
}