/** * Creates a new empty PDStream object. * * @param document The document that the stream will be part of. */ public PDStream(PDDocument document) { stream = document.getDocument().createCOSStream(); }
/** * Create a new document to write the split contents to. * * @return the newly created PDDocument. * @throws IOException If there is an problem creating the new document. */ protected PDDocument createNewDocument() throws IOException { PDDocument document = memoryUsageSetting == null ? new PDDocument() : new PDDocument(memoryUsageSetting); document.getDocument().setVersion(getSourceDocument().getVersion()); document.setDocumentInformation(getSourceDocument().getDocumentInformation()); document.getDocumentCatalog().setViewerPreferences( getSourceDocument().getDocumentCatalog().getViewerPreferences()); return document; }
@Override public void createVisualSignature(PDDocument template) { pdfStructure.setVisualSignature(template.getDocument()); LOG.info("Visible signature has been created"); }
/** * Constructor to take a document. * * @param doc The document to display in the tree. */ public PDFTreeModel(PDDocument doc) { root = doc.getDocument().getTrailer(); }
/** * Close the given Document. If the close method of the document throws an * exception, it is logged using a commons logger (Level : WARN) * * @param document the document. */ public static void closeDocumentQuietly(PDDocument document) { if (document != null) { closeDocumentQuietly(document.getDocument()); } }
private COSStream createStream(String content) throws IOException { COSStream stream = inputPDFDocument.getDocument().createCOSStream(); try (OutputStream out = stream.createOutputStream( content.length() > 20 ? COSName.FLATE_DECODE : null)) { out.write(content.getBytes("ISO-8859-1")); } return stream; }
/** * Creates a Flate encoded <code>COSStream</code> object with the given data. * * @param data to write into the COSStream * @return COSStream a COSStream object that can be added to the document * @throws IOException */ private COSStream writeDataToStream(byte[] data) throws IOException { COSStream stream = document.getDocument().createCOSStream(); try (OutputStream os = stream.createOutputStream(COSName.FLATE_DECODE)) { os.write(data); } return stream; }
/** * Creates a COS stream from raw (encoded) data. */ private static COSStream createRawStream(PDDocument document, InputStream rawInput) throws IOException { COSStream stream = document.getDocument().createCOSStream(); try (OutputStream output = stream.createRawOutputStream()) { IOUtils.copy(rawInput, output); } return stream; }
/** * Constructor. Reads all data from the input stream and embeds it into the document with the * given filters applied, if any. This method closes the InputStream. */ private PDStream(PDDocument doc, InputStream input, COSBase filters) throws IOException { stream = doc.getDocument().createCOSStream(); try (OutputStream output = stream.createOutputStream(filters)) { IOUtils.copy(input, output); } finally { if (input != null) { input.close(); } } }
COSDocument cosDoc = doc.getDocument(); long highestNumber = doc.getDocument().getHighestXRefObjectNumber(); for ( COSObjectKey cosObjectKey : keySet )
/** * Constructor. Internal PDFBox use only! If you need to get the document catalog, call * {@link PDDocument#getDocumentCatalog()}. * * @param doc The document that this catalog is part of. */ protected PDDocumentCatalog(PDDocument doc) { document = doc; root = new COSDictionary(); root.setItem(COSName.TYPE, COSName.CATALOG); document.getDocument().getTrailer().setItem(COSName.ROOT, root); }
/** * This will create a new PDObjectStream object. * * @param document The document that the stream will be part of. * @return A new stream object. */ public static PDObjectStream createStream( PDDocument document ) { COSStream cosStream = document.getDocument().createCOSStream(); PDObjectStream strm = new PDObjectStream( cosStream ); strm.getCOSObject().setItem( COSName.TYPE, COSName.OBJ_STM ); return strm; }
/** * According to the PDF Reference, A linearized PDF contain a dictionary as first object (linearized dictionary) and * only this one in the first section. * * @param document the document to validate. * @return the linearization dictionary or null. */ protected COSDictionary getLinearizedDictionary(PDDocument document) { // ---- Get Ref to obj COSDocument cDoc = document.getDocument(); List<?> lObj = cDoc.getObjects(); for (Object object : lObj) { COSBase curObj = ((COSObject) object).getObject(); if (curObj instanceof COSDictionary && ((COSDictionary) curObj).keySet().contains(COSName.getPDFName(DICTIONARY_KEY_LINEARIZED))) { return (COSDictionary) curObj; } } return null; }
@Override public void validate(PreflightContext ctx) throws ValidationException { PDDocument pdfDoc = ctx.getDocument(); COSDocument cDoc = pdfDoc.getDocument(); List<?> lCOSObj = cDoc.getObjects(); for (Object o : lCOSObj) { COSBase cBase = ((COSObject) o).getObject(); if (cBase instanceof COSDictionary) { COSDictionary dic = (COSDictionary) cBase; String type = dic.getNameAsString(COSName.TYPE); if (FILE_SPECIFICATION_VALUE_TYPE.equals(type) || COSName.F.getName().equals(type)) { // ---- It is a file specification validateFileSpecification(ctx, dic); } } } }
private COSStream createCombinedContentStream(COSBase contents) throws IOException { List<COSStream> contentStreams = createContentStreamList(contents); // concatenate streams COSStream concatStream = inputPDFDocument.getDocument().createCOSStream(); try (OutputStream out = concatStream.createOutputStream(COSName.FLATE_DECODE)) { for (COSStream contentStream : contentStreams) { try (InputStream in = contentStream.createInputStream()) { IOUtils.copy(in, out); out.flush(); } } } return concatStream; }
@Override public void validate(PreflightContext ctx) throws ValidationException { PDDocument pdfDoc = ctx.getDocument(); COSDocument cDoc = pdfDoc.getDocument(); List<?> lCOSObj = cDoc.getObjects(); for (Object o : lCOSObj) { COSObject cObj = (COSObject) o; // If this object represents a Stream, the Dictionary must contain the Length key COSBase cBase = cObj.getObject(); if (cBase instanceof COSStream) { validateStreamObject(ctx, cObj); } } }
/** * This will export all FDF form data. * * @return An FDF document used to export the document. * @throws IOException If there is an error when exporting the document. */ public FDFDocument exportFDF() throws IOException { FDFDocument fdf = new FDFDocument(); FDFCatalog catalog = fdf.getCatalog(); FDFDictionary fdfDict = new FDFDictionary(); catalog.setFDF(fdfDict); List<FDFField> fdfFields = new ArrayList<>(); List<PDField> fields = getFields(); for (PDField field : fields) { fdfFields.add(field.exportFDF()); } fdfDict.setID(document.getDocument().getDocumentID()); if (!fdfFields.isEmpty()) { fdfDict.setFields(fdfFields); } return fdf; }
@Override public void createBackgroundLayerForm(PDResources innerFormResource, PDRectangle bbox) throws IOException { // create blank n0 background layer form PDFormXObject n0Form = new PDFormXObject(pdfStructure.getTemplate().getDocument().createCOSStream()); n0Form.setBBox(bbox); n0Form.setResources(new PDResources()); n0Form.setFormType(1); innerFormResource.put(COSName.getPDFName("n0"), n0Form); LOG.info("Created background layer form"); }
/** * This will write the header to the PDF document. * * @param doc The document to get the data from. * * @throws IOException If there is an error writing to the stream. */ protected void doWriteHeader(COSDocument doc) throws IOException { String headerString; if (fdfDocument != null) { headerString = "%FDF-"+ Float.toString(fdfDocument.getDocument().getVersion()); } else { headerString = "%PDF-"+ Float.toString(pdDocument.getDocument().getVersion()); } getStandardOutput().write( headerString.getBytes(Charsets.ISO_8859_1) ); getStandardOutput().writeEOL(); getStandardOutput().write(COMMENT); getStandardOutput().write(GARBAGE); getStandardOutput().writeEOL(); }
public void initTree() { TreeStatus treeStatus = new TreeStatus(document.getDocument().getTrailer()); statusPane.updateTreeStatus(treeStatus); if (isPageMode) { File file = new File(currentFilePath); DocumentEntry documentEntry = new DocumentEntry(document, file.getName()); ZoomMenu.getInstance().resetZoom(); RotationMenu.getInstance().setRotationSelection(RotationMenu.ROTATE_0_DEGREES); tree.setModel(new PDFTreeModel(documentEntry)); // Root/Pages/Kids/[0] is not always the first page, so use the first row instead: tree.setSelectionPath(tree.getPathForRow(1)); } else { tree.setModel(new PDFTreeModel(document)); tree.setSelectionPath(treeStatus.getPathForString("Root")); } }