private static void GetUsedNonFlushedOCGsFromXObject(PdfDictionary toXObject, PdfDictionary fromXObject, ICollection <PdfIndirectReference> fromUsedOcgs, PdfDictionary toOcProperties) { if (toXObject != null && !toXObject.IsFlushed()) { if (toXObject.IsStream() && !toXObject.IsFlushed()) { PdfStream toStream = (PdfStream)toXObject; PdfStream fromStream = (PdfStream)fromXObject; iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromOcDict(toStream.GetAsDictionary(PdfName.OC), fromStream.GetAsDictionary(PdfName.OC), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromResources(toStream.GetAsDictionary(PdfName.Resources ), fromStream.GetAsDictionary(PdfName.Resources), fromUsedOcgs, toOcProperties); } else { foreach (PdfName name in toXObject.KeySet()) { PdfObject toCurrObj = toXObject.Get(name); PdfObject fromCurrObj = fromXObject.Get(name); if (toCurrObj.IsStream() && !toCurrObj.IsFlushed()) { PdfStream toStream = (PdfStream)toCurrObj; PdfStream fromStream = (PdfStream)fromCurrObj; iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toStream, fromStream, fromUsedOcgs, toOcProperties); } } } } }
public virtual void SetLinkDestinationToPageAppendMode() { String input = sourceFolder + "100pages.pdf"; String output = destinationFolder + "setLinkDestinationToPageAppendMode.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(input), new PdfWriter(output), new StampingProperties() .UseAppendMode()); PdfPage page1 = pdfDoc.GetPage(1); PdfPage page2 = pdfDoc.GetPage(2); PdfIndirectReference page1IndRef = page1.GetPdfObject().GetIndirectReference(); PdfIndirectReference page2IndRef = page2.GetPdfObject().GetIndirectReference(); PdfDictionary aDict = ((PdfLinkAnnotation)page1.GetAnnotations()[0]).GetAction(); new PdfAction(aDict).Put(PdfName.D, PdfExplicitDestination.CreateXYZ(page2, 300, 400, 1).GetPdfObject()); PageFlushingHelper flushingHelper = new PageFlushingHelper(pdfDoc); flushingHelper.AppendModeFlush(2); flushingHelper.UnsafeFlushDeep(1); // annotation is flushed NUnit.Framework.Assert.IsTrue(aDict.IsFlushed()); // page is not flushed NUnit.Framework.Assert.IsFalse(page1IndRef.CheckState(PdfObject.FLUSHED)); // page is released NUnit.Framework.Assert.IsNull(page1IndRef.refersTo); // page is not flushed NUnit.Framework.Assert.IsFalse(page2IndRef.CheckState(PdfObject.FLUSHED)); // page is released NUnit.Framework.Assert.IsNull(page2IndRef.refersTo); // exception is not thrown pdfDoc.Close(); }
private static void GetUsedNonFlushedOCGsFromResources(PdfDictionary toResources, PdfDictionary fromResources , ICollection<PdfIndirectReference> fromUsedOcgs, PdfDictionary toOcProperties) { if (toResources != null && !toResources.IsFlushed()) { // Copy OCGs from properties PdfDictionary toProperties = toResources.GetAsDictionary(PdfName.Properties); PdfDictionary fromProperties = fromResources.GetAsDictionary(PdfName.Properties); if (toProperties != null && !toProperties.IsFlushed()) { foreach (PdfName name in toProperties.KeySet()) { PdfObject toCurrObj = toProperties.Get(name); PdfObject fromCurrObj = fromProperties.Get(name); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromOcDict(toCurrObj, fromCurrObj, fromUsedOcgs, toOcProperties); } } // Copy OCGs from xObject PdfDictionary toXObject = toResources.GetAsDictionary(PdfName.XObject); PdfDictionary fromXObject = fromResources.GetAsDictionary(PdfName.XObject); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toXObject, fromXObject, fromUsedOcgs , toOcProperties); } }
public virtual void ModifyAnnotationOnlyAppendMode() { String input = sourceFolder + "100pages.pdf"; String output = destinationFolder + "modifyAnnotOnly.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(input), new PdfWriter(output), new StampingProperties() .UseAppendMode()); PdfPage page = pdfDoc.GetPage(1); PdfIndirectReference pageIndRef = page.GetPdfObject().GetIndirectReference(); PdfDictionary annotObj = page.GetAnnotations()[0].SetRectangle(new PdfArray(new Rectangle(0, 0, 300, 300)) ).SetPage(page).GetPdfObject(); PageFlushingHelper flushingHelper = new PageFlushingHelper(pdfDoc); flushingHelper.AppendModeFlush(1); // annotation is flushed NUnit.Framework.Assert.IsTrue(annotObj.IsFlushed()); // page is not flushed NUnit.Framework.Assert.IsFalse(pageIndRef.CheckState(PdfObject.FLUSHED)); // page is released NUnit.Framework.Assert.IsNull(pageIndRef.refersTo); // exception is not thrown pdfDoc.Close(); }
private static ICollection<PdfIndirectReference> GetAllUsedNonFlushedOCGs(IDictionary<PdfPage, PdfPage> page2page , PdfDictionary toOcProperties) { // NOTE: the PDF is considered to be valid and therefore the presence of OСG in OCProperties.OCGs is not checked ICollection<PdfIndirectReference> fromUsedOcgs = new LinkedHashSet<PdfIndirectReference>(); // Visit the pages in parallel to find non-flush OSGs PdfPage[] fromPages = page2page.Keys.ToArray(new PdfPage[0]); PdfPage[] toPages = page2page.Values.ToArray(new PdfPage[0]); for (int i = 0; i < toPages.Length; i++) { PdfPage fromPage = fromPages[i]; PdfPage toPage = toPages[i]; // Copy OCGs from annotations IList<PdfAnnotation> toAnnotations = toPage.GetAnnotations(); IList<PdfAnnotation> fromAnnotations = fromPage.GetAnnotations(); for (int j = 0; j < toAnnotations.Count; j++) { if (!toAnnotations[j].IsFlushed()) { PdfDictionary toAnnotDict = toAnnotations[j].GetPdfObject(); PdfDictionary fromAnnotDict = fromAnnotations[j].GetPdfObject(); PdfAnnotation toAnnot = toAnnotations[j]; PdfAnnotation fromAnnot = fromAnnotations[j]; if (!toAnnotDict.IsFlushed()) { iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromOcDict(toAnnotDict.GetAsDictionary(PdfName.OC ), fromAnnotDict.GetAsDictionary(PdfName.OC), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetNormalAppearanceObject(), fromAnnot.GetNormalAppearanceObject(), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetRolloverAppearanceObject( ), fromAnnot.GetRolloverAppearanceObject(), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetDownAppearanceObject(), fromAnnot .GetDownAppearanceObject(), fromUsedOcgs, toOcProperties); } } } PdfDictionary toResources = toPage.GetPdfObject().GetAsDictionary(PdfName.Resources); PdfDictionary fromResources = fromPage.GetPdfObject().GetAsDictionary(PdfName.Resources); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromResources(toResources, fromResources, fromUsedOcgs , toOcProperties); } return fromUsedOcgs; }
public virtual void FlushSelfContainingObjectsWritingMode() { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new MemoryStream())); PdfDictionary pageDict = pdfDoc.AddNewPage().GetPdfObject(); PdfDictionary dict1 = new PdfDictionary(); pageDict.Put(new PdfName("dict1"), dict1); PdfArray arr1 = new PdfArray(); pageDict.Put(new PdfName("arr1"), arr1); dict1.Put(new PdfName("dict1"), dict1); dict1.Put(new PdfName("arr1"), arr1); arr1.Add(arr1); arr1.Add(dict1); arr1.MakeIndirect(pdfDoc); dict1.MakeIndirect(pdfDoc); PageFlushingHelper flushingHelper = new PageFlushingHelper(pdfDoc); flushingHelper.UnsafeFlushDeep(1); NUnit.Framework.Assert.IsTrue(dict1.IsFlushed()); NUnit.Framework.Assert.IsTrue(arr1.IsFlushed()); pdfDoc.Close(); }
private bool FlushPage(int pageNum) { PdfPage page = pdfDoc.GetPage(pageNum); if (page.IsFlushed()) { return(false); } bool pageChanged = false; if (!release) { pdfDoc.DispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.END_PAGE, page)); InitCurrentLayers(pdfDoc); } PdfDictionary pageDict = page.GetPdfObject(); // Using PdfPage package internal methods in order to avoid PdfResources initialization: initializing PdfResources // limits processing possibilities only to cases in which resources and specific resource type dictionaries are not flushed. PdfDictionary resourcesDict = page.InitResources(false); // inits /Resources dict entry if not inherited and not created yet PdfResources resources = page.GetResources(false); if (resources != null && resources.IsModified() && !resources.IsReadOnly()) { resourcesDict = resources.GetPdfObject(); pageDict.Put(PdfName.Resources, resources.GetPdfObject()); pageDict.SetModified(); pageChanged = true; } if (!resourcesDict.IsFlushed()) { FlushDictRecursively(resourcesDict, null); FlushOrRelease(resourcesDict); } FlushDictRecursively(pageDict, pageContext); if (release) { if (!page.GetPdfObject().IsModified()) { pdfDoc.GetCatalog().GetPageTree().ReleasePage(pageNum); page.UnsetForbidRelease(); page.GetPdfObject().Release(); } } else { if (pdfDoc.IsTagged() && !pdfDoc.GetStructTreeRoot().IsFlushed()) { page.TryFlushPageTags(); } if (!pdfDoc.IsAppendMode() || page.GetPdfObject().IsModified()) { page.ReleaseInstanceFields(); page.GetPdfObject().Flush(); } else { // it's append mode pdfDoc.GetCatalog().GetPageTree().ReleasePage(pageNum); page.UnsetForbidRelease(); page.GetPdfObject().Release(); } } layersRefs.Clear(); return(pageChanged); }