本文整理汇总了Java中org.apache.pdfbox.pdmodel.PDDocument.addPage方法的典型用法代码示例。如果您正苦于以下问题:Java PDDocument.addPage方法的具体用法?Java PDDocument.addPage怎么用?Java PDDocument.addPage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.PDDocument
的用法示例。
在下文中一共展示了PDDocument.addPage方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createAlternateRowsDocument
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的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.PDDocument; //导入方法依赖的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: removeText
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的package包/类
private PDDocument removeText(PDPage page) throws IOException {
PDFStreamParser parser = new PDFStreamParser(page);
parser.parse();
List<Object> tokens = parser.getTokens();
List<Object> newTokens = new ArrayList<>();
for (Object token : tokens) {
if (token instanceof Operator) {
Operator op = (Operator) token;
if (op.getName().equals("TJ") || op.getName().equals("Tj")) {
//remove the one argument to this operator
newTokens.remove(newTokens.size() - 1);
continue;
}
}
newTokens.add(token);
}
PDDocument document = new PDDocument();
document.addPage(page);
PDStream newContents = new PDStream(document);
OutputStream out = newContents.createOutputStream(COSName.FLATE_DECODE);
ContentStreamWriter writer = new ContentStreamWriter(out);
writer.writeTokens(newTokens);
out.close();
page.setContents(newContents);
return document;
}
示例4: testMultiPageJFreeChart
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的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();
}
示例5: createSampleDocument
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的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();
}
示例6: printPDFFile
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的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());
}
}
示例7: createPage
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的package包/类
static void createPage(PDDocument temp,int count)
{
for(int i = 0; i < count;i++)
{
PDPage page = new PDPage(new PDRectangle(297 * POINTS_PER_MM, 210 * POINTS_PER_MM));
temp.addPage(page);
}
}
示例8: saveDocument
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的package包/类
@Override
public void saveDocument(List<Page> pages, File file) throws IOException {
if (pages.size() == 0) {
throw new IOException("Empty document.");
}
PDDocument outDoc = new PDDocument();
Map<File,PDDocument> docs = new HashMap<File,PDDocument>();
try {
for (Page page : pages) {
PDDocument pageDoc = docs.get(page.getFile());
if (pageDoc == null) {
pageDoc = PDDocument.load(page.getFile());
docs.put(page.getFile(), pageDoc);
}
outDoc.addPage((PDPage)pageDoc.getPrintable(page.getIndex()));
}
try {
outDoc.save(file.toString());
}
catch (COSVisitorException e) {
throw new IOException(e);
}
}
finally {
outDoc.close();
for (PDDocument doc : docs.values()) {
doc.close();
}
}
}
示例9: exportGraphic
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的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);
}
}
示例10: newPage
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的package包/类
/**
* Starts a new page with the same size as the last one
*
* @param document
* The document the table is rendered to
* @param stream
* The PDPageContentStream used to render the table up to now (will be closed after calling this method)
* @return A new PDPageContentStream for rendering to the new page
* @throws IOException
* If writing to the streams fails
*/
private PDPageContentStream newPage(final PDDocument document, final PDPageContentStream stream) throws IOException {
final PDRectangle pageSize = document.getPage(document.getNumberOfPages() - 1).getMediaBox();
handleEvent(EventType.END_PAGE, document, stream, 0, pageSize.getHeight(), pageSize.getWidth(), pageSize.getHeight());
stream.close();
final PDPage page = new PDPage(pageSize);
document.addPage(page);
PDPageContentStream newStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
handleEvent(EventType.BEGIN_PAGE, document, newStream, 0, pageSize.getHeight(), pageSize.getWidth(), pageSize.getHeight());
return newStream;
}
示例11: make
import org.apache.pdfbox.pdmodel.PDDocument; //导入方法依赖的package包/类
/**
* Given a pdf document, creates a printable booklet.
* @param doc original document to be converted
* @return booklet form of the original document.
*/
public static PDDocument make(PDDocument doc) {
PDDocument booklet = new PDDocument();
// "effective pages" defines the effective number of pages in the booklet (including blanks)
int effectivePages = doc.getNumberOfPages() + doc.getNumberOfPages() % 4;
// "pages" defines the actual number of sides of paper the booklet will use
int pages = effectivePages / 2;
boolean left = false; // smaller page number goes on the left?
for(int i=0; i<pages; i++) {
int page1 = i;
int page2 = effectivePages - i - 1;
PDPage page;
if(left) page = createBookPage(doc, page1, page2);
else page = createBookPage(doc, page2, page1);
booklet.addPage(page);
left = !left;
}
return booklet;
}