List of usage examples for org.apache.pdfbox.cos COSDictionary removeItem
public void removeItem(COSName key)
From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java
License:Apache License
/** * This will write the trailer to the PDF document. * * @param doc The document to create the trailer for. * * @throws IOException If there is an IOError while writing the document. * @throws COSVisitorException If there is an error while generating the * data./*from w w w.jav a 2 s . c o m*/ */ protected void doWriteTrailer(COSDocument doc) throws IOException, COSVisitorException { getStandardOutput().write(TRAILER); getStandardOutput().writeEOL(); COSDictionary trailer = doc.getTrailer(); //sort xref, needed only if object keys not regenerated Collections.sort(getXRefEntries()); COSWriterXRefEntry lastEntry = getXRefEntries().get(getXRefEntries().size() - 1); trailer.setInt(COSName.SIZE, (int) lastEntry.getKey().getNumber() + 1); // Only need to stay, if an incremental update will be performed if (!incrementalUpdate) { trailer.removeItem(COSName.PREV); } // Remove a checksum if present trailer.removeItem(COSName.DOC_CHECKSUM); trailer.accept(this); }
From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java
License:Apache License
private void doWriteXRefInc(COSDocument doc, long hybridPrev) throws IOException, COSVisitorException { if (doc.isXRefStream() || hybridPrev != -1) { // the file uses XrefStreams, so we need to update // it with an xref stream. We create a new one and fill it // with data available here // first set an entry for the null entry in the xref table // this is probably not necessary // addXRefEntry(COSWriterXRefEntry.getNullEntry()); // create a new XRefStrema object PDFXRefStream pdfxRefStream = new PDFXRefStream(); // add all entries from the incremental update. List<COSWriterXRefEntry> xRefEntries2 = getXRefEntries(); for (COSWriterXRefEntry cosWriterXRefEntry : xRefEntries2) { pdfxRefStream.addEntry(cosWriterXRefEntry); }/* www . ja va 2 s . c om*/ COSDictionary trailer = doc.getTrailer(); // trailer.setLong(COSName.PREV, hybridPrev == -1 ? prev : hybridPrev); trailer.setLong(COSName.PREV, doc.getStartXref()); pdfxRefStream.addTrailerInfo(trailer); // the size is the highest object number+1. we add one more // for the xref stream object we are going to write pdfxRefStream.setSize(getNumber() + 2); setStartxref(getStandardOutput().getPos()); COSStream stream2 = pdfxRefStream.getStream(); doWriteObject(stream2); } if (!doc.isXRefStream() || hybridPrev != -1) { COSDictionary trailer = doc.getTrailer(); trailer.setLong(COSName.PREV, doc.getStartXref()); if (hybridPrev != -1) { COSName xrefStm = COSName.XREF_STM; trailer.removeItem(xrefStm); trailer.setLong(xrefStm, getStartxref()); } addXRefEntry(COSWriterXRefEntry.getNullEntry()); // sort xref, needed only if object keys not regenerated Collections.sort(getXRefEntries()); // remember the position where x ref was written setStartxref(getStandardOutput().getPos()); getStandardOutput().write(XREF); getStandardOutput().writeEOL(); // write start object number and object count for this x ref section // we assume starting from scratch Integer[] xRefRanges = getXRefRanges(getXRefEntries()); int xRefLength = xRefRanges.length; int x = 0; int j = 0; while (x < xRefLength && (xRefLength % 2) == 0) { writeXrefRange(xRefRanges[x], xRefRanges[x + 1]); for (int i = 0; i < xRefRanges[x + 1]; ++i) { writeXrefEntry(xRefEntries.get(j++)); } x += 2; } } }
From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java
License:Apache License
/** * This will write the pdf document.// w w w. j a v a 2 s . c o m * * @param doc The document to write. * @param idTime The time seed used to generate the id * * @throws COSVisitorException If an error occurs while generating the data. */ public void write(PDDocument doc, long idTime) throws COSVisitorException { document = doc; if (incrementalUpdate) { prepareIncrement(doc); } // if the document says we should remove encryption, then we shouldn't encrypt if (doc.isAllSecurityToBeRemoved()) { this.willEncrypt = false; // also need to get rid of the "Encrypt" in the trailer so readers // don't try to decrypt a document which is not encrypted COSDocument cosDoc = doc.getDocument(); COSDictionary trailer = cosDoc.getTrailer(); trailer.removeItem(COSName.ENCRYPT); } else { SecurityHandler securityHandler = document.getSecurityHandler(); if (securityHandler != null) { try { securityHandler.prepareDocumentForEncryption(document); this.willEncrypt = true; } catch (IOException e) { throw new COSVisitorException(e); } catch (CryptographyException e) { throw new COSVisitorException(e); } } else { this.willEncrypt = false; } } COSDocument cosDoc = document.getDocument(); COSDictionary trailer = cosDoc.getTrailer(); COSArray idArray = (COSArray) trailer.getDictionaryObject(COSName.ID); if (idArray == null || incrementalUpdate) { try { //algorithm says to use time/path/size/values in doc to generate //the id. We don't have path or size, so do the best we can MessageDigest md = MessageDigest.getInstance("MD5"); md.update(Long.toString(idTime).getBytes("ISO-8859-1")); COSDictionary info = (COSDictionary) trailer.getDictionaryObject(COSName.INFO); if (info != null) { Iterator<COSBase> values = info.getValues().iterator(); while (values.hasNext()) { md.update(values.next().toString().getBytes("ISO-8859-1")); } } idArray = new COSArray(); COSString id = new COSString(md.digest()); idArray.add(id); idArray.add(id); trailer.setItem(COSName.ID, idArray); } catch (NoSuchAlgorithmException e) { throw new COSVisitorException(e); } catch (UnsupportedEncodingException e) { throw new COSVisitorException(e); } } cosDoc.accept(this); }
From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java
License:Apache License
private void mergeXObj(COSDictionary sourcePageResources, FontInfo fontinfo, UniqueName uniqueName) throws IOException { COSDictionary xobj = (COSDictionary) sourcePageResources.getDictionaryObject(COSName.XOBJECT); if (xobj != null && pdfDoc.isMergeFontsEnabled()) { for (Map.Entry<COSName, COSBase> i : xobj.entrySet()) { COSObject v = (COSObject) i.getValue(); COSStream stream = (COSStream) v.getObject(); COSDictionary res = (COSDictionary) stream.getDictionaryObject(COSName.RESOURCES); if (res != null) { COSDictionary src = (COSDictionary) res.getDictionaryObject(COSName.FONT); if (src != null) { COSDictionary target = (COSDictionary) sourcePageResources .getDictionaryObject(COSName.FONT); if (target == null) { sourcePageResources.setItem(COSName.FONT, src); } else { for (Map.Entry<COSName, COSBase> entry : src.entrySet()) { if (!target.keySet().contains(entry.getKey())) { target.setItem(uniqueName.getName(entry.getKey()), entry.getValue()); }//from w w w. j a va2s . c o m } } PDFWriter writer = new MergeFontsPDFWriter(src, fontinfo, uniqueName, parentFonts, 0); String c = writer.writeText(new PDStream(stream)); if (c != null) { stream.removeItem(COSName.FILTER); newXObj.put(i.getKey(), c); for (Object e : src.keySet().toArray()) { COSName name = (COSName) e; src.setItem(uniqueName.getName(name), src.getItem(name)); src.removeItem(name); } } } } } } }