public void parse(File file, int maxPaths, ProgressMonitor monitor) throws Exception { monitor.beginTask(tr("Parsing PDF", 1)); PDDocument document = PDDocument.load(file); if (document.isEncrypted()) { throw new Exception(tr("Encrypted documents not supported.")); } List<?> allPages = document.getDocumentCatalog().getAllPages(); if (allPages.size() != 1) { throw new Exception(tr("The PDF file must have exactly one page.")); } PDPage page = (PDPage) allPages.get(0); PDRectangle pageSize = page.findMediaBox(); Integer rotationVal = page.getRotation(); int rotation = 0; if (rotationVal != null) { rotation = rotationVal.intValue(); } GraphicsProcessor p = new GraphicsProcessor(target, rotation, maxPaths, monitor); PageDrawer drawer = new PageDrawer(); drawer.drawPage(p, page); this.target.bounds = new Rectangle2D.Double( pageSize.getLowerLeftX(), pageSize.getLowerLeftY(), pageSize.getWidth(), pageSize.getHeight()); monitor.finishTask(); }
/** * Returns the given page as an RGB image at the given scale. * * @param pageIndex the zero-based index of the page to be converted * @param scale the scaling factor, where 1 = 72 DPI * @param config the bitmap config to create * @return the rendered page image * @throws IOException if the PDF cannot be read */ public Bitmap renderImage(int pageIndex, float scale, Bitmap.Config config) throws IOException { PDPage page = document.getPage(pageIndex); PDRectangle cropbBox = page.getCropBox(); float widthPt = cropbBox.getWidth(); float heightPt = cropbBox.getHeight(); int widthPx = Math.round(widthPt * scale); int heightPx = Math.round(heightPt * scale); int rotationAngle = page.getRotation(); // swap width and height Bitmap image; if (rotationAngle == 90 || rotationAngle == 270) { image = Bitmap.createBitmap(heightPx, widthPx, config); } else { image = Bitmap.createBitmap(widthPx, heightPx, config); } // use a transparent background if the imageType supports alpha Paint paint = new Paint(); Canvas canvas = new Canvas(image); if (config != Bitmap.Config.ARGB_8888) { paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.FILL); canvas.drawRect(0, 0, image.getWidth(), image.getHeight(), paint); paint.reset(); } renderPage(page, paint, canvas, image.getWidth(), image.getHeight(), scale, scale); return image; }
/** * extract clickable links from pdf * * @param pdf the document to parse * @return all detected links */ private Collection<AnchorURL>[] extractPdfLinks(final PDDocument pdf) { @SuppressWarnings("unchecked") List<PDPage> allPages = pdf.getDocumentCatalog().getAllPages(); @SuppressWarnings("unchecked") Collection<AnchorURL>[] linkCollections = (Collection<AnchorURL>[]) new Collection<?>[allPages.size()]; int pagecount = 0; for (PDPage page : allPages) { final Collection<AnchorURL> pdflinks = new ArrayList<AnchorURL>(); try { List<PDAnnotation> annotations = page.getAnnotations(); if (annotations != null) { for (PDAnnotation pdfannotation : annotations) { if (pdfannotation instanceof PDAnnotationLink) { PDAction link = ((PDAnnotationLink) pdfannotation).getAction(); if (link != null && link instanceof PDActionURI) { PDActionURI pdflinkuri = (PDActionURI) link; String uristr = pdflinkuri.getURI(); AnchorURL url = new AnchorURL(uristr); pdflinks.add(url); } } } } } catch (IOException ex) { } linkCollections[pagecount++] = pdflinks; } return linkCollections; }
/** * This will print the documents data. * * @param args The command line arguments. * @throws Exception If there is an error parsing the document. */ public static void main(String[] args) throws Exception { if (args.length != 1) { usage(); } else { PDDocument document = null; try { document = PDDocument.load(args[0]); if (document.isEncrypted()) { try { document.decrypt(""); } catch (InvalidPasswordException e) { System.err.println("Error: Document is encrypted with a password."); System.exit(1); } } PrintTextLocations printer = new PrintTextLocations(); List allPages = document.getDocumentCatalog().getAllPages(); for (int i = 0; i < allPages.size(); i++) { PDPage page = (PDPage) allPages.get(i); System.out.println("Processing page: " + i); PDStream contents = page.getContents(); if (contents != null) { printer.processStream(page, page.findResources(), page.getContents().getStream()); } } } finally { if (document != null) { document.close(); } } } }
public static void main(String[] args) throws IOException { try (PDDocument doc = new PDDocument()) { PDPage page = new PDPage(); // Create a landscape page // page.setMediaBox(new PDRectangle(PDRectangle.A4.getHeight(), // PDRectangle.A4.getWidth())); doc.addPage(page); // Initialize table float margin = 10; float tableWidth = page.getMediaBox().getWidth() - (2 * margin); float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin); float yStart = yStartNewPage; float bottomMargin = 0; // Create the data List<List> data = new ArrayList<>(); data.add(new ArrayList<>(Arrays.asList("Key", "Value"))); for (int i = 1; i <= 5; i++) { data.add(new ArrayList<>(Arrays.asList(String.valueOf(i), "value:" + i))); } BaseTable dataTable = new BaseTable( yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, true, true); DataTable t = new DataTable(dataTable, page); t.addListToTable(data, DataTable.HASHEADER); dataTable.draw(); File file = new File("box.pdf"); System.out.println("Sample file saved at : " + file.getAbsolutePath()); doc.save(file); } }
/** * Returns the given page as an RGB or ARGB image at the given scale. * * @param pageIndex the zero-based index of the page to be converted * @param scale the scaling factor, where 1 = 72 DPI * @param imageType the type of image to return * @return the rendered page image * @throws IOException if the PDF cannot be read */ public BufferedImage renderImage(int pageIndex, float scale, ImageType imageType) throws IOException { PDPage page = document.getPage(pageIndex); PDRectangle cropbBox = page.getCropBox(); float widthPt = cropbBox.getWidth(); float heightPt = cropbBox.getHeight(); int widthPx = Math.round(widthPt * scale); int heightPx = Math.round(heightPt * scale); int rotationAngle = page.getRotation(); // swap width and height BufferedImage image; if (rotationAngle == 90 || rotationAngle == 270) { image = new BufferedImage(heightPx, widthPx, imageType.toBufferedImageType()); } else { image = new BufferedImage(widthPx, heightPx, imageType.toBufferedImageType()); } // use a transparent background if the imageType supports alpha Graphics2D g = image.createGraphics(); if (imageType == ImageType.ARGB) { g.setBackground(new Color(0, 0, 0, 0)); } else { g.setBackground(Color.WHITE); } renderPage(page, g, image.getWidth(), image.getHeight(), scale, scale); g.dispose(); return image; }
// renders a page to the given graphics public void renderPage( PDPage page, Paint paint, Canvas canvas, int width, int height, float scaleX, float scaleY) throws IOException { canvas.scale(scaleX, scaleY); // TODO should we be passing the scale to PageDrawer rather than messing with Graphics? PDRectangle cropBox = page.getCropBox(); int rotationAngle = page.getRotation(); if (rotationAngle != 0) { float translateX = 0; float translateY = 0; switch (rotationAngle) { case 90: translateX = cropBox.getHeight(); break; case 270: translateY = cropBox.getWidth(); break; case 180: translateX = cropBox.getWidth(); translateY = cropBox.getHeight(); break; } canvas.translate(translateX, translateY); canvas.rotate((float) Math.toRadians(rotationAngle)); } PageDrawer drawer = new PageDrawer(page); drawer.drawPage(paint, canvas, cropBox); }
/** * Create a new PDPage content stream. * * @param document The document the page is part of. * @param sourcePage The page to write the contents to. * @param appendContent Indicates whether content will be overwritten. If false all previous * content is deleted. * @param compress Tell if the content stream should compress the page contents. * @throws IOException If there is an error writing to the page contents. */ public PDPageContentStream( PDDocument document, PDPage sourcePage, boolean appendContent, boolean compress) throws IOException { page = sourcePage; resources = page.getResources(); if (resources == null) { resources = new PDResources(); page.setResources(resources); } fonts = resources.getFonts(); xobjects = resources.getImages(); // If request specifies the need to append to the document if (appendContent) { // Get the pdstream from the source page instead of creating a new one PDStream contents = sourcePage.getContents(); // Create a pdstream to append new content PDStream contentsToAppend = new PDStream(document); // This will be the resulting COSStreamArray after existing and new streams are merged COSStreamArray compoundStream = null; // If contents is already an array, a new stream is simply appended to it if (contents.getStream() instanceof COSStreamArray) { compoundStream = (COSStreamArray) contents.getStream(); compoundStream.appendStream(contentsToAppend.getStream()); } else { // Creates the COSStreamArray and adds the current stream plus a new one to it COSArray newArray = new COSArray(); newArray.add(contents.getCOSObject()); newArray.add(contentsToAppend.getCOSObject()); compoundStream = new COSStreamArray(newArray); } if (compress) { List filters = new ArrayList(); filters.add(COSName.FLATE_DECODE); contentsToAppend.setFilters(filters); } // Sets the compoundStream as page contents sourcePage.setContents(new PDStream(compoundStream)); output = contentsToAppend.createOutputStream(); } else { PDStream contents = new PDStream(document); if (compress) { List filters = new ArrayList(); filters.add(COSName.FLATE_DECODE); contents.setFilters(filters); } sourcePage.setContents(contents); output = contents.createOutputStream(); } formatDecimal.setMaximumFractionDigits(10); formatDecimal.setGroupingUsed(false); }
/** Initialises the stream engine for the given page. */ private void initPage(PDPage page) { if (page == null) { throw new IllegalArgumentException("Page cannot be null"); } currentPage = page; graphicsStack.clear(); graphicsStack.push(new PDGraphicsState(page.getCropBox())); textMatrix = null; textLineMatrix = null; resources = null; initialMatrix = page.getMatrix(); }
/** * Renders a given page to an AWT Graphics2D instance. * * @param pageIndex the zero-based index of the page to be converted * @param graphics the Graphics2D on which to draw the page * @param scale the scale to draw the page at * @throws IOException if the PDF cannot be read */ public void renderPageToGraphics(int pageIndex, Graphics2D graphics, float scale) throws IOException { PDPage page = document.getPage(pageIndex); // TODO need width/wight calculations? should these be in PageDrawer? PDRectangle adjustedCropBox = page.getCropBox(); renderPage( page, graphics, (int) adjustedCropBox.getWidth(), (int) adjustedCropBox.getHeight(), scale, scale); }
@SuppressWarnings("unchecked") public static void main_3(String[] args) throws IOException { PDDocument doc = PDDocument.load(iconFile); List<PDPage> pages = doc.getDocumentCatalog().getAllPages(); List<COSObject> objects = doc.getDocument().getObjects(); for (COSObject cosObject : objects) { COSBase cosbase = cosObject.getObject(); if (cosObject.getObject() instanceof COSStream) { COSStream cosstream = (COSStream) cosbase; COSBase filter = cosstream.getDictionaryObject(COSName.FILTER); COSBase subtype = cosstream.getDictionaryObject(COSName.SUBTYPE); if (subtype != null && subtype.equals(COSName.IMAGE)) { System.out.println(filter); InputStream filtered = cosstream.getFilteredStream(); // PDStream stream = new PDStream(costream); System.out.println(Hex.encodeHex(IOUtils.toByteArray(filtered))); } } } for (PDPage pdPage : pages) { PDResources resources = pdPage.getResources(); Map<String, PDXObject> images = resources.getXObjects(); Set<String> keys = images.keySet(); for (String key : keys) { PDXObject image = images.get(key); byte[] imgData = image.getPDStream().getByteArray(); System.out.println(Hex.encodeHex(imgData)); } } }
private PDPageContentStream addPageToDoc(PDDocument doc) throws IOException { PDPage page = new PDPage(); page.setMediaBox(PAGE_SIZE); page.setRotation(IS_LANDSCAPE ? 90 : 0); doc.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(doc, page, false, false); // User transformation matrix to change the reference when drawing. // This is necessary for the landscape position to draw correctly if (IS_LANDSCAPE) { contentStream.transform(new Matrix(0f, 1f, -1f, 0f, PAGE_SIZE.getWidth(), 0f)); } contentStream.setFont(TEXT_FONT, FONT_SIZE); contentStream.setLineWidth(0.25f); return contentStream; }
private static Rectangle2D.Float getRectangle(PDPage page, PDRectangle rect) { float x = rect.getLowerLeftX() - 1; float y = rect.getUpperRightY() - 1; float width = rect.getWidth() + 2; float height = rect.getHeight() + rect.getHeight() / 4; int rotation = page.findRotation(); if (rotation == 0) { PDRectangle pageSize = page.findMediaBox(); y = pageSize.getHeight() - y; } Rectangle2D.Float awtRect = new Rectangle2D.Float(x, y, width, height); return awtRect; }
public void highlight( final Pattern searchText, final Pattern markingPattern, Color color, int pageNr) { if (textCache == null || document == null) { throw new IllegalArgumentException("TextCache was not initialized"); } final List<PDPage> pages = document.getDocumentCatalog().getAllPages(); try { boolean found = false; final PDPage page = pages.get(pageNr - 1); PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true); PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); graphicsState.setNonStrokingAlphaConstant(0.5f); PDResources resources = page.findResources(); Map graphicsStateDictionary = resources.getGraphicsStates(); if (graphicsStateDictionary == null) { // There is no graphics state dictionary in the resources dictionary, create one. graphicsStateDictionary = new TreeMap(); } graphicsStateDictionary.put("highlights", graphicsState); resources.setGraphicsStates(graphicsStateDictionary); for (Match searchMatch : textCache.match(pageNr, searchText)) { if (textCache.match(searchMatch.positions, markingPattern).size() > 0) { for (Match markingMatch : textCache.match(searchMatch.positions, markingPattern)) { if (markupMatch(color, contentStream, markingMatch)) { found = true; } } } else { System.out.println( "Cannot highlight: " + markingPattern.pattern() + " on page " + (pageNr - 1)); } if (found) { break; } } contentStream.close(); } catch (Exception e) { e.printStackTrace(); } catch (Error e1) { e1.printStackTrace(); throw e1; } }
/** * This will initialise and process the contents of the stream. * * @param page the page to process * @throws IOException if there is an error accessing the stream */ public void processPage(PDPage page) throws IOException { initPage(page); if (page.hasContents()) { isProcessingPage = true; processStream(page); isProcessingPage = false; } }
private void _generateImagesPB( FileVersion fileVersion, PDPage pdPage, int dpi, int height, int width, boolean thumbnail, int index) throws Exception { // Generate images RenderedImage renderedImage = pdPage.convertToImage(BufferedImage.TYPE_INT_RGB, PropsValues.DL_FILE_ENTRY_THUMBNAIL_DPI); if (height != 0) { renderedImage = ImageProcessorUtil.scale(renderedImage, width, height); } else { renderedImage = ImageProcessorUtil.scale(renderedImage, width); } // Store images String tempFileId = DLUtil.getTempFileId(fileVersion.getFileEntryId(), fileVersion.getVersion()); File thumbnailTempFile = null; try { if (thumbnail) { thumbnailTempFile = getThumbnailTempFile(tempFileId); thumbnailTempFile.createNewFile(); ImageIO.write(renderedImage, THUMBNAIL_TYPE, new FileOutputStream(thumbnailTempFile)); addFileToStore( fileVersion.getCompanyId(), THUMBNAIL_PATH, getThumbnailFilePath(fileVersion), thumbnailTempFile); } else { thumbnailTempFile = getPreviewTempFile(tempFileId, index); thumbnailTempFile.createNewFile(); ImageIO.write(renderedImage, PREVIEW_TYPE, new FileOutputStream(thumbnailTempFile)); addFileToStore( fileVersion.getCompanyId(), PREVIEW_PATH, getPreviewFilePath(fileVersion, index), thumbnailTempFile); } } finally { FileUtil.delete(thumbnailTempFile); } }
/** * Converts a given page range of a PDF document to bitmap images. * * @param document the PDF document * @param imageFormat the target format (ex. "png") * @param password the password (needed if the PDF is encrypted) * @param startPage the start page (1 is the first page) * @param endPage the end page (set to Integer.MAX_VALUE for all pages) * @param outputPrefix used to construct the filename for the individual images * @param imageType the image type (see {@link BufferedImage}.TYPE_*) * @param resolution the resolution in dpi (dots per inch) * @return true if the images were produced, false if there was an error * @throws IOException if an I/O error occurs */ public boolean writeImage( PDDocument document, String imageFormat, String password, int startPage, int endPage, String outputPrefix, int imageType, int resolution) throws IOException { boolean bSuccess = true; List<PDPage> pages = document.getDocumentCatalog().getAllPages(); int pagesSize = pages.size(); for (int i = startPage - 1; i < endPage && i < pagesSize; i++) { PDPage page = pages.get(i); BufferedImage image = page.convertToImage(imageType, resolution); String fileName = outputPrefix + (i + 1); LOG.info("Writing: " + fileName + "." + imageFormat); bSuccess &= ImageIOUtil.writeImage(image, imageFormat, fileName, imageType, resolution); } return bSuccess; }
/** Pushes the given stream's resources, returning the previous resources. */ private PDResources pushResources(PDContentStream contentStream) { // resource lookup: first look for stream resources, then fallback to the current page PDResources parentResources = resources; PDResources streamResources = contentStream.getResources(); if (streamResources != null) { resources = streamResources; } else if (resources != null) { // inherit directly from parent stream, this is not in the PDF spec, but the file from // PDFBOX-1359 does this and works in Acrobat } else { resources = currentPage.getResources(); } // resources are required in PDF if (resources == null) { resources = new PDResources(); } return parentResources; }
@SuppressWarnings("deprecation") @Override public void generateReport() throws ReportException { int pageNum = 1; // log.info("Report creation started"); // declare data variables List<WarehousePart> records = null; try { records = gateway.fetchWarehouseAndParts(); } catch (GatewayException e) { throw new ReportException("Error in report generation: " + e.getMessage()); } // prep the report page 1 doc = new PDDocument(); PDPage page1 = new PDPage(); PDRectangle rect = page1.getMediaBox(); doc.addPage(page1); PDPage page2 = new PDPage(); PDRectangle rect2 = page2.getMediaBox(); doc.addPage(page2); // get content stream for page 1 PDPageContentStream content = null; PDFont fontPlain = PDType1Font.HELVETICA; PDFont fontBold = PDType1Font.HELVETICA_BOLD; PDFont fontItalic = PDType1Font.HELVETICA_OBLIQUE; PDFont fontMono = PDType1Font.COURIER; PDFont fontMonoBold = PDType1Font.COURIER_BOLD; page1.setRotation(90); float margin = 20; try { content = new PDPageContentStream(doc, page1); // print header of page 1 content.concatenate2CTM(0, 1, -1, 0, 718, 0); content.setNonStrokingColor(Color.CYAN); content.setStrokingColor(Color.BLACK); float bottomY = rect.getHeight() - margin - 100; float headerEndX = rect.getWidth() - margin * 2; content.addRect(margin, bottomY, headerEndX, 100); content.fillAndStroke(); content.setNonStrokingColor(Color.BLACK); // print report title content.setFont(fontBold, 24); content.beginText(); content.newLineAtOffset(margin + 15, bottomY + 15); content.showText("Warehouse Inventory Summary"); content.endText(); // page Number content.setFont(fontBold, 12); content.beginText(); content.newLineAtOffset(710, 150); content.showText("Page " + pageNum); content.endText(); // Date Date date = new Date(); content.setFont(fontBold, 12); content.beginText(); content.newLineAtOffset(30, 150); content.showText(date.toString()); content.endText(); content.setFont(fontMonoBold, 12); float dataY = 610; // colum layout, this might require tweaking // warehouse Name Part # Part Name Quantity Unit float colX_0 = margin + 15; // warehouse name float colX_1 = colX_0 + 180; // Part number float colX_2 = colX_1 + 100; // Part Name float colX_3 = colX_2 + 180; // quantity float colX_4 = colX_3 + 100; // unit // print the colum texts content.beginText(); content.newLineAtOffset(colX_0, dataY); content.showText("Warehouse Name"); content.endText(); content.beginText(); content.newLineAtOffset(colX_1, dataY); content.showText("Part Number"); content.endText(); content.beginText(); content.newLineAtOffset(colX_2, dataY); content.showText("Part Name"); content.endText(); content.beginText(); content.newLineAtOffset(colX_3, dataY); content.showText("Quantity"); content.endText(); content.beginText(); content.newLineAtOffset(colX_4, dataY); content.showText("Unit"); content.endText(); // print the report rows content.setFont(fontMono, 12); int counter = 1; for (WarehousePart wp : records) { // the offset for the current row float offset = dataY - (counter * (fontMono.getHeight(12) + 15)); Warehouse w = wp.getOwner(); Part p = wp.getPart(); content.beginText(); content.newLineAtOffset(colX_0, offset); content.showText(w.getWarehouseName()); content.endText(); content.beginText(); content.newLineAtOffset(colX_1, offset); content.showText("" + p.getPartNumber()); content.endText(); content.beginText(); content.newLineAtOffset(colX_2, offset); content.showText("" + p.getPartName()); content.endText(); content.beginText(); content.newLineAtOffset(colX_3, offset); content.showText("" + wp.getQuantity()); content.endText(); content.beginText(); content.newLineAtOffset(colX_4, offset); content.showText("" + p.getUnitQuanitity()); content.endText(); counter++; if (counter > 25) { content.close(); break; } } content = new PDPageContentStream(doc, page2); content.concatenate2CTM(0, 1, -1, 0, 718, 0); content.setFont(fontMono, 12); page2.setRotation(90); pageNum = 2; int counter2 = 1; // page Number content.setFont(fontBold, 12); content.beginText(); content.newLineAtOffset(710, 150); content.showText("Page " + pageNum); content.endText(); // Date content.setFont(fontBold, 12); content.beginText(); content.newLineAtOffset(30, 150); content.showText(date.toString()); content.endText(); content.setFont(fontMono, 12); for (WarehousePart wp : records.subList(25, records.size())) { // the offset for the current row float offset = dataY - (counter2 * (fontMono.getHeight(12) + 15)); Warehouse w = wp.getOwner(); Part p = wp.getPart(); content.beginText(); content.newLineAtOffset(colX_0, offset); content.showText(w.getWarehouseName()); content.endText(); content.beginText(); content.newLineAtOffset(colX_1, offset); content.showText("" + p.getPartNumber()); content.endText(); content.beginText(); content.newLineAtOffset(colX_2, offset); content.showText("" + p.getPartName()); content.endText(); content.beginText(); content.newLineAtOffset(colX_3, offset); content.showText("" + wp.getQuantity()); content.endText(); content.beginText(); content.newLineAtOffset(colX_4, offset); content.showText("" + p.getUnitQuanitity()); content.endText(); counter2++; } } catch (IOException e) { throw new ReportException("Error in report generation: " + e.getMessage()); } finally { try { content.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // Create a document and add a page to it PDDocument document = new PDDocument(); PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4); // PDPage.PAGE_SIZE_LETTER is also possible PDRectangle rect = page1.getMediaBox(); // rect can be used to get the page width and height document.addPage(page1); // Create a new font object selecting one of the PDF base fonts PDFont fontPlain = PDType1Font.HELVETICA; PDFont fontBold = PDType1Font.HELVETICA_BOLD; PDFont fontItalic = PDType1Font.HELVETICA_OBLIQUE; PDFont fontMono = PDType1Font.COURIER; // Start a new content stream which will "hold" the to be created content PDPageContentStream cos = new PDPageContentStream(document, page1); int line = 1; // Define a text content stream using the selected font, move the cursor and draw some text cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(Color.RED); // width is 100; height is 20 lines down everytime cos.moveTextPositionByAmount(100, rect.getHeight() - 20 * (++line)); cos.drawString("eeey boy"); cos.endText(); cos.beginText(); cos.setFont(fontItalic, 12); cos.setNonStrokingColor(Color.GREEN); cos.moveTextPositionByAmount(100, rect.getHeight() - 20 * (++line)); cos.drawString("How do you do?"); cos.endText(); cos.beginText(); cos.setFont(fontBold, 12); cos.setNonStrokingColor(Color.BLACK); cos.moveTextPositionByAmount(100, rect.getHeight() - 20 * (++line)); cos.drawString("Dit duurde veel langer dan nodig"); cos.endText(); cos.beginText(); cos.setFont(fontMono, 12); cos.setNonStrokingColor(Color.BLACK); cos.moveTextPositionByAmount(100, rect.getHeight() - 20 * (++line)); cos.drawString("maar ik was wat dingentjes aan t uitzoeken."); cos.endText(); // Add some gucci into this place try { BufferedImage awtImage = ImageIO.read(new File("Gucci.jpg")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 0.5f; // alter this value to set the image size cos.drawXObject(ximage, 100, 400, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (IIOException Fnfex) { System.out.println("No image for you"); } // Make sure that the content stream is closed: cos.close(); // New page PDPage page2 = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page2); cos = new PDPageContentStream(document, page2); // draw a red box in the lower left hand corner cos.setNonStrokingColor(Color.RED); cos.fillRect(10, 10, 100, 100); // add two lines of different widths cos.setLineWidth(1); cos.addLine(200, 250, 400, 250); cos.closeAndStroke(); cos.setLineWidth(5); cos.addLine(200, 300, 400, 300); cos.closeAndStroke(); // close the content stream for page 2 cos.close(); // Save the results and ensure that the document is properly closed: document.save("GUCCI.pdf"); document.close(); }
@Override protected void endPage(PDPage page) throws IOException { try { EmbeddedDocumentExtractor extractor = getEmbeddedDocumentExtractor(); for (PDAnnotation annotation : page.getAnnotations()) { if (annotation instanceof PDAnnotationFileAttachment) { PDAnnotationFileAttachment fann = (PDAnnotationFileAttachment) annotation; PDComplexFileSpecification fileSpec = (PDComplexFileSpecification) fann.getFile(); try { extractMultiOSPDEmbeddedFiles(fann.getAttachmentName(), fileSpec, extractor); } catch (SAXException e) { throw new IOExceptionWithCause("file embedded in annotation sax exception", e); } catch (TikaException e) { throw new IOExceptionWithCause("file embedded in annotation tika exception", e); } catch (IOException e) { handleCatchableIOE(e); } } // TODO: remove once PDFBOX-1143 is fixed: if (config.getExtractAnnotationText()) { if (annotation instanceof PDAnnotationLink) { PDAnnotationLink annotationlink = (PDAnnotationLink) annotation; if (annotationlink.getAction() != null) { PDAction action = annotationlink.getAction(); if (action instanceof PDActionURI) { // can't currently associate link to text. // for now, extract link and repeat the link as if it // were the visible text PDActionURI uri = (PDActionURI) action; String link = uri.getURI(); if (link != null && link.trim().length() > 0) { xhtml.startElement("div", "class", "annotation"); xhtml.startElement("a", "href", link); xhtml.characters(link); xhtml.endElement("a"); xhtml.endElement("div"); } } } } if (annotation instanceof PDAnnotationMarkup) { PDAnnotationMarkup annotationMarkup = (PDAnnotationMarkup) annotation; String title = annotationMarkup.getTitlePopup(); String subject = annotationMarkup.getSubject(); String contents = annotationMarkup.getContents(); // TODO: maybe also annotationMarkup.getRichContents()? if (title != null || subject != null || contents != null) { xhtml.startElement("div", "class", "annotation"); if (title != null) { xhtml.startElement("div", "class", "annotationTitle"); xhtml.characters(title); xhtml.endElement("div"); } if (subject != null) { xhtml.startElement("div", "class", "annotationSubject"); xhtml.characters(subject); xhtml.endElement("div"); } if (contents != null) { xhtml.startElement("div", "class", "annotationContents"); xhtml.characters(contents); xhtml.endElement("div"); } xhtml.endElement("div"); } } } } if (config.getOCRStrategy().equals(PDFParserConfig.OCR_STRATEGY.OCR_AND_TEXT_EXTRACTION)) { doOCROnCurrentPage(); } xhtml.endElement("div"); } catch (SAXException | TikaException e) { throw new IOExceptionWithCause("Unable to end a page", e); } catch (IOException e) { exceptions.add(e); } finally { pageIndex++; } }
// renders a page to the given graphics private void renderPage( PDPage page, Graphics2D graphics, int width, int height, float scaleX, float scaleY) throws IOException { graphics.clearRect(0, 0, width, height); graphics.scale(scaleX, scaleY); // TODO should we be passing the scale to PageDrawer rather than messing with Graphics? PDRectangle cropBox = page.getCropBox(); int rotationAngle = page.getRotation(); if (rotationAngle != 0) { float translateX = 0; float translateY = 0; switch (rotationAngle) { case 90: translateX = cropBox.getHeight(); break; case 270: translateY = cropBox.getWidth(); break; case 180: translateX = cropBox.getWidth(); translateY = cropBox.getHeight(); break; } graphics.translate(translateX, translateY); graphics.rotate((float) Math.toRadians(rotationAngle)); } // the end-user may provide a custom PageDrawer PageDrawerParameters parameters = new PageDrawerParameters(this, page); PageDrawer drawer = createPageDrawer(parameters); drawer.drawPage(graphics, cropBox); }