/** * Check if a PDF at given path is encrypted * * @param path - path of PDF * @return true - if encrypted otherwise false */ public boolean isPDFEncrypted(String path) { boolean isEncrypted = false; try { new PdfReader(path); } catch (IOException e) { isEncrypted = true; } return isEncrypted; }
/** * Writes information about the specified page in a PDF file to the specified output stream. * @since 2.1.5 * @param pdfFile a File instance referring to a PDF file * @param pageNum the page number to read * @param out the output stream to send the content to * @throws IOException */ static public void listContentStream(File pdfFile, int pageNum, PrintWriter out) throws IOException { PdfReader reader = new PdfReader(pdfFile.getCanonicalPath()); listContentStreamForPage(reader, pageNum, out); }
/** * Writes information about the specified page in a PDF file to the specified output stream. * @since 2.1.5 * @param pdfFile a File instance referring to a PDF file * @param pageNum the page number to read * @param out the output stream to send the content to * @throws IOException */ static public void listContentStream(File pdfFile, int pageNum, PrintWriter out) throws IOException { PdfReader reader = new PdfReader(pdfFile.getCanonicalPath()); listContentStreamForPage(reader, pageNum, out); }
public PdfReader getReader(URI uri) throws IOException { PdfReader result = (PdfReader) _readerCache.get(uri.getPath()); if (result == null) { result = new PdfReader(getSharedContext().getUserAgentCallback().getBinaryResource(uri.toString())); _readerCache.put(uri.getPath(), result); } return result; }
public static ClusterJob createClusterJob(File origFile) throws IOException, PdfException { PdfReader reader = new PdfReader(origFile.getAbsolutePath()); ClusterJob clusterJob = new ClusterJob(origFile); reader.close(); return clusterJob; }
private static boolean isPasswordRequired(final File file) throws IOException { PdfReader reader = new PdfReader(file.getAbsolutePath()); boolean isEncrypted = reader.isEncrypted(); reader.close(); return isEncrypted; }
private boolean createPDF(String inputPath, String output, String pages) { try { PdfReader reader = new PdfReader(inputPath); reader.selectPages(pages); PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream(output)); pdfStamper.close(); return true; } catch (IOException | DocumentException e) { e.printStackTrace(); return false; } }
/** * Writes information about each page in a PDF file to the specified output stream. * @since 2.1.5 * @param pdfFile a File instance referring to a PDF file * @param out the output stream to send the content to * @throws IOException */ static public void listContentStream(File pdfFile, PrintWriter out) throws IOException { PdfReader reader = new PdfReader(pdfFile.getCanonicalPath()); int maxPageNum = reader.getNumberOfPages(); for (int pageNum = 1; pageNum <= maxPageNum; pageNum++){ listContentStreamForPage(reader, pageNum, out); } }
/** * Writes information about each page in a PDF file to the specified output stream. * @since 2.1.5 * @param pdfFile a File instance referring to a PDF file * @param out the output stream to send the content to * @throws IOException */ static public void listContentStream(File pdfFile, PrintWriter out) throws IOException { PdfReader reader = new PdfReader(pdfFile.getCanonicalPath()); int maxPageNum = reader.getNumberOfPages(); for (int pageNum = 1; pageNum <= maxPageNum; pageNum++){ listContentStreamForPage(reader, pageNum, out); } }
public PdfMetaInformation(final File source) throws IOException { PdfReader reader = new PdfReader(source.getAbsolutePath()); this.sourcePageCount = reader.getNumberOfPages(); this.sourceMetaInfo = reader.getInfo(); this.sourceBookmarks = SimpleBookmark.getBookmark(reader); reader.close(); }
public static CropJob createCropJob(File source) throws IOException { CropJob result = null; if (source != null && source.exists()) { PdfReader reader = new PdfReader(source.getAbsolutePath()); result = new CropJob(source, reader.getNumberOfPages(), reader .getInfo(), SimpleBookmark.getBookmark(reader)); reader.close(); return result; } return result; }
public void parse( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context ) throws IOException, SAXException, TikaException { PdfReader reader = new PdfReader( stream ); PDFParser.extractMetadata( reader, metadata ); XHTMLContentHandler xhtml = new XHTMLContentHandler( handler, metadata ); xhtml.startDocument(); xhtml.startElement( "p" ); xhtml.characters( new String( PDFParser.extractText( reader ).getBytes( "UTF-8" ), "UTF-8" ) ); xhtml.endElement( "p" ); xhtml.endDocument(); }
public void parse( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context ) throws IOException, SAXException, TikaException { PdfReader reader = new PdfReader( stream ); PDFParser.extractMetadata( reader, metadata ); XHTMLContentHandler xhtml = new XHTMLContentHandler( handler, metadata ); xhtml.startDocument(); xhtml.startElement( "p" ); xhtml.characters( new String( PDFParser.extractText( reader ).getBytes( "UTF-8" ), "UTF-8" ) ); xhtml.endElement( "p" ); xhtml.endDocument(); }
public static CropJob createCropJob(ClusterJob curClusterJob) throws IOException { File source = curClusterJob.getSource(); if (source != null && source.exists()) { PdfReader reader = new PdfReader(source.getAbsolutePath()); CropJob result = new CropJob(source, reader.getNumberOfPages(), reader.getInfo(), SimpleBookmark.getBookmark(reader)); reader.close(); result.setClusterCollection(curClusterJob.getClusterCollection()); return result; } return null; }
void addDocument(PdfReader reader, List<Integer> pagesToKeep) throws DocumentException, IOException { if (!readers2intrefs.containsKey(reader) && reader.isTampered()) throw new DocumentException(MessageLocalization.getComposedMessage("the.document.was.reused")); reader = new PdfReader(reader); reader.selectPages(pagesToKeep); if (reader.getNumberOfPages() == 0) return; reader.setTampered(false); addDocument(reader); }
void addDocument(PdfReader reader, List<Integer> pagesToKeep) throws DocumentException, IOException { if (!readers2intrefs.containsKey(reader) && reader.isTampered()) throw new DocumentException(MessageLocalization.getComposedMessage("the.document.was.reused")); reader = new PdfReader(reader); reader.selectPages(pagesToKeep); if (reader.getNumberOfPages() == 0) return; reader.setTampered(false); addDocument(reader); }
public static void execute(Config config) throws IOException, DocumentException { PdfReader reader = new PdfReader(config.pdfInputStream); PdfStamper stamper = new PdfStamper(reader, config.pdfOutputStream, '\0'); if (!config.stampFilename.isEmpty()) { stampBackground(reader, stamper, config.stampFilename, true); } if (!config.backgroundFilename.isEmpty()) { stampBackground(reader, stamper, config.backgroundFilename, false); } if (config.formInputStream != null) { stamper.getAcroFields().setFields(new XfdfReader(config.formInputStream)); } stamper.setFormFlattening(config.flatten); stamper.close(); }
@Test public void canPrintPdfFromAnsiFile() throws DocumentException, IOException, InvalidColourException{ String ansiInput= FileUtils.readFileToString(new File("test_data/ansicolor.txt")); File tmp= new File("test_data/deleteme.pdf"); tmp.deleteOnExit(); Pdf pdf= new Pdf(ansiInput); pdf.convert(tmp, 10, false); assertTrue(tmp.length() > 1000); // Check file size is about right. PdfReader p = new PdfReader(tmp.getAbsolutePath()); assertEquals(1, p.getNumberOfPages()); }
@Test public void canAppendToPdf() throws IOException, DocumentException, InvalidColourException{ File tmp= new File("append.pdf"); tmp.delete(); tmp.deleteOnExit(); String ansiInput= FileUtils.readFileToString(new File("test_data/ansicolor.txt")); Pdf pdf= new Pdf(ansiInput); // Append to file not yet created: pdf.convert(tmp, 10, true); PdfReader p = new PdfReader(tmp.getAbsolutePath()); assertEquals(1, p.getNumberOfPages()); // Append another file pdf.convert(tmp, 10, true); p = new PdfReader(tmp.getAbsolutePath()); assertEquals(2, p.getNumberOfPages()); // We got two pages right? } }
public static void stampBackground(PdfReader reader, PdfStamper stamper, String signature, boolean isStamp) throws IOException, DocumentException { int num_pages = reader.getNumberOfPages(); PdfReader r = new PdfReader(signature); PdfImportedPage page = stamper.getImportedPage(r, 1); for (int i = 1; i <= num_pages; i++) { PdfContentByte canvas = isStamp ? stamper.getOverContent(i) : stamper.getUnderContent(i); canvas.addTemplate(page, 0, 0); } stamper.getWriter().freeReader(r); } }