List of usage examples for com.itextpdf.text.pdf PdfReader getPageContent
public byte[] getPageContent(final int pageNum) throws IOException
From source file:de.mat.utils.pdftools.PdfExtractEmptyPages.java
License:Mozilla Public License
/** * <h4>FeatureDomain:</h4>// w ww. j a v a 2s . co m * PublishingTools * <h4>FeatureDescription:</h4> * reads readerOrig and adds pages to writerRemoved if empty, or to * writerTrimmed if not empty * <h4>FeatureResult:</h4> * <ul> * <li>updates writerTrimmed - add all pages which are not empty * <li>updates writerRemoved - add all empty pages * </ul> * <h4>FeatureKeywords:</h4> * PDF Publishing * @param origFileName - orig filename of the sourcepdf * @param readerOrig - reader of source * @param writerTrimmed - writer for trimmed pages * @param writerRemoved - writer for empty pages * @param flgTrim - ?? * @return - count of trimmed pages * @throws Exception */ public static int addTrimmedPages(String origFileName, PdfReader readerOrig, PdfCopy writerTrimmed, PdfCopy writerRemoved, boolean flgTrim) throws Exception { PdfImportedPage page = null; int countTrimmedPages = 0; //loop each page for (int i = 1; i <= readerOrig.getNumberOfPages(); i++) { boolean flgIsEmpty = true; // get dictionary PdfDictionary pageDict = readerOrig.getPageN(i); // every pdf-version has its own way :-( char version = readerOrig.getPdfVersion(); if (version == '3') { // PDF-Version: 3 // examine the resource dictionary for /Font or // /XObject keys. If either are present, they're almost // certainly actually used on the page -> not blank. PdfObject myObj = pageDict.get(PdfName.RESOURCES); PdfDictionary resDict = null; if (myObj instanceof PdfDictionary) { resDict = (PdfDictionary) myObj; } else { resDict = (PdfDictionary) PdfReader.getPdfObject(myObj); } if (resDict != null) { flgIsEmpty = resDict.get(PdfName.FONT) == null && resDict.get(PdfName.XOBJECT) == null; if (LOGGER.isInfoEnabled()) { if (flgIsEmpty) { LOGGER.info("probably empty page " + i + " Version: 1." + version + " FONT/XOBJECT found in File:" + origFileName); } else { LOGGER.info("normal page " + i + " Version: 1." + version + " no FONT/XOBJECT found in File:" + origFileName); } } } } else if (version == '4') { // PDF-Version: 4 // check the contentsize. // get the page content byte bContent[] = readerOrig.getPageContent(i); ByteArrayOutputStream bs = new ByteArrayOutputStream(); // write the content to an output stream bs.write(bContent); flgIsEmpty = true; if (bs.size() > blankPdfsize) { if (LOGGER.isInfoEnabled()) LOGGER.info("normal page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:" + origFileName); flgIsEmpty = false; } else { if (LOGGER.isInfoEnabled()) LOGGER.info("probably empty page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:" + origFileName); } } else if (version == '5') { // PDF-Version: 5 // check the contentsize. // get the page content byte bContent[] = readerOrig.getPageContent(i); ByteArrayOutputStream bs = new ByteArrayOutputStream(); // write the content to an output stream bs.write(bContent); flgIsEmpty = true; if (bs.size() > blankPdfsize_v5) { if (LOGGER.isInfoEnabled()) LOGGER.info("normal page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:" + origFileName); flgIsEmpty = false; } else { if (LOGGER.isInfoEnabled()) LOGGER.info("probably empty page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:" + origFileName); } } // add page to removed or trimmed document if (!flgIsEmpty || !flgTrim) { if (LOGGER.isInfoEnabled()) LOGGER.info("add page " + i); page = writerTrimmed.getImportedPage(readerOrig, i); writerTrimmed.addPage(page); countTrimmedPages++; } else { if (LOGGER.isInfoEnabled()) LOGGER.info("skip page " + i + " Version: 1." + version + " File:" + origFileName); if (writerRemoved != null) { page = writerRemoved.getImportedPage(readerOrig, i); writerRemoved.addPage(page); } } } return countTrimmedPages; }
From source file:jp.nichicom.ndk.affair.nu.nu001.NU001.java
License:Open Source License
/** * ???/*from w w w . jav a 2 s.com*/ * @param e * @throws Exception ? */ protected void compareActionPerformed(ActionEvent e) throws Exception { // if (!new File(getPdf1().getText()).exists()) { ACMessageBox.showExclamation("PDF1?????"); return; } if (!new File(getPdf2().getText()).exists()) { ACMessageBox.showExclamation("PDF2?????"); return; } final PdfReader p1 = new PdfReader(getPdf1().getText()); final PdfReader p2 = new PdfReader(getPdf2().getText()); final int pages1 = p1.getNumberOfPages(); final int pages2 = p2.getNumberOfPages(); if (pages1 != pages2) { if (pages1 > pages2) { ACMessageBox.showExclamation("PDF1PDF2??????"); return; } if (ACMessageBox.showOkCancel("PDF?????" + ACConstants.LINE_SEPARATOR + "PDF1??????PDF2?????????") != ACMessageBox.RESULT_OK) { return; } } new Runnable() { private boolean firstLock; public void run() { final int stopKeyCode = KeyEvent.VK_CAPS_LOCK; firstLock = Toolkit.getDefaultToolkit().getLockingKeyState(stopKeyCode); LinkedList<Integer> pages = new LinkedList<Integer>(); for (int i = 1; i <= pages2; i++) { pages.add(new Integer(i)); } errorCount = 0; pageOfProcessed = 0; pageOfCount = pages1; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= pages1; i++) { try { boolean match = false; byte[] b1 = p1.getPageContent(i); Iterator<Integer> it = pages.iterator(); while (it.hasNext()) { int page = ((Integer) it.next()).intValue(); byte[] b2 = p2.getPageContent(page); if (Arrays.equals(b1, b2)) { sb.append("PDF1(" + i + ") = PDF2(" + page + ")" + ACConstants.LINE_SEPARATOR); match = true; it.remove(); break; } } if (!match) { sb.append("PDF1(" + i + ") ???" + ACConstants.LINE_SEPARATOR); errorCount++; } } catch (Exception ex) { sb.append("PDF1(" + i + ") ??" + ACConstants.LINE_SEPARATOR); errorCount++; } setProgress(sb); pageOfProcessed = i; if (Toolkit.getDefaultToolkit().getLockingKeyState(stopKeyCode) != firstLock) { getResult().setText( "?(" + pageOfProcessed + " / " + pageOfCount + ") / ?:" + errorCount + " " + ACConstants.LINE_SEPARATOR + sb.toString()); return; } } getResult().setText("(" + pageOfProcessed + " / " + pageOfCount + ") / ?:" + errorCount + " " + ACConstants.LINE_SEPARATOR + sb.toString()); } }.run(); }
From source file:org.h819.commons.file.MyPDFUtils.java
/** * ? pdf ??/*from www. j a v a 2s . c o m*/ * * @param srcPdfFile the original PDF * @param descPdfFile the resulting PDF * @throws java.io.IOException * @throws DocumentException */ public static void compressPdf(File srcPdfFile, File descPdfFile) throws IOException, DocumentException { if (srcPdfFile == null || !srcPdfFile.exists()) throw new IOException("src pdf file '" + srcPdfFile + "' does not exsit."); PdfReader reader = getPdfReader(srcPdfFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(descPdfFile.getAbsoluteFile()), PdfWriter.VERSION_1_7); stamper.getWriter().setCompressionLevel(9); int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { reader.setPageContent(i, reader.getPageContent(i)); } stamper.setFullCompression(); stamper.close(); reader.close(); }
From source file:org.h819.commons.file.MyPDFUtils.java
/** * ?pdf//from w w w .ja v a 2 s . c o m * * @param srcPdfFile the original PDF * @param descPdfFile the resulting PDF * @throws java.io.IOException * @throws DocumentException */ public static void decompressPdf(File srcPdfFile, File descPdfFile) throws IOException, DocumentException { if (srcPdfFile == null || !srcPdfFile.exists()) throw new IOException("src pdf file '" + srcPdfFile + "' does not exsit."); PdfReader reader = getPdfReader(srcPdfFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(descPdfFile.getAbsoluteFile())); stamper.getWriter().setCompressionLevel(PdfStream.NO_COMPRESSION); int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { reader.setPageContent(i, reader.getPageContent(i)); } stamper.close(); reader.close(); }
From source file:org.sejda.impl.itext5.component.PdfStamperHandler.java
License:Open Source License
/** * Enables compression if compress is true * /*www . j a v a2 s . c o m*/ * @param compress * @throws TaskException */ public void setCompression(boolean compress, PdfReader reader) throws TaskException { if (compress) { try { stamper.getWriter().setCompressionLevel(PdfStream.BEST_COMPRESSION); int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { reader.setPageContent(i, reader.getPageContent(i)); } stamper.setFullCompression(); } catch (DocumentException de) { throw new TaskException("Unable to set compression on stamper", de); } catch (IOException e) { throw new TaskException("Unable to set compression on stamper", e); } } }