List of usage examples for com.lowagie.text.pdf PdfReader PdfReader
public PdfReader(PdfReader reader)
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void handouts(String file) { try {/*from w w w . j a v a 2 s . c o m*/ File src = new File(selectedFile); File dest = new File(file); int pages = 4; float x1 = 30f; float x2 = 280f; float x3 = 320f; float x4 = 565f; float[] y1 = new float[pages]; float[] y2 = new float[pages]; float height = (778f - (20f * (pages - 1))) / pages; y1[0] = 812f; y2[0] = 812f - height; for (int i = 1; i < pages; i++) { y1[i] = y2[i - 1] - 20f; y2[i] = y1[i] - height; } // we create a reader for a certain document PdfReader reader = new PdfReader(src.getAbsolutePath()); // we retrieve the total number of pages int n = reader.getNumberOfPages(); // step 1: creation of a document-object Document document = new Document(PageSize.A4); // step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); // step 3: we open the document document.open(); PdfContentByte cb = writer.getDirectContent(); PdfImportedPage page; int rotation; int i = 0; int p = 0; // step 4: we add content while (i < n) { i++; Rectangle rect = reader.getPageSizeWithRotation(i); float factorx = (x2 - x1) / rect.width(); float factory = (y1[p] - y2[p]) / rect.height(); float factor = (factorx < factory ? factorx : factory); float dx = (factorx == factor ? 0f : ((x2 - x1) - rect.width() * factor) / 2f); float dy = (factory == factor ? 0f : ((y1[p] - y2[p]) - rect.height() * factor) / 2f); page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { cb.addTemplate(page, 0, -factor, factor, 0, x1 + dx, y2[p] + dy + rect.height() * factor); } else { cb.addTemplate(page, factor, 0, 0, factor, x1 + dx, y2[p] + dy); } cb.setRGBColorStroke(0xC0, 0xC0, 0xC0); cb.rectangle(x3 - 5f, y2[p] - 5f, x4 - x3 + 10f, y1[p] - y2[p] + 10f); for (float l = y1[p] - 19; l > y2[p]; l -= 16) { cb.moveTo(x3, l); cb.lineTo(x4, l); } cb.rectangle(x1 + dx, y2[p] + dy, rect.width() * factor, rect.height() * factor); cb.stroke(); p++; if (p == pages) { p = 0; document.newPage(); } } // step 5: we close the document document.close(); } catch (Exception e) { System.err.println(e.getMessage()); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void add(int pageCount, PdfPageData currentPageData, InsertBlankPDFPage addPage) { File tempFile = null;//from w w w . j a v a 2 s . c o m try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } int pageToInsertBefore = addPage.getInsertBefore(); boolean insertAsLastPage = false; if (pageToInsertBefore == -1) return; else if (pageToInsertBefore == -2) insertAsLastPage = true; try { PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); if (insertAsLastPage) stamp.insertPage(pageCount + 1, reader.getPageSizeWithRotation(pageCount)); else stamp.insertPage(pageToInsertBefore, reader.getPageSizeWithRotation(pageToInsertBefore)); stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void rotate(int pageCount, PdfPageData currentPageData, RotatePDFPages current_selection) { File tempFile = null;//w w w . j ava 2s .c o m try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToRotate = current_selection.getRotatedPages(); if (pgsToRotate == null) return; int check = -1; if (pgsToRotate.length == 1) { check = currentGUI.showConfirmDialog(Messages.getMessage("PdfViewerMessage.ConfirmRotatePages"), Messages.getMessage("PdfViewerMessage.Confirm"), JOptionPane.YES_NO_OPTION); } else { check = currentGUI.showConfirmDialog(Messages.getMessage("PdfViewerMessage.ConfirmRotatePages"), Messages.getMessage("PdfViewerMessage.Confirm"), JOptionPane.YES_NO_OPTION); } if (check != 0) return; if (pgsToRotate == null) return; List pagesToRotate = new ArrayList(); for (int i = 0; i < pgsToRotate.length; i++) pagesToRotate.add(new Integer(pgsToRotate[i])); int direction = current_selection.getDirection(); PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); for (int page = 1; page <= pageCount; page++) { if (pagesToRotate.contains(new Integer(page))) { // int currentRotation = // Integer.parseInt(reader.getPageN(page).get(PdfName.ROTATE).toString()); int currentRotation = currentPageData.getRotation(page); if (direction == ROTATECLOCKWISE) reader.getPageN(page).put(PdfName.ROTATE, new PdfNumber((currentRotation + 90) % 360)); else if (direction == ROTATECOUNTERCLOCKWISE) reader.getPageN(page).put(PdfName.ROTATE, new PdfNumber((currentRotation - 90) % 360)); else if (direction == ROTATE180) reader.getPageN(page).put(PdfName.ROTATE, new PdfNumber((currentRotation + 180) % 360)); else throw new Exception("invalid desired rotation"); } } PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void setCrop(int pageCount, PdfPageData currentPageData, CropPDFPages cropPage) { File tempFile = null;/*from ww w . j a v a2 s.c o m*/ try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToEdit = cropPage.getPages(); if (pgsToEdit == null) return; List pagesToEdit = new ArrayList(); for (int i = 0; i < pgsToEdit.length; i++) pagesToEdit.add(new Integer(pgsToEdit[i])); PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); boolean applyToCurrent = cropPage.applyToCurrentCrop(); for (int page = 1; page <= pageCount; page++) { if (pagesToEdit.contains(new Integer(page))) { float currentLeftCrop = currentPageData.getCropBoxX(page); float currentBottomCrop = currentPageData.getCropBoxY(page); float currentRightCrop = currentPageData.getCropBoxWidth(page) + currentLeftCrop; float currentTopCrop = currentPageData.getCropBoxHeight(page) + currentBottomCrop; float[] newCrop = cropPage.getCrop(); if (applyToCurrent) { newCrop[0] = currentLeftCrop + newCrop[0]; newCrop[1] = currentBottomCrop + newCrop[1]; newCrop[2] = currentRightCrop - newCrop[2]; newCrop[3] = currentTopCrop - newCrop[3]; } else { newCrop[2] = reader.getPageSize(page).width() - newCrop[2]; newCrop[3] = reader.getPageSize(page).height() - newCrop[3]; } reader.getPageN(page).put(PdfName.CROPBOX, new PdfArray(newCrop)); } } PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void delete(int pageCount, PdfPageData currentPageData, DeletePDFPages deletedPages) { File tempFile = null;//from w w w .j a v a 2 s . c o m try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToDelete = deletedPages.getDeletedPages(); if (pgsToDelete == null) return; int check = -1; if (pgsToDelete.length == 1) { check = currentGUI.showConfirmDialog(Messages.getMessage("PdfViewerMessage.ConfirmDeletePage"), Messages.getMessage("PdfViewerMessage.Confirm"), JOptionPane.YES_NO_OPTION); } else { check = currentGUI.showConfirmDialog(Messages.getMessage("PdfViewerMessage.ConfirmDeletePage"), Messages.getMessage("PdfViewerMessage.Confirm"), JOptionPane.YES_NO_OPTION); } if (check != 0) return; if (pgsToDelete == null) return; List pagesToDelete = new ArrayList(); for (int i = 0; i < pgsToDelete.length; i++) pagesToDelete.add(new Integer(pgsToDelete[i])); PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); List bookmarks = SimpleBookmark.getBookmark(reader); // int[][] xx = new int[pgsToDelete.length][1]; // for(int i=0; i<pgsToDelete.length;i++){ // xx[i][0] = pgsToDelete[i]; // } // // PageRanges pr = new PageRanges(xx); // int[] toRemove = linearize(pr.getMembers()); // // SimpleBookmark.eliminatePages(bookmarks,toRemove); SimpleBookmark.shiftPageNumbers(bookmarks, -1, new int[] { 5, 5 }); // if(1==1) // return; /** * check document will have at leat 1 page */ boolean pageAdded = false; for (int page = 1; page <= pageCount; page++) { if (!pagesToDelete.contains(new Integer(page))) { pageAdded = true; page = pageCount; } } if (!pageAdded) { currentGUI.showMessageDialog(Messages.getMessage("PdfViewerError.PageWillNotDelete")); return; } Document document = new Document(); PdfCopy writer = new PdfCopy(document, new FileOutputStream(selectedFile)); document.open(); for (int page = 1; page <= pageCount; page++) { if (!pagesToDelete.contains(new Integer(page))) { PdfImportedPage pip = writer.getImportedPage(reader, page); writer.addPage(pip); pageAdded = true; } } writer.setOutlines(bookmarks); document.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void stampImage(int pageCount, PdfPageData currentPageData, final StampImageToPDFPages stampImage) { File tempFile = null;//ww w .j ava2s .c o m try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToEdit = stampImage.getPages(); if (pgsToEdit == null) return; File fileToTest = new File(stampImage.getImageLocation()); if (!fileToTest.exists()) { currentGUI.showMessageDialog(Messages.getMessage("PdfViewerError.ImageDoesNotExist")); return; } List pagesToEdit = new ArrayList(); for (int i = 0; i < pgsToEdit.length; i++) pagesToEdit.add(new Integer(pgsToEdit[i])); final PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); int n = reader.getNumberOfPages(); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); Image img = Image.getInstance(fileToTest.getAbsolutePath()); int chosenWidthScale = stampImage.getWidthScale(); int chosenHeightScale = stampImage.getHeightScale(); img.scalePercent(chosenWidthScale, chosenHeightScale); String chosenPlacement = stampImage.getPlacement(); int chosenRotation = stampImage.getRotation(); img.setRotationDegrees(chosenRotation); String chosenHorizontalPosition = stampImage.getHorizontalPosition(); String chosenVerticalPosition = stampImage.getVerticalPosition(); float chosenHorizontalOffset = stampImage.getHorizontalOffset(); float chosenVerticalOffset = stampImage.getVerticalOffset(); for (int page = 0; page <= n; page++) { if (pagesToEdit.contains(new Integer(page))) { PdfContentByte cb; if (chosenPlacement.equals("Overlay")) cb = stamp.getOverContent(page); else cb = stamp.getUnderContent(page); int currentRotation = currentPageData.getRotation(page); Rectangle pageSize; if (currentRotation == 90 || currentRotation == 270) pageSize = reader.getPageSize(page).rotate(); else pageSize = reader.getPageSize(page); float startx, starty; if (chosenVerticalPosition.equals("From the top")) { starty = pageSize.height() - ((img.height() * (chosenHeightScale / 100)) / 2); } else if (chosenVerticalPosition.equals("Centered")) { starty = (pageSize.height() / 2) - ((img.height() * (chosenHeightScale / 100)) / 2); } else { starty = 0; } if (chosenHorizontalPosition.equals("From the left")) { startx = 0; } else if (chosenHorizontalPosition.equals("Centered")) { startx = (pageSize.width() / 2) - ((img.width() * (chosenWidthScale / 100)) / 2); } else { startx = pageSize.width() - ((img.width() * (chosenWidthScale / 100)) / 2); } img.setAbsolutePosition(startx + chosenHorizontalOffset, starty + chosenVerticalOffset); cb.addImage(img); } } stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void stampText(int pageCount, PdfPageData currentPageData, final StampTextToPDFPages stampText) { File tempFile = null;//from www .ja v a 2s . c o m try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToEdit = stampText.getPages(); if (pgsToEdit == null) return; List pagesToEdit = new ArrayList(); for (int i = 0; i < pgsToEdit.length; i++) pagesToEdit.add(new Integer(pgsToEdit[i])); final PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); for (int page = 1; page <= pageCount; page++) { if (pagesToEdit.contains(new Integer(page))) { String chosenText = stampText.getText(); if (!chosenText.equals("")) { String chosenFont = stampText.getFontName(); Color chosenFontColor = stampText.getFontColor(); int chosenFontSize = stampText.getFontSize(); int chosenRotation = stampText.getRotation(); String chosenPlacement = stampText.getPlacement(); String chosenHorizontalPosition = stampText.getHorizontalPosition(); String chosenVerticalPosition = stampText.getVerticalPosition(); float chosenHorizontalOffset = stampText.getHorizontalOffset(); float chosenVerticalOffset = stampText.getVerticalOffset(); BaseFont font = BaseFont.createFont(chosenFont, BaseFont.WINANSI, false); PdfContentByte cb; if (chosenPlacement.equals("Overlay")) cb = stamp.getOverContent(page); else cb = stamp.getUnderContent(page); cb.beginText(); cb.setColorFill(chosenFontColor); cb.setFontAndSize(font, chosenFontSize); int currentRotation = currentPageData.getRotation(page); Rectangle pageSize; if (currentRotation == 90 || currentRotation == 270) pageSize = reader.getPageSize(page).rotate(); else pageSize = reader.getPageSize(page); float startx; float starty; if (chosenVerticalPosition.equals("From the top")) { starty = pageSize.height(); } else if (chosenVerticalPosition.equals("Centered")) { starty = pageSize.height() / 2; } else { starty = 0; } if (chosenHorizontalPosition.equals("From the left")) { startx = 0; } else if (chosenHorizontalPosition.equals("Centered")) { startx = pageSize.width() / 2; } else { startx = pageSize.width(); } cb.showTextAligned(Element.ALIGN_CENTER, chosenText, startx + chosenHorizontalOffset, starty + chosenVerticalOffset, chosenRotation); cb.endText(); } } } stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void addHeaderFooter(int pageCount, PdfPageData currentPageData, final AddHeaderFooterToPDFPages addHeaderFooter) { File tempFile = null;/*w w w. j a v a 2 s .c o m*/ try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToEdit = addHeaderFooter.getPages(); if (pgsToEdit == null) return; List pagesToEdit = new ArrayList(); for (int i = 0; i < pgsToEdit.length; i++) pagesToEdit.add(new Integer(pgsToEdit[i])); final PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); String chosenFont = addHeaderFooter.getFontName(); Color chosenFontColor = addHeaderFooter.getFontColor(); int chosenFontSize = addHeaderFooter.getFontSize(); float chosenLeftRightMargin = addHeaderFooter.getLeftRightMargin(); float chosenTopBottomMargin = addHeaderFooter.getTopBottomMargin(); String text[] = new String[6]; text[0] = addHeaderFooter.getLeftHeader(); text[1] = addHeaderFooter.getCenterHeader(); text[2] = addHeaderFooter.getRightHeader(); text[3] = addHeaderFooter.getLeftFooter(); text[4] = addHeaderFooter.getCenterFooter(); text[5] = addHeaderFooter.getRightFooter(); Date date = new Date(); String shortDate = DateFormat.getDateInstance(DateFormat.SHORT).format(date); String longDate = DateFormat.getDateInstance(DateFormat.LONG).format(date); SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss a"); String time12 = formatter.format(date); formatter = new SimpleDateFormat("HH.mm.ss"); String time24 = formatter.format(date); String fileName = new File(selectedFile).getName(); BaseFont font = BaseFont.createFont(chosenFont, BaseFont.WINANSI, false); for (int page = 1; page <= pageCount; page++) { if (pagesToEdit.contains(new Integer(page))) { String[] textCopy = new String[text.length]; System.arraycopy(text, 0, textCopy, 0, text.length); for (int i = 0; i < 6; i++) { textCopy[i] = textCopy[i].replaceAll("<d>", shortDate); textCopy[i] = textCopy[i].replaceAll("<D>", longDate); textCopy[i] = textCopy[i].replaceAll("<t>", time12); textCopy[i] = textCopy[i].replaceAll("<T>", time24); textCopy[i] = textCopy[i].replaceAll("<f>", fileName); textCopy[i] = textCopy[i].replaceAll("<F>", selectedFile); textCopy[i] = textCopy[i].replaceAll("<p>", "" + page); textCopy[i] = textCopy[i].replaceAll("<P>", "" + pageCount); } PdfContentByte cb = stamp.getOverContent(page); cb.beginText(); cb.setColorFill(chosenFontColor); cb.setFontAndSize(font, chosenFontSize); Rectangle pageSize = reader.getPageSizeWithRotation(page); cb.showTextAligned(Element.ALIGN_LEFT, textCopy[0], chosenLeftRightMargin, pageSize.height() - chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_CENTER, textCopy[1], pageSize.width() / 2, pageSize.height() - chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_RIGHT, textCopy[2], pageSize.width() - chosenLeftRightMargin, pageSize.height() - chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_LEFT, textCopy[3], chosenLeftRightMargin, chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_CENTER, textCopy[4], pageSize.width() / 2, chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_RIGHT, textCopy[5], pageSize.width() - chosenLeftRightMargin, chosenTopBottomMargin, 0); cb.endText(); } } stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void encrypt(int pageCount, PdfPageData currentPageData, EncryptPDFDocument encryptPage) { String p = encryptPage.getPermissions(); int encryptionLevel = encryptPage.getEncryptionLevel(); String userPassword = encryptPage.getUserPassword(); String masterPassword = encryptPage.getMasterPassword(); int permit[] = { PdfWriter.AllowPrinting, PdfWriter.AllowModifyContents, PdfWriter.AllowCopy, PdfWriter.AllowModifyAnnotations, PdfWriter.AllowFillIn }; int permissions = 0; for (int i = 0; i < p.length(); ++i) { permissions |= (p.charAt(i) == '0' ? 0 : permit[i]); }//w w w. jav a 2 s. c o m File tempFile = null; try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); PdfEncryptor.encrypt(reader, new FileOutputStream(selectedFile), userPassword.getBytes(), masterPassword.getBytes(), permissions, encryptionLevel == 0); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jrimum.bopepo.pdf.PdfDocMix.java
License:Apache License
/** * Inicializa os principais objetos para a escrita dos dados do documento no * template PDF: {@code stamper}, {@code reader} e {@code outputStream}. * // w w w .ja va2 s. co m * @since 0.2 */ private void init() { try { reader = new PdfReader(getTemplate()); outputStream = new ByteArrayOutputStream(); stamper = new PdfStamper(reader, outputStream); final String JRIMUM = "jrimum.org/bopepo"; String creator = docInfo.creator(); if (isBlank(creator)) { withCreator(JRIMUM); } else { withCreator(creator + " by (" + JRIMUM + ")"); } if (isNull(docInfo.creation())) { docInfo.creation(Calendar.getInstance()); } stamper.setMoreInfo((HashMap<?, ?>) docInfo.toMap()); if (isNotNull(displayDocTitle)) { stamper.addViewerPreference(PdfName.DISPLAYDOCTITLE, displayDocTitle ? PdfBoolean.PDFTRUE : PdfBoolean.PDFFALSE); } form = stamper.getAcroFields(); } catch (Exception e) { Exceptions.throwIllegalStateException(e); } }