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(); }
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; }