protected internal virtual bool ContainsFlateFilter(PdfStream pdfStream) { PdfObject filter = pdfStream.Get(PdfName.Filter); if (filter != null) { if (filter.GetObjectType() == PdfObject.NAME) { if (PdfName.FlateDecode.Equals(filter)) { return(true); } } else { if (filter.GetObjectType() == PdfObject.ARRAY) { if (((PdfArray)filter).Contains(PdfName.FlateDecode)) { return(true); } } else { throw new PdfException(PdfException.FilterIsNotANameOrArray); } } } return(false); }
protected internal virtual bool CheckEncryption(PdfStream pdfStream) { if (crypto == null || crypto.IsEmbeddedFilesOnly()) { return(false); } else { PdfObject filter = pdfStream.Get(PdfName.Filter, true); if (filter != null) { if (PdfName.Crypt.Equals(filter)) { return(false); } else { if (filter.GetObjectType() == PdfObject.ARRAY) { PdfArray filters = (PdfArray)filter; if (!filters.IsEmpty() && PdfName.Crypt.Equals(filters.Get(0, true))) { return(false); } } } } return(true); } }
public virtual PdfName AddImage(PdfObject image) { if (image.GetObjectType() != PdfObject.STREAM) { throw new PdfException(PdfException.CannotAddNonStreamImageToResources1).SetMessageParams(image.GetType(). ToString()); } return(AddResource(image, imageNamesGen)); }
public virtual PdfName AddProperties(PdfObject properties) { if (properties.GetObjectType() != PdfObject.DICTIONARY) { throw new PdfException(PdfException.CannotAddNonDictionaryPropertiesToResources1).SetMessageParams(properties .GetType().ToString()); } return(AddResource(properties, propNamesGen)); }
public virtual PdfName AddExtGState(PdfObject extGState) { if (extGState.GetObjectType() != PdfObject.DICTIONARY) { throw new PdfException(PdfException.CannotAddNonDictionaryExtGStateToResources1).SetMessageParams(extGState .GetType().ToString()); } return(AddResource(extGState, egsNamesGen)); }
public virtual PdfName AddForm(PdfObject form) { if (form.GetObjectType() != PdfObject.STREAM) { throw new PdfException(PdfException.CannotAddNonStreamFormToResources1).SetMessageParams(form.GetType().ToString ()); } return(AddResource(form, formNamesGen)); }
/// <summary>Returns the value associated to this key as a PdfBoolean.</summary> /// <remarks>Returns the value associated to this key as a PdfBoolean. If the value isn't a PdfBoolean, null is returned. /// </remarks> /// <param name="key">the key of which the associated value needs to be returned</param> /// <returns>PdfBoolean associated with this key</returns> public virtual PdfBoolean GetAsBoolean(PdfName key) { PdfObject direct = Get(key, true); if (direct != null && direct.GetObjectType() == PdfObject.BOOLEAN) { return((PdfBoolean)direct); } return(null); }
/// <summary>Returns the value associated to this key as a PdfString.</summary> /// <remarks>Returns the value associated to this key as a PdfString. If the value isn't a PdfString, null is returned. /// </remarks> /// <param name="key">the key of which the associated value needs to be returned</param> /// <returns>PdfString associated with this key</returns> public virtual PdfString GetAsString(PdfName key) { PdfObject direct = Get(key, true); if (direct != null && direct.GetObjectType() == PdfObject.STRING) { return((PdfString)direct); } return(null); }
/// <summary>Returns the value associated to this key as a PdfNumber.</summary> /// <remarks>Returns the value associated to this key as a PdfNumber. If the value isn't a PdfNumber, null is returned. /// </remarks> /// <param name="key">the key of which the associated value needs to be returned</param> /// <returns>PdfNumber associated with this key</returns> public virtual PdfNumber GetAsNumber(PdfName key) { PdfObject direct = Get(key, true); if (direct != null && direct.GetObjectType() == PdfObject.NUMBER) { return((PdfNumber)direct); } return(null); }
/// <summary>Returns the value associated to this key as a PdfStream.</summary> /// <remarks>Returns the value associated to this key as a PdfStream. If the value isn't a PdfStream, null is returned. /// </remarks> /// <param name="key">the key of which the associated value needs to be returned</param> /// <returns>PdfStream associated with this key</returns> public virtual PdfStream GetAsStream(PdfName key) { PdfObject direct = Get(key, true); if (direct != null && direct.GetObjectType() == PdfObject.STREAM) { return((PdfStream)direct); } return(null); }
/// <summary>Returns the value associated to this key as a PdfDictionary.</summary> /// <remarks>Returns the value associated to this key as a PdfDictionary. If the value isn't a PdfDictionary, null is returned. /// </remarks> /// <param name="key">the key of which the associated value needs to be returned</param> /// <returns>PdfDictionary associated with this key</returns> public virtual iText.Kernel.Pdf.PdfDictionary GetAsDictionary(PdfName key) { PdfObject direct = Get(key, true); if (direct != null && direct.GetObjectType() == PdfObject.DICTIONARY) { return((iText.Kernel.Pdf.PdfDictionary)direct); } return(null); }
/// <summary>Returns the value associated to this key as a PdfArray.</summary> /// <remarks>Returns the value associated to this key as a PdfArray. If the value isn't a PdfArray, null is returned. /// </remarks> /// <param name="key">the key of which the associated value needs to be returned</param> /// <returns>PdfArray associated with this key</returns> public virtual PdfArray GetAsArray(PdfName key) { PdfObject direct = Get(key, true); if (direct != null && direct.GetObjectType() == PdfObject.ARRAY) { return((PdfArray)direct); } return(null); }
/// <summary>Returns the value associated to this key as a PdfName.</summary> /// <remarks>Returns the value associated to this key as a PdfName. If the value isn't a PdfName, null is returned. /// </remarks> /// <param name="key">the key of which the associated value needs to be returned</param> /// <returns>PdfName associated with this key</returns> public virtual PdfName GetAsName(PdfName key) { PdfObject direct = Get(key, true); if (direct != null && direct.GetObjectType() == PdfObject.NAME) { return((PdfName)direct); } return(null); }
private void Write(PdfDictionary pdfDictionary) { WriteBytes(openDict); foreach (PdfName key in pdfDictionary.KeySet()) { bool isAlreadyWriteSpace = false; Write(key); PdfObject value = pdfDictionary.Get(key, false); if (value == null) { ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Pdf.PdfOutputStream)); logger.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.INVALID_KEY_VALUE_KEY_0_HAS_NULL_VALUE, key )); value = PdfNull.PDF_NULL; } if ((value.GetObjectType() == PdfObject.NUMBER || value.GetObjectType() == PdfObject.LITERAL || value.GetObjectType () == PdfObject.BOOLEAN || value.GetObjectType() == PdfObject.NULL || value.GetObjectType() == PdfObject .INDIRECT_REFERENCE || value.CheckState(PdfObject.MUST_BE_INDIRECT))) { isAlreadyWriteSpace = true; WriteSpace(); } PdfIndirectReference indirectReference; if ((indirectReference = value.GetIndirectReference()) != null) { if (!isAlreadyWriteSpace) { WriteSpace(); } Write(indirectReference); } else { Write(value); } } WriteBytes(closeDict); }
private void MarkObjectToFlush(PdfObject pdfObject) { if (pdfObject != null) { PdfIndirectReference indirectReference = pdfObject.GetIndirectReference(); if (indirectReference != null) { if (!indirectReference.CheckState(PdfObject.FLUSHED)) { indirectReference.SetState(PdfObject.MUST_BE_FLUSHED); } } else { if (pdfObject.GetObjectType() == PdfObject.INDIRECT_REFERENCE) { if (!pdfObject.CheckState(PdfObject.FLUSHED)) { pdfObject.SetState(PdfObject.MUST_BE_FLUSHED); } } else { if (pdfObject.GetObjectType() == PdfObject.ARRAY) { MarkArrayContentToFlush((PdfArray)pdfObject); } else { if (pdfObject.GetObjectType() == PdfObject.DICTIONARY) { MarkDictionaryContentToFlush((PdfDictionary)pdfObject); } } } } } }
/// <summary>Flushes the object.</summary> /// <remarks>Flushes the object. Override this method if you want to define custom behaviour for object flushing. /// </remarks> /// <param name="pdfObject">object to flush.</param> /// <param name="canBeInObjStm">indicates whether object can be placed into object stream.</param> /// <exception cref="System.IO.IOException">on error.</exception> protected internal virtual void FlushObject(PdfObject pdfObject, bool canBeInObjStm) { PdfIndirectReference indirectReference = pdfObject.GetIndirectReference(); if (IsFullCompression() && canBeInObjStm) { PdfObjectStream objectStream = GetObjectStream(); objectStream.AddObject(pdfObject); } else { indirectReference.SetOffset(GetCurrentPos()); WriteToBody(pdfObject); } ((PdfIndirectReference)indirectReference.SetState(PdfObject.FLUSHED)).ClearState(PdfObject.MUST_BE_FLUSHED ); switch (pdfObject.GetObjectType()) { case PdfObject.BOOLEAN: case PdfObject.NAME: case PdfObject.NULL: case PdfObject.NUMBER: case PdfObject.STRING: { ((PdfPrimitiveObject)pdfObject).content = null; break; } case PdfObject.ARRAY: { PdfArray array = ((PdfArray)pdfObject); MarkArrayContentToFlush(array); array.ReleaseContent(); break; } case PdfObject.STREAM: case PdfObject.DICTIONARY: { PdfDictionary dictionary = ((PdfDictionary)pdfObject); MarkDictionaryContentToFlush(dictionary); dictionary.ReleaseContent(); break; } case PdfObject.INDIRECT_REFERENCE: { MarkObjectToFlush(((PdfIndirectReference)pdfObject).GetRefersTo(false)); break; } } }
/// <param name="asDirect">true is to extract direct object always.</param> public virtual PdfObject Get(PdfName key, bool asDirect) { if (!asDirect) { return(map.Get(key)); } else { PdfObject obj = map.Get(key); if (obj != null && obj.GetObjectType() == INDIRECT_REFERENCE) { return(((PdfIndirectReference)obj).GetRefersTo(true)); } else { return(obj); } } }
// For internal usage only /// <summary>Write a PdfObject to the outputstream.</summary> /// <param name="pdfObject">PdfObject to write</param> /// <returns>this PdfOutPutStream</returns> public virtual iText.Kernel.Pdf.PdfOutputStream Write(PdfObject pdfObject) { if (pdfObject.CheckState(PdfObject.MUST_BE_INDIRECT) && document != null) { pdfObject.MakeIndirect(document); pdfObject = pdfObject.GetIndirectReference(); } if (pdfObject.CheckState(PdfObject.READ_ONLY)) { throw new PdfException(PdfException.CannotWriteObjectAfterItWasReleased); } switch (pdfObject.GetObjectType()) { case PdfObject.ARRAY: { Write((PdfArray)pdfObject); break; } case PdfObject.DICTIONARY: { Write((PdfDictionary)pdfObject); break; } case PdfObject.INDIRECT_REFERENCE: { Write((PdfIndirectReference)pdfObject); break; } case PdfObject.NAME: { Write((PdfName)pdfObject); break; } case PdfObject.NULL: case PdfObject.BOOLEAN: { Write((PdfPrimitiveObject)pdfObject); break; } case PdfObject.LITERAL: { Write((PdfLiteral)pdfObject); break; } case PdfObject.STRING: { Write((PdfString)pdfObject); break; } case PdfObject.NUMBER: { Write((PdfNumber)pdfObject); break; } case PdfObject.STREAM: { Write((PdfStream)pdfObject); break; } } return(this); }
protected internal virtual byte[] DecodeFlateBytes(PdfStream stream, byte[] bytes) { PdfObject filterObject = stream.Get(PdfName.Filter); if (filterObject == null) { return(bytes); } // check if flateDecode filter is on top PdfName filterName; PdfArray filtersArray = null; if (filterObject is PdfName) { filterName = (PdfName)filterObject; } else { if (filterObject is PdfArray) { filtersArray = (PdfArray)filterObject; filterName = filtersArray.GetAsName(0); } else { throw new PdfException(PdfException.FilterIsNotANameOrArray); } } if (!PdfName.FlateDecode.Equals(filterName)) { return(bytes); } // get decode params if present PdfDictionary decodeParams; PdfArray decodeParamsArray = null; PdfObject decodeParamsObject = stream.Get(PdfName.DecodeParms); if (decodeParamsObject == null) { decodeParams = null; } else { if (decodeParamsObject.GetObjectType() == PdfObject.DICTIONARY) { decodeParams = (PdfDictionary)decodeParamsObject; } else { if (decodeParamsObject.GetObjectType() == PdfObject.ARRAY) { decodeParamsArray = (PdfArray)decodeParamsObject; decodeParams = decodeParamsArray.GetAsDictionary(0); } else { throw new PdfException(PdfException.DecodeParameterType1IsNotSupported).SetMessageParams(decodeParamsObject .GetType().ToString()); } } } // decode byte[] res = FlateDecodeFilter.FlateDecode(bytes, true); if (res == null) { res = FlateDecodeFilter.FlateDecode(bytes, false); } bytes = FlateDecodeFilter.DecodePredictor(res, decodeParams); //remove filter and decode params filterObject = null; if (filtersArray != null) { filtersArray.Remove(0); if (filtersArray.Size() == 1) { filterObject = filtersArray.Get(0); } else { if (!filtersArray.IsEmpty()) { filterObject = filtersArray; } } } decodeParamsObject = null; if (decodeParamsArray != null) { decodeParamsArray.Remove(0); if (decodeParamsArray.Size() == 1 && decodeParamsArray.Get(0).GetObjectType() != PdfObject.NULL) { decodeParamsObject = decodeParamsArray.Get(0); } else { if (!decodeParamsArray.IsEmpty()) { decodeParamsObject = decodeParamsArray; } } } if (filterObject == null) { stream.Remove(PdfName.Filter); } else { stream.Put(PdfName.Filter, filterObject); } if (decodeParamsObject == null) { stream.Remove(PdfName.DecodeParms); } else { stream.Put(PdfName.DecodeParms, decodeParamsObject); } return(bytes); }
private void LoadPage(int pageNum) { PdfDictionary targetPage = pageRefs[pageNum]; if (targetPage != null) { return; } //if we go here, we have to split PdfPages that contains pageNum int parentIndex = FindPageParent(pageNum); PdfPages parent = parents[parentIndex]; PdfArray kids = parent.GetKids(); if (kids == null) { throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1); } int kidsCount = parent.GetCount(); // we should handle separated pages, it means every PdfArray kids must contain either PdfPage or PdfPages, // mix of PdfPage and PdfPages not allowed. bool findPdfPages = false; // NOTE optimization? when we already found needed index for (int i = 0; i < kids.Size(); i++) { PdfDictionary page = kids.GetAsDictionary(i); if (page == null) { // null values not allowed in pages tree. throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1); } PdfObject pageKids = page.Get(PdfName.Kids); if (pageKids != null) { if (pageKids.GetObjectType() == PdfObject.ARRAY) { findPdfPages = true; } else { // kids must be of type array throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1); } } } if (findPdfPages) { // handle mix of PdfPage and PdfPages. // handle count property! IList <PdfPages> newParents = new List <PdfPages>(kids.Size()); PdfPages lastPdfPages = null; for (int i_1 = 0; i_1 < kids.Size() && kidsCount > 0; i_1++) { PdfDictionary pdfPagesObject = kids.GetAsDictionary(i_1); if (pdfPagesObject.GetAsArray(PdfName.Kids) == null) { // pdfPagesObject is PdfPage if (lastPdfPages == null) { // possible if only first kid is PdfPage lastPdfPages = new PdfPages(parent.GetFrom(), document, parent); kids.Set(i_1, lastPdfPages.GetPdfObject()); newParents.Add(lastPdfPages); } lastPdfPages.AddPage(pdfPagesObject); kids.Remove(i_1); i_1--; kidsCount--; } else { // pdfPagesObject is PdfPages int from = lastPdfPages == null?parent.GetFrom() : lastPdfPages.GetFrom() + lastPdfPages.GetCount(); lastPdfPages = new PdfPages(from, kidsCount, pdfPagesObject, parent); newParents.Add(lastPdfPages); kidsCount -= lastPdfPages.GetCount(); } } parents.JRemoveAt(parentIndex); for (int i_2 = newParents.Count - 1; i_2 >= 0; i_2--) { parents.Add(parentIndex, newParents[i_2]); } // recursive call, to load needed pageRef. // NOTE optimization? add to loadPage startParentIndex. LoadPage(pageNum); } else { int from = parent.GetFrom(); // Possible exception in case kids.getSize() < parent.getCount(). // In any case parent.getCount() has higher priority. // NOTE optimization? when we already found needed index for (int i_1 = 0; i_1 < parent.GetCount(); i_1++) { pageRefs[from + i_1] = kids.GetAsDictionary(i_1); } } }