public virtual void FullCompressionAppendMode() { PdfWriter writer = new PdfWriter(destinationFolder + "fullCompressionAppendMode.pdf", new WriterProperties ().SetFullCompressionMode(true).SetCompressionLevel(CompressionConstants.NO_COMPRESSION)); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "fullCompressionDoc.pdf"), writer, new StampingProperties().UseAppendMode()); PdfPage page = pdfDocument.GetPage(1); PdfStream contentStream = new PdfStream(); String contentStr = iText.IO.Util.JavaUtil.GetStringForBytes(pdfDocument.GetPage(1).GetFirstContentStream( ).GetBytes(), Encoding.ASCII); contentStream.SetData(contentStr.Replace("/F1 16", "/F1 24").GetBytes(Encoding.ASCII)); page.GetPdfObject().Put(PdfName.Contents, contentStream); page.SetModified(); pdfDocument.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "fullCompressionAppendMode.pdf" , sourceFolder + "cmp_fullCompressionAppendMode.pdf", destinationFolder, "diff_")); PdfDocument assertDoc = new PdfDocument(new PdfReader(destinationFolder + "fullCompressionAppendMode.pdf") ); NUnit.Framework.Assert.IsTrue(assertDoc.GetPdfObject(9).IsStream()); NUnit.Framework.Assert.AreEqual(1, ((PdfDictionary)assertDoc.GetPdfObject(9)).GetAsNumber(PdfName.N).IntValue ()); }
/// <summary> /// Flushes to the output stream modified objects that can belong only to the given page, which makes this method /// "safe" compared to the /// <see cref="UnsafeFlushDeep(int)"/> /// . Flushed object frees the memory, but it's impossible to /// modify such objects or read data from them. This method releases all other page structure objects that are not /// modified. /// <p> /// This method is mainly designed for the append mode. It is similar to the /// <see cref="PdfPage.Flush()"/> /// , but it /// additionally releases all page objects that were not flushed. This method is ideal for small amendments of pages, /// but it makes more sense to use /// <see cref="PdfPage.Flush()"/> /// for newly created or heavily modified pages. <br /> /// This method will throw an exception for documents opened in reading mode (see /// <see cref="PageFlushingHelper"/> /// for more details on modes). It is also not advised to be used in stamping mode: even though it will indeed /// release the objects and free the memory, the released objects will definitely be re-read again on document /// closing, which would affect performance. /// </p> /// <p> /// When using this method in append mode (or in stamping mode), be careful not to try to modify the object instances /// obtained before this method call! See /// <see cref="PageFlushingHelper"/> /// for details on released and flushed objects state. /// </p> /// <p> /// This method shall be used only when it's known that the page and its inner structures processing is finished. /// This includes reading data from pages, page modification and page handling via addons/utilities. /// </p> /// </summary> /// <param name="pageNum">the page number which low level objects structure is to be flushed or released from memory. /// </param> public virtual void AppendModeFlush(int pageNum) { if (pdfDoc.GetWriter() == null) { throw new ArgumentException(PdfException.FlushingHelperFLushingModeIsNotForDocReadingMode); } PdfPage page = pdfDoc.GetPage(pageNum); if (page.IsFlushed()) { return; } page.GetDocument().DispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.END_PAGE, page)); bool pageWasModified = page.GetPdfObject().IsModified(); page.SetModified(); release = true; pageWasModified = FlushPage(pageNum) || pageWasModified; PdfArray annots = page.GetPdfObject().GetAsArray(PdfName.Annots); if (annots != null && !annots.IsFlushed()) { ArrayFlushIfModified(annots); } PdfObject thumb = page.GetPdfObject().Get(PdfName.Thumb, false); FlushIfModified(thumb); PdfObject contents = page.GetPdfObject().Get(PdfName.Contents, false); if (contents is PdfIndirectReference) { if (contents.CheckState(PdfObject.MODIFIED) && !contents.CheckState(PdfObject.FLUSHED)) { PdfObject contentsDirectObj = ((PdfIndirectReference)contents).GetRefersTo(); if (contentsDirectObj.IsArray()) { ArrayFlushIfModified((PdfArray)contentsDirectObj); } else { contentsDirectObj.Flush(); } } } else { // already checked that modified if (contents is PdfArray) { ArrayFlushIfModified((PdfArray)contents); } else { if (contents is PdfStream) { FlushIfModified(contents); } } } // Page tags flushing is supported only in PdfPage#flush and #unsafeFlushDeep: it makes sense to flush tags // completely for heavily modified or new pages. For the slightly modified pages it should be enough to release // the tag structure objects via tag structure releasing utility. if (!pageWasModified) { page.GetPdfObject().GetIndirectReference().ClearState(PdfObject.MODIFIED); pdfDoc.GetCatalog().GetPageTree().ReleasePage(pageNum); page.UnsetForbidRelease(); page.GetPdfObject().Release(); } else { // inherited and modified resources are handled in #flushPage call in the beginning of method page.ReleaseInstanceFields(); page.GetPdfObject().Flush(); } }