/// <summary>Use this method to get the <b>Optional Content Properties Dictionary</b>.</summary> /// <remarks> /// Use this method to get the <b>Optional Content Properties Dictionary</b>. /// Note that if you call this method, then the PdfDictionary with OCProperties will be /// generated from PdfOCProperties object right before closing the PdfDocument, /// so if you want to make low-level changes in Pdf structures themselves (PdfArray, PdfDictionary, etc), /// then you should address directly those objects, e.g.: /// <c> /// PdfCatalog pdfCatalog = pdfDoc.getCatalog(); /// PdfDictionary ocProps = pdfCatalog.getAsDictionary(PdfName.OCProperties); /// // manipulate with ocProps. /// </c> /// Also note that this method is implicitly called when creating a new PdfLayer instance, /// so you should either use hi-level logic of operating with layers, /// or manipulate low-level Pdf objects by yourself. /// </remarks> /// <param name="createIfNotExists"> /// true to create new /OCProperties entry in catalog if not exists, /// false to return null if /OCProperties entry in catalog is not present. /// </param> /// <returns>the Optional Content Properties Dictionary</returns> public virtual PdfOCProperties GetOCProperties(bool createIfNotExists) { if (ocProperties != null) { return(ocProperties); } else { PdfDictionary ocPropertiesDict = GetPdfObject().GetAsDictionary(PdfName.OCProperties); if (ocPropertiesDict != null) { if (GetDocument().GetWriter() != null) { ocPropertiesDict.MakeIndirect(GetDocument()); } ocProperties = new PdfOCProperties(ocPropertiesDict); } else { if (createIfNotExists) { ocProperties = new PdfOCProperties(GetDocument()); } } } return(ocProperties); }
private static void AssertLayerNames(PdfDocument outDocument, IList <String> layerNames) { NUnit.Framework.Assert.IsNotNull(outDocument.GetCatalog()); PdfOCProperties ocProperties = outDocument.GetCatalog().GetOCProperties(true); NUnit.Framework.Assert.IsNotNull(ocProperties); NUnit.Framework.Assert.AreEqual(layerNames.Count, ocProperties.GetLayers().Count); for (int i = 0; i < layerNames.Count; i++) { PdfLayer layer = ocProperties.GetLayers()[i]; NUnit.Framework.Assert.IsNotNull(layer); PdfDocumentUnitTest.AssertLayerNameEqual(layerNames[i], layer); } }
internal virtual PdfDictionary FillAndGetOcPropertiesDictionary() { if (ocProperties != null) { ocProperties.FillDictionary(false); GetPdfObject().Put(PdfName.OCProperties, ocProperties.GetPdfObject()); ocProperties = null; } if (GetPdfObject().GetAsDictionary(PdfName.OCProperties) == null) { PdfDictionary pdfDictionary = new PdfDictionary(); pdfDictionary.MakeIndirect(GetDocument()); GetDocument().GetCatalog().GetPdfObject().Put(PdfName.OCProperties, pdfDictionary); } return(GetPdfObject().GetAsDictionary(PdfName.OCProperties)); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); PdfCatalog catalog = pdfDoc.GetCatalog(); PdfOCProperties ocProps = catalog.GetOCProperties(true); PdfDictionary occd = (PdfDictionary)ocProps.GetPdfObject().Get(PdfName.D); PdfArray order = occd.GetAsArray(PdfName.Order); PdfObject nestedLayers = order.Get(0); PdfObject nestedLayerArray = order.Get(1); PdfObject groupedLayers = order.Get(2); PdfObject radioGroup = order.Get(3); order.Set(0, radioGroup); order.Set(1, nestedLayers); order.Set(2, nestedLayerArray); order.Set(3, groupedLayers); pdfDoc.Close(); }
public virtual void CopyPagesFlushedResources() { byte[] docBytes; using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { using (PdfDocument document = new PdfDocument(new PdfWriter(outputStream))) { PdfPage page = document.AddNewPage(); PdfResources pdfResource = page.GetResources(); PdfDictionary ocg = new PdfDictionary(); ocg.Put(PdfName.Type, PdfName.OCG); ocg.Put(PdfName.Name, new PdfString("name1")); ocg.MakeIndirect(document); pdfResource.AddProperties(ocg); pdfResource.MakeIndirect(document); PdfPage page2 = document.AddNewPage(); page2.SetResources(pdfResource); document.GetCatalog().GetOCProperties(true); } docBytes = outputStream.ToArray(); } PdfWriter writer = new PdfWriter(new ByteArrayOutputStream()); using (PdfDocument outDocument = new PdfDocument(writer)) { using (PdfDocument fromDocument = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) { fromDocument.CopyPagesTo(1, 1, outDocument); IList <String> layerNames = new List <String>(); layerNames.Add("name1"); PdfDocumentUnitTest.AssertLayerNames(outDocument, layerNames); outDocument.FlushCopiedObjects(fromDocument); fromDocument.CopyPagesTo(2, 2, outDocument); NUnit.Framework.Assert.IsNotNull(outDocument.GetCatalog()); PdfOCProperties ocProperties = outDocument.GetCatalog().GetOCProperties(false); NUnit.Framework.Assert.IsNotNull(ocProperties); NUnit.Framework.Assert.AreEqual(1, ocProperties.GetLayers().Count); PdfLayer layer = ocProperties.GetLayers()[0]; NUnit.Framework.Assert.IsTrue(layer.GetPdfObject().IsFlushed()); } } }
protected void FillOCProperties(bool erase) { if (vOCProperties == null) vOCProperties = new PdfOCProperties(); if (erase) { vOCProperties.Remove(PdfName.OCGS); vOCProperties.Remove(PdfName.D); } if (vOCProperties.Get(PdfName.OCGS) == null) { PdfArray gr = new PdfArray(); foreach (PdfLayer layer in documentOCG.Keys) { gr.Add(layer.Ref); } vOCProperties.Put(PdfName.OCGS, gr); } if (vOCProperties.Get(PdfName.D) != null) return; List<IPdfOCG> docOrder = new List<IPdfOCG>(documentOCGorder); for (ListIterator<IPdfOCG> it = new ListIterator<IPdfOCG>(docOrder); it.HasNext();) { PdfLayer layer = (PdfLayer)it.Next(); if (layer.Parent != null) it.Remove(); } PdfArray order = new PdfArray(); foreach (PdfLayer layer in docOrder) { GetOCGOrder(order, layer); } PdfDictionary d = new PdfDictionary(); vOCProperties.Put(PdfName.D, d); d.Put(PdfName.ORDER, order); PdfArray grx = new PdfArray(); foreach (PdfLayer layer in documentOCG.Keys) { if (!layer.On) grx.Add(layer.Ref); } if (grx.Size > 0) d.Put(PdfName.OFF, grx); if (OCGRadioGroup.Size > 0) d.Put(PdfName.RBGROUPS, OCGRadioGroup); if (OCGLocked.Size > 0) d.Put(PdfName.LOCKED, OCGLocked); AddASEvent(PdfName.VIEW, PdfName.ZOOM); AddASEvent(PdfName.VIEW, PdfName.VIEW); AddASEvent(PdfName.PRINT, PdfName.PRINT); AddASEvent(PdfName.EXPORT, PdfName.EXPORT); d.Put(PdfName.LISTMODE, PdfName.VISIBLEPAGES); }
protected override void CheckLayer(PdfWriter writer, int key, Object obj1) { if (obj1 is IPdfOCG) { } else if (obj1 is PdfOCProperties) { PdfOCProperties properties = (PdfOCProperties)obj1; List <PdfDictionary> configsList = new List <PdfDictionary>(); PdfDictionary d = properties.GetAsDict(PdfName.D); if (d != null) { configsList.Add(d); } PdfArray configs = properties.GetAsArray(PdfName.CONFIGS); if (configs != null) { for (int i = 0; i < configs.Size; i++) { PdfDictionary config = configs.GetAsDict(i); if (config != null) { configsList.Add(config); } } } HashSet2 <PdfObject> ocgs = new HashSet2 <PdfObject>(); PdfArray ocgsArray = properties.GetAsArray(PdfName.OCGS); if (ocgsArray != null) { for (int i = 0; i < ocgsArray.Size; i++) { ocgs.Add(ocgsArray[i]); } } HashSet2 <String> names = new HashSet2 <String>(); HashSet2 <PdfObject> order = new HashSet2 <PdfObject>(); foreach (PdfDictionary config in configsList) { PdfString name = config.GetAsString(PdfName.NAME); if (name == null) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("optional.content.configuration.dictionary.shall.contain.name.entry")); } String name1 = name.ToUnicodeString(); if (names.Contains(name1)) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("value.of.name.entry.shall.be.unique.amongst.all.optional.content.configuration.dictionaries")); } names.Add(name1); if (config.Contains(PdfName.AS)) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("the.as.key.shall.not.appear.in.any.optional.content.configuration.dictionary")); } PdfArray orderArray = config.GetAsArray(PdfName.ORDER); if (orderArray != null) { fillOrderRecursively(orderArray, order); } } if (order.Count != ocgs.Count) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("order.array.shall.contain.references.to.all.ocgs")); } ocgs.RetainAll(order); if (order.Count != ocgs.Count) { throw new PdfAConformanceException(MessageLocalization.GetComposedMessage("order.array.shall.contain.references.to.all.ocgs")); } } else { } }