List of usage examples for org.apache.pdfbox.pdmodel PDPage PDPage
public PDPage(COSDictionary pageDictionary)
From source file:com.evanbelcher.DrillBook.display.DBDesktopPane.java
License:Open Source License
/** * Prints the current page to a pdf file * * @throws IOException if the file cannot be found or the pdf cannot be created *///from ww w . ja va2 s . c om protected void printCurrentPageToPdf() throws IOException { io.clearActivePoints(); ddf.updateAll(io.getActivePoints()); String fileName = DBMenuBar.cleanseFileName( Main.getState().getCurrentFileName().substring(0, Main.getState().getCurrentFileName().length() - 6) + ": " + Main.getCurrentPage().toDisplayString().replaceAll("\\|", "-")); File f = new File(Main.getFilePath()); f.mkdirs(); f = new File(Main.getFilePath() + fileName + ".pdf"); BufferedImage bi = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB); Graphics g = bi.createGraphics(); paintComponent(g); g.dispose(); PDDocument doc = null; try { doc = new PDDocument(); boolean crop = true; State.print(field); for (Point p : Main.getCurrentPage().getDots().keySet()) if (p.getX() < field.getWidth() * 0.1 + field.getX() || p.getX() > field.getWidth() * 0.9 + field.getX()) { crop = false; break; } if (Main.getCurrentPage().getTextPoint().getX() < field.getWidth() * 0.1 + field.getX() || Main.getCurrentPage().getTextPoint().getX() + 100 > field.getWidth() * 0.9 + field.getX()) crop = false; float scale = 1.0f; if (crop) scale = 0.8f; PDPage page = new PDPage(new PDRectangle((float) field.getWidth() * scale, (float) field.getHeight())); doc.addPage(page); PDImageXObject pdImage = LosslessFactory.createFromImage(doc, bi); PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true); contentStream.drawImage(pdImage, -1 * field.x - (float) (((1 - scale) / 2.0f) * field.getWidth()), -1 * field.y, pdImage.getWidth(), pdImage.getHeight()); contentStream.close(); doc.save(f); } finally { if (doc != null) doc.close(); } }
From source file:com.evanbelcher.DrillBook.display.DBDesktopPane.java
License:Open Source License
/** * Prints every page to a pdf file//from www . j av a2 s . c om * * @throws IOException if the file cannot be found or the pdf cannot be created */ protected void printAllPagesToPdf() throws IOException { io.clearActivePoints(); ddf.updateAll(io.getActivePoints()); File f = new File(Main.getFilePath()); f.mkdirs(); String fileName = DBMenuBar.cleanseFileName(Main.getState().getCurrentFileName().substring(0, Main.getState().getCurrentFileName().length() - 6)); f = new File(Main.getFilePath() + fileName + " full show" + ".pdf"); boolean crop = true; a: for (int pageNum : Main.getPages().keySet()) { for (Point p : Main.getPages().get(pageNum).getDots().keySet()) { if (p.getX() < field.getWidth() * 0.1 + field.getX() || p.getX() > field.getWidth() * 0.9 + field.getX()) { crop = false; break a; } } if (Main.getPages().get(pageNum).getTextPoint().getX() < field.getWidth() * 0.1 + field.getX() || Main.getPages().get(pageNum).getTextPoint().getX() + 100 > field.getWidth() * 0.9 + field.getX()) { crop = false; break; } } float scale = 1.0f; if (crop) scale = 0.8f; PDDocument doc = null; try { doc = new PDDocument(); for (int i : Main.getPages().keySet()) { Main.setCurrentPage(i); BufferedImage bi = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB); Graphics g = bi.createGraphics(); paintComponent(g); g.dispose(); PDPage page = new PDPage( new PDRectangle((float) field.getWidth() * scale, (float) field.getHeight())); doc.addPage(page); PDImageXObject pdImage = LosslessFactory.createFromImage(doc, bi); PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true); contentStream.drawImage(pdImage, -1 * field.x - (float) (((1 - scale) / 2.0f) * field.getWidth()), -1 * field.y, pdImage.getWidth(), pdImage.getHeight()); contentStream.close(); } doc.save(f); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (doc != null) doc.close(); pdf.updateAfterPrintAll(); } }
From source file:com.fangxin365.core.utils.PDFMerger.java
License:Apache License
/** * append all pages from source to destination. * /*from ww w.jav a 2 s . com*/ * @param destination * the document to receive the pages * @param source * the document originating the new pages * * @throws IOException * If there is an error accessing data from either document. */ public void appendDocument(PDDocument destination, PDDocument source) throws IOException { if (destination.isEncrypted()) { System.out.println("Error: destination PDF is encrypted, can't append encrypted PDF documents."); } if (source.isEncrypted()) { System.out.println("Error: source PDF is encrypted, can't append encrypted PDF documents."); } PDDocumentInformation destInfo = destination.getDocumentInformation(); PDDocumentInformation srcInfo = source.getDocumentInformation(); destInfo.getDictionary().mergeInto(srcInfo.getDictionary()); PDDocumentCatalog destCatalog = destination.getDocumentCatalog(); PDDocumentCatalog srcCatalog = source.getDocumentCatalog(); // use the highest version number for the resulting pdf float destVersion = destination.getDocument().getVersion(); float srcVersion = source.getDocument().getVersion(); if (destVersion < srcVersion) { destination.getDocument().setVersion(srcVersion); } if (destCatalog.getOpenAction() == null) { destCatalog.setOpenAction(srcCatalog.getOpenAction()); } // maybe there are some shared resources for all pages COSDictionary srcPages = (COSDictionary) srcCatalog.getCOSDictionary().getDictionaryObject(COSName.PAGES); COSDictionary srcResources = (COSDictionary) srcPages.getDictionaryObject(COSName.RESOURCES); COSDictionary destPages = (COSDictionary) destCatalog.getCOSDictionary().getDictionaryObject(COSName.PAGES); COSDictionary destResources = (COSDictionary) destPages.getDictionaryObject(COSName.RESOURCES); if (srcResources != null) { if (destResources != null) { destResources.mergeInto(srcResources); } else { destPages.setItem(COSName.RESOURCES, srcResources); } } PDFCloneUtility cloner = new PDFCloneUtility(destination); try { PDAcroForm destAcroForm = destCatalog.getAcroForm(); PDAcroForm srcAcroForm = srcCatalog.getAcroForm(); if (destAcroForm == null) { cloner.cloneForNewDocument(srcAcroForm); destCatalog.setAcroForm(srcAcroForm); } else { if (srcAcroForm != null) { mergeAcroForm(cloner, destAcroForm, srcAcroForm); } } } catch (Exception e) { // if we are not ignoring exceptions, we'll re-throw this if (!ignoreAcroFormErrors) { throw (IOException) e; } } COSArray destThreads = (COSArray) destCatalog.getCOSDictionary().getDictionaryObject(COSName.THREADS); COSArray srcThreads = (COSArray) cloner .cloneForNewDocument(destCatalog.getCOSDictionary().getDictionaryObject(COSName.THREADS)); if (destThreads == null) { destCatalog.getCOSDictionary().setItem(COSName.THREADS, srcThreads); } else { destThreads.addAll(srcThreads); } PDDocumentNameDictionary destNames = destCatalog.getNames(); PDDocumentNameDictionary srcNames = srcCatalog.getNames(); if (srcNames != null) { if (destNames == null) { destCatalog.getCOSDictionary().setItem(COSName.NAMES, cloner.cloneForNewDocument(srcNames)); } else { cloner.cloneMerge(srcNames, destNames); } } PDDocumentOutline destOutline = destCatalog.getDocumentOutline(); PDDocumentOutline srcOutline = srcCatalog.getDocumentOutline(); if (srcOutline != null) { if (destOutline == null) { PDDocumentOutline cloned = new PDDocumentOutline( (COSDictionary) cloner.cloneForNewDocument(srcOutline)); destCatalog.setDocumentOutline(cloned); } else { PDOutlineItem first = srcOutline.getFirstChild(); if (first != null) { PDOutlineItem clonedFirst = new PDOutlineItem( (COSDictionary) cloner.cloneForNewDocument(first)); destOutline.appendChild(clonedFirst); } } } String destPageMode = destCatalog.getPageMode(); String srcPageMode = srcCatalog.getPageMode(); if (destPageMode == null) { destCatalog.setPageMode(srcPageMode); } COSDictionary destLabels = (COSDictionary) destCatalog.getCOSDictionary() .getDictionaryObject(COSName.PAGE_LABELS); COSDictionary srcLabels = (COSDictionary) srcCatalog.getCOSDictionary() .getDictionaryObject(COSName.PAGE_LABELS); if (srcLabels != null) { int destPageCount = destination.getNumberOfPages(); COSArray destNums = null; if (destLabels == null) { destLabels = new COSDictionary(); destNums = new COSArray(); destLabels.setItem(COSName.NUMS, destNums); destCatalog.getCOSDictionary().setItem(COSName.PAGE_LABELS, destLabels); } else { destNums = (COSArray) destLabels.getDictionaryObject(COSName.NUMS); } COSArray srcNums = (COSArray) srcLabels.getDictionaryObject(COSName.NUMS); if (srcNums != null) { for (int i = 0; i < srcNums.size(); i += 2) { COSNumber labelIndex = (COSNumber) srcNums.getObject(i); long labelIndexValue = labelIndex.intValue(); destNums.add(COSInteger.get(labelIndexValue + destPageCount)); destNums.add(cloner.cloneForNewDocument(srcNums.getObject(i + 1))); } } } COSStream destMetadata = (COSStream) destCatalog.getCOSDictionary().getDictionaryObject(COSName.METADATA); COSStream srcMetadata = (COSStream) srcCatalog.getCOSDictionary().getDictionaryObject(COSName.METADATA); if (destMetadata == null && srcMetadata != null) { PDStream newStream = new PDStream(destination, srcMetadata.getUnfilteredStream(), false); newStream.getStream().mergeInto(srcMetadata); newStream.addCompression(); destCatalog.getCOSDictionary().setItem(COSName.METADATA, newStream); } // finally append the pages @SuppressWarnings("unchecked") List<PDPage> pages = srcCatalog.getAllPages(); Iterator<PDPage> pageIter = pages.iterator(); while (pageIter.hasNext()) { PDPage page = pageIter.next(); PDPage newPage = new PDPage((COSDictionary) cloner.cloneForNewDocument(page.getCOSDictionary())); newPage.setCropBox(page.findCropBox()); newPage.setMediaBox(page.findMediaBox()); newPage.setRotation(page.findRotation()); destination.addPage(newPage); } }
From source file:com.fileOperations.ImageToPDF.java
/** * create PDF from image and scales it accordingly to fit in a single page * * @param folderPath String// w ww. j a v a2 s. c o m * @param imageFileName String * @return String new filename */ public static String createPDFFromImage(String folderPath, String imageFileName) { String pdfFile = FilenameUtils.removeExtension(imageFileName) + ".pdf"; String image = folderPath + imageFileName; PDImageXObject pdImage = null; File imageFile = null; FileInputStream fileStream = null; BufferedImage bim = null; File attachmentLocation = new File(folderPath); if (!attachmentLocation.exists()) { attachmentLocation.mkdirs(); } // the document PDDocument doc = null; PDPageContentStream contentStream = null; try { doc = new PDDocument(); PDPage page = new PDPage(PDRectangle.LETTER); float margin = 72; float pageWidth = page.getMediaBox().getWidth() - 2 * margin; float pageHeight = page.getMediaBox().getHeight() - 2 * margin; if (image.toLowerCase().endsWith(".jpg")) { imageFile = new File(image); fileStream = new FileInputStream(image); pdImage = JPEGFactory.createFromStream(doc, fileStream); // } else if ((image.toLowerCase().endsWith(".tif") // || image.toLowerCase().endsWith(".tiff")) // && TIFFCompression(image) == COMPRESSION_GROUP4) { // imageFile = new File(image); // pdImage = CCITTFactory.createFromFile(doc, imageFile); } else if (image.toLowerCase().endsWith(".gif") || image.toLowerCase().endsWith(".bmp") || image.toLowerCase().endsWith(".png")) { imageFile = new File(image); bim = ImageIO.read(imageFile); pdImage = LosslessFactory.createFromImage(doc, bim); } if (pdImage != null) { if (pdImage.getWidth() > pdImage.getHeight()) { page.setRotation(270); PDRectangle rotatedPage = new PDRectangle(page.getMediaBox().getHeight(), page.getMediaBox().getWidth()); page.setMediaBox(rotatedPage); pageWidth = page.getMediaBox().getWidth() - 2 * margin; pageHeight = page.getMediaBox().getHeight() - 2 * margin; } Dimension pageSize = new Dimension((int) pageWidth, (int) pageHeight); Dimension imageSize = new Dimension(pdImage.getWidth(), pdImage.getHeight()); Dimension scaledDim = PDFBoxTools.getScaledDimension(imageSize, pageSize); float startX = page.getMediaBox().getLowerLeftX() + margin; float startY = page.getMediaBox().getUpperRightY() - margin - scaledDim.height; doc.addPage(page); contentStream = new PDPageContentStream(doc, page); contentStream.drawImage(pdImage, startX, startY, scaledDim.width, scaledDim.height); contentStream.close(); doc.save(folderPath + pdfFile); } } catch (IOException ex) { ExceptionHandler.Handle(ex); return ""; } finally { if (doc != null) { try { doc.close(); } catch (IOException ex) { ExceptionHandler.Handle(ex); return ""; } } } if (fileStream != null) { try { fileStream.close(); } catch (IOException ex) { ExceptionHandler.Handle(ex); return ""; } } if (bim != null) { bim.flush(); } if (imageFile != null) { imageFile.delete(); } return pdfFile; }
From source file:com.fileOperations.ImageToPDF.java
/** * create PDF from image and scales it accordingly to fit in a single page * * @param folderPath String/* w w w. ja va 2 s. c o m*/ * @param imageFileName String * @return String new filename */ public static String createPDFFromImageNoDelete(String folderPath, String imageFileName) { String pdfFile = FilenameUtils.removeExtension(imageFileName) + ".pdf"; String image = folderPath + imageFileName; PDImageXObject pdImage = null; File imageFile = null; FileInputStream fileStream = null; BufferedImage bim = null; File attachmentLocation = new File(folderPath); if (!attachmentLocation.exists()) { attachmentLocation.mkdirs(); } // the document PDDocument doc = null; PDPageContentStream contentStream = null; try { doc = new PDDocument(); PDPage page = new PDPage(PDRectangle.LETTER); float margin = 72; float pageWidth = page.getMediaBox().getWidth() - 2 * margin; float pageHeight = page.getMediaBox().getHeight() - 2 * margin; if (image.toLowerCase().endsWith(".jpg")) { imageFile = new File(image); fileStream = new FileInputStream(image); pdImage = JPEGFactory.createFromStream(doc, fileStream); // } else if ((image.toLowerCase().endsWith(".tif") // || image.toLowerCase().endsWith(".tiff")) // && TIFFCompression(image) == COMPRESSION_GROUP4) { // imageFile = new File(image); // pdImage = CCITTFactory.createFromFile(doc, imageFile); } else if (image.toLowerCase().endsWith(".gif") || image.toLowerCase().endsWith(".bmp") || image.toLowerCase().endsWith(".png")) { imageFile = new File(image); bim = ImageIO.read(imageFile); pdImage = LosslessFactory.createFromImage(doc, bim); } if (pdImage != null) { if (pdImage.getWidth() > pdImage.getHeight()) { page.setRotation(270); PDRectangle rotatedPage = new PDRectangle(page.getMediaBox().getHeight(), page.getMediaBox().getWidth()); page.setMediaBox(rotatedPage); pageWidth = page.getMediaBox().getWidth() - 2 * margin; pageHeight = page.getMediaBox().getHeight() - 2 * margin; } Dimension pageSize = new Dimension((int) pageWidth, (int) pageHeight); Dimension imageSize = new Dimension(pdImage.getWidth(), pdImage.getHeight()); Dimension scaledDim = PDFBoxTools.getScaledDimension(imageSize, pageSize); float startX = page.getMediaBox().getLowerLeftX() + margin; float startY = page.getMediaBox().getUpperRightY() - margin - scaledDim.height; doc.addPage(page); contentStream = new PDPageContentStream(doc, page); contentStream.drawImage(pdImage, startX, startY, scaledDim.width, scaledDim.height); contentStream.close(); doc.save(folderPath + pdfFile); } } catch (IOException ex) { ExceptionHandler.Handle(ex); return ""; } finally { if (doc != null) { try { doc.close(); } catch (IOException ex) { ExceptionHandler.Handle(ex); return ""; } } } if (fileStream != null) { try { fileStream.close(); } catch (IOException ex) { ExceptionHandler.Handle(ex); return ""; } } if (bim != null) { bim.flush(); } return pdfFile; }
From source file:com.helger.pdflayout.element.PLPageSet.java
License:Apache License
/** * Render all pages of this layout to the specified PDDocument * * @param aPrepareResult/*from w w w. j a v a 2 s . co m*/ * The preparation result. May not be <code>null</code>. * @param aDoc * The PDDocument. May not be <code>null</code>. * @param bDebug * <code>true</code> for debug output * @param nPageSetIndex * Page set index. Always ≥ 0. * @param nTotalPageStartIndex * Total page index. Always ≥ 0. * @param nTotalPageCount * Total page count. Always ≥ 0. * @throws IOException * In case of render errors */ public void renderAllPages(@Nonnull final PageSetPrepareResult aPrepareResult, @Nonnull final PDDocument aDoc, final boolean bDebug, @Nonnegative final int nPageSetIndex, @Nonnegative final int nTotalPageStartIndex, @Nonnegative final int nTotalPageCount) throws IOException { // Start at the left final float fXLeft = getMarginLeft() + getPaddingLeft(); final float fYTop = getYTop(); final boolean bCompressPDF = !bDebug; int nPageIndex = 0; final int nPageCount = aPrepareResult.getPageCount(); for (final List<PLElementWithSize> aPerPage : aPrepareResult.directGetPerPageElements()) { if (PLDebug.isDebugRender()) PLDebug.debugRender(this, "Start rendering page index " + nPageIndex + " (" + (nTotalPageStartIndex + nPageIndex) + ") with page size " + getPageWidth() + " & " + getPageHeight() + " and available size " + getAvailableWidth() + " & " + getAvailableHeight()); final RenderPageIndex aPageIndex = new RenderPageIndex(nPageSetIndex, nPageIndex, nPageCount, nTotalPageStartIndex + nPageIndex, nTotalPageCount); // Layout in memory final PDPage aPage = new PDPage(m_aPageSize.getAsRectangle()); aDoc.addPage(aPage); { final PageSetupContext aCtx = new PageSetupContext(aDoc, aPage); if (m_aPageHeader != null) m_aPageHeader.doPageSetup(aCtx); for (final PLElementWithSize aElement : aPerPage) aElement.getElement().doPageSetup(aCtx); if (m_aPageFooter != null) m_aPageFooter.doPageSetup(aCtx); } final PDPageContentStreamWithCache aContentStream = new PDPageContentStreamWithCache(aDoc, aPage, false, bCompressPDF); try { // Page rect before content - debug: red { final float fLeft = getMarginLeft(); final float fTop = m_aPageSize.getHeight() - getMarginTop(); final float fWidth = m_aPageSize.getWidth() - getMarginXSum(); final float fHeight = m_aPageSize.getHeight() - getMarginYSum(); // Fill before border if (getFillColor() != null) { aContentStream.setNonStrokingColor(getFillColor()); aContentStream.fillRect(fLeft, fTop - fHeight, fWidth, fHeight); } BorderSpec aRealBorder = getBorder(); if (shouldApplyDebugBorder(aRealBorder, bDebug)) aRealBorder = new BorderSpec(new BorderStyleSpec(PLDebug.BORDER_COLOR_PAGESET)); if (aRealBorder.hasAnyBorder()) renderBorder(aContentStream, fLeft, fTop, fWidth, fHeight, aRealBorder); } // Start with the page rect if (m_aPageHeader != null) { // Page header does not care about page padding // header top-left final RenderingContext aRC = new RenderingContext(ERenderingElementType.PAGE_HEADER, aContentStream, bDebug, getMarginLeft() + m_aPageHeader.getMarginLeft(), m_aPageSize.getHeight() - m_aPageHeader.getMarginTop(), m_aPageSize.getWidth() - getMarginXSum() - m_aPageHeader.getMarginXSum(), aPrepareResult.getHeaderHeight() + m_aPageHeader.getPaddingYSum()); aPageIndex.setPlaceholdersInRenderingContext(aRC); if (m_aRCCustomizer != null) m_aRCCustomizer.customizeRenderingContext(aRC); m_aPageHeader.perform(aRC); } float fCurY = fYTop; for (final PLElementWithSize aElementWithHeight : aPerPage) { final AbstractPLElement<?> aElement = aElementWithHeight.getElement(); // Get element height final float fThisHeight = aElementWithHeight.getHeight(); final float fThisHeightWithPadding = fThisHeight + aElement.getPaddingYSum(); final RenderingContext aRC = new RenderingContext(ERenderingElementType.CONTENT_ELEMENT, aContentStream, bDebug, fXLeft + aElement.getMarginLeft(), fCurY - aElement.getMarginTop(), getAvailableWidth() - aElement.getMarginXSum(), fThisHeightWithPadding); aPageIndex.setPlaceholdersInRenderingContext(aRC); if (m_aRCCustomizer != null) m_aRCCustomizer.customizeRenderingContext(aRC); aElement.perform(aRC); fCurY -= fThisHeightWithPadding + aElement.getMarginYSum(); } if (m_aPageFooter != null) { // Page footer does not care about page padding // footer top-left final float fStartLeft = getMarginLeft() + m_aPageFooter.getMarginLeft(); final float fStartTop = getMarginBottom() - m_aPageFooter.getMarginTop(); final float fWidth = m_aPageSize.getWidth() - getMarginXSum() - m_aPageFooter.getMarginXSum(); final float fHeight = aPrepareResult.getFooterHeight() + m_aPageFooter.getPaddingYSum(); final RenderingContext aRC = new RenderingContext(ERenderingElementType.PAGE_FOOTER, aContentStream, bDebug, fStartLeft, fStartTop, fWidth, fHeight); aPageIndex.setPlaceholdersInRenderingContext(aRC); if (m_aRCCustomizer != null) m_aRCCustomizer.customizeRenderingContext(aRC); m_aPageFooter.perform(aRC); } } finally { aContentStream.close(); } ++nPageIndex; } if (PLDebug.isDebugRender()) PLDebug.debugRender(this, "Finished rendering"); }
From source file:com.jaromin.alfresco.repo.content.transform.CorelDrawContentTransformer.java
License:Apache License
/** * //from w ww.j a v a2 s.c o m * @param pageImages * @param out * @throws IOException * @throws FileNotFoundException * @throws COSVisitorException */ private void buildPdfFromImages(Map<File, Dimension> pageImages, OutputStream out) throws IOException, FileNotFoundException, COSVisitorException { PDDocument doc = new PDDocument(); for (Map.Entry<File, Dimension> entry : pageImages.entrySet()) { File pFile = entry.getKey(); Dimension d = entry.getValue(); PDRectangle size = new PDRectangle(d.width, d.height); PDPage page = new PDPage(size); doc.addPage(page); PDXObjectImage ximage = new PDJpeg(doc, new FileInputStream(pFile)); PDPageContentStream contentStream = new PDPageContentStream(doc, page); contentStream.drawImage(ximage, size.getLowerLeftX(), size.getLowerLeftY()); contentStream.close(); } doc.save(out); }
From source file:com.PDF.Resume.java
public ByteArrayOutputStream createResume() throws ParseException, IOException, COSVisitorException { System.out.println(imagePath); // 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);//from ww w. j a v a2 s. c om // Create a new font object selecting one of the PDF base fonts PDFont fontPlain = PDType1Font.HELVETICA; // Start a new content stream which will "hold" the to be created content PDPageContentStream cos = new PDPageContentStream(document, page1); int line = 0; // Define a text content stream using the selected font, move the cursor and draw some text cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(475, rect.getHeight() - 800 * (++line)); cos.drawString("Pagina " + ++pagenumber); cos.endText(); line--; cos.beginText(); cos.setFont(fontPlain, 24); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 90 * (++line)); cos.drawString("Curriculum Vitae"); cos.endText(); if (user != null) { cos.beginText(); cos.setFont(fontPlain, 24); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 90 * (++line)); cos.drawString(user.getSurname() + ""); cos.endText(); } // add an image try { BufferedImage awtImage = ImageIO.read(new File( "C:\\ICT\\HBO\\Jaar 2\\Project Enterprise Web Apps\\Images\\Info-Support-klein-formaat-JPG.png")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 1f; // alter this value to set the image size cos.drawXObject(ximage, 350, 750, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (FileNotFoundException fnfex) { System.out.println("No image for you"); } // Make sure that the content stream is closed: cos.close(); //second page line = 0; PDPage page2 = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page2); cos = new PDPageContentStream(document, page2); try { BufferedImage awtImage = ImageIO.read(new File( "C:\\ICT\\HBO\\Jaar 2\\Project Enterprise Web Apps\\Images\\Info-Support-klein-formaat-JPG.png")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 1f; // alter this value to set the image size cos.drawXObject(ximage, 350, 750, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (FileNotFoundException fnfex) { System.out.println("No image for you"); } cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(475, rect.getHeight() - 800 * (++line)); cos.drawString("Pagina " + ++pagenumber); cos.endText(); line--; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 90 * (++line)); cos.drawString("Personalia"); cos.endText(); line += 4; cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Naam:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getSurname() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Geslacht:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getGender() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Telefoonnummer:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getPersonalPhone() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Emailadres:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getPersonalEmail() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Adres:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getAddress() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Postcode:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getZipCode() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Stad:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getCity() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Provincie:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getRegion() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Functie:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getFunction() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Business Unit:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getBusinessUnitAsString() + ""); cos.endText(); if (!educationList.isEmpty()) { line++; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Opleidingen"); cos.endText(); for (EmployeeHasEducation e : educationList) { cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("School:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getEducation().getSchool() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Richting:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getEducation().getDegree() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Niveau:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getEducation().getGrade() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Begindatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getStartDate().substring(0, 10) + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Einddatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getEndDate().substring(0, 10) + ""); cos.endText(); } } if (!certificateList.isEmpty()) { line++; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Certificaten"); cos.endText(); for (Certificate c : certificateList) { cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Certificaat naam:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getName() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Certificaat plaats:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getCertificationAuthority() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Licentienummer:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getLicenseNumber() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Startdatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getStartDate().substring(0, 10) + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Einddatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getExpirationDate().substring(0, 10) + ""); cos.endText(); line++; } line++; } cos.close(); if (!preceedings.isEmpty()) { line = 0; PDPage page3 = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page3); cos = new PDPageContentStream(document, page3); try { BufferedImage awtImage = ImageIO.read(new File( "C:\\ICT\\HBO\\Jaar 2\\Project Enterprise Web Apps\\Images\\Info-Support-klein-formaat-JPG.png")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 1f; // alter this value to set the image size cos.drawXObject(ximage, 350, 750, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (FileNotFoundException fnfex) { System.out.println("No image for you"); } cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(475, rect.getHeight() - 800 * (++line)); cos.drawString("Pagina " + ++pagenumber); cos.endText(); line--; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 100 * (++line)); cos.drawString("Projecten"); line += 4; cos.endText(); for (Preceeding p : preceedings) { cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Projectnaam:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getProject().getName() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Bedrijfsnaam:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getProject().getCompany().getName() + " "); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Project rol:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getDescription() + ""); cos.endText(); // } cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Startdatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getProject().getStartDate() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Einddatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getProject().getEndDate() + ""); cos.endText(); line++; } } cos.close(); //Skills of the user if (!skillList.isEmpty()) { line = 0; PDPage page3 = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page3); cos = new PDPageContentStream(document, page3); try { BufferedImage awtImage = ImageIO.read(new File( "C:\\ICT\\HBO\\Jaar 2\\Project Enterprise Web Apps\\Images\\Info-Support-klein-formaat-JPG.png")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 1f; // alter this value to set the image size cos.drawXObject(ximage, 350, 750, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (FileNotFoundException fnfex) { System.out.println("No image for you"); } cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(475, rect.getHeight() - 800 * (++line)); cos.drawString("Pagina " + ++pagenumber); cos.endText(); line--; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 100 * (++line)); cos.drawString("Skills"); cos.endText(); line += 4; cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Skill & Rating:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Jaren ervaring:"); cos.endText(); line -= 2; for (SkillRating s : skillList) { cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (++line)); cos.drawString(s.getSkill().getName() + " niveau " + s.getRating()); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (++line)); cos.drawString("" + s.getExpYears()); cos.endText(); line++; } } cos.close(); ByteArrayOutputStream output = new ByteArrayOutputStream(); document.save(output); document.close(); return output; }
From source file:com.vigglet.pdf.PdfPage.java
private PDPage getPage(PDRectangle pageSize) { return new PDPage(pageSize); }
From source file:controldeadministradores.Admin.java
public void crearPDF(String file, String body) throws Exception { String outputFileName = file + ".pdf"; // if (args.length > 0) // outputFileName = args[0]; // 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);/*ww w . j av a 2s. c o m*/ // 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 = 0; // Define a text content stream using the selected font, move the cursor and draw some text cos.beginText(); cos.setFont(fontPlain, 14); cos.moveTextPositionByAmount(100, rect.getHeight() - 50 * (++line)); cos.drawString("Reporte de " + file); cos.endText(); String[] txtLine = body.split("-"); for (int k = 0; k < txtLine.length; k++) { cos.beginText(); cos.setFont(fontPlain, 12); cos.moveTextPositionByAmount(100, rect.getHeight() - 50 * (++line)); cos.drawString(txtLine[k]); cos.endText(); cos.close(); } // Make sure that the content stream is closed: // Save the results and ensure that the document is properly closed: document.save(outputFileName); document.close(); }