/// <summary>Adds object to the object stream.</summary> /// <param name="object">object to add.</param> public virtual void AddObject(PdfObject @object) { if (size.IntValue() == MAX_OBJ_STREAM_SIZE) { throw new PdfException(PdfException.PdfObjectStreamReachMaxSize); } PdfOutputStream outputStream = GetOutputStream(); indexStream.WriteInteger(@object.GetIndirectReference().GetObjNumber()).WriteSpace().WriteLong(outputStream .GetCurrentPos()).WriteSpace(); outputStream.Write(@object); @object.GetIndirectReference().SetObjStreamNumber(GetIndirectReference().GetObjNumber()); @object.GetIndirectReference().SetIndex(size.IntValue()); outputStream.WriteSpace(); size.Increment(); ((PdfNumber)Get(PdfName.First)).SetValue(indexStream.GetCurrentPos()); }
/// <summary>Returns the value associated to this key as an Integer.</summary> /// <remarks>Returns the value associated to this key as an Integer. 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>Integer associated with this key</returns> public virtual int?GetAsInt(PdfName key) { PdfNumber number = GetAsNumber(key); int? intNumber = null; if (number != null) { intNumber = number.IntValue(); } return(intNumber); }
public virtual int GetRotation() { PdfNumber rotate = GetPdfObject().GetAsNumber(PdfName.Rotate); if (rotate == null) { return(0); } else { int n = rotate.IntValue(); n %= 360; return(n < 0 ? n + 360 : n); } }
/// <exception cref="System.IO.IOException"/> internal virtual void VerifyPagesOrder(String filename, int numOfPages) { PdfReader reader = new PdfReader(filename); PdfDocument pdfDocument = new PdfDocument(reader); NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt"); for (int i = 1; i <= pdfDocument.GetNumberOfPages(); i++) { PdfDictionary page = pdfDocument.GetPage(i).GetPdfObject(); NUnit.Framework.Assert.IsNotNull(page); PdfNumber number = page.GetAsNumber(PageNum5); NUnit.Framework.Assert.AreEqual(i, number.IntValue(), "Page number"); } NUnit.Framework.Assert.AreEqual(numOfPages, pdfDocument.GetNumberOfPages(), "Number of pages"); pdfDocument.Close(); }
public virtual int?GetStructParentIndex() { if (structParents == -1) { PdfNumber n = GetPdfObject().GetAsNumber(PdfName.StructParents); if (n != null) { structParents = n.IntValue(); } else { structParents = (int)GetDocument().GetNextStructParentIndex(); } } return(structParents); }
private void Write(PdfNumber pdfNumber) { if (pdfNumber.HasContent()) { WriteBytes(pdfNumber.GetInternalContent()); } else { if (pdfNumber.IsDoubleNumber()) { WriteDouble(pdfNumber.GetValue()); } else { WriteInteger(pdfNumber.IntValue()); } } }
private void Write(PdfStream pdfStream) { try { bool userDefinedCompression = pdfStream.GetCompressionLevel() != CompressionConstants.UNDEFINED_COMPRESSION; if (!userDefinedCompression) { int defaultCompressionLevel = document != null?document.GetWriter().GetCompressionLevel() : CompressionConstants .DEFAULT_COMPRESSION; pdfStream.SetCompressionLevel(defaultCompressionLevel); } bool toCompress = pdfStream.GetCompressionLevel() != CompressionConstants.NO_COMPRESSION; bool allowCompression = !pdfStream.ContainsKey(PdfName.Filter) && IsNotMetadataPdfStream(pdfStream); if (pdfStream.GetInputStream() != null) { Stream fout = this; DeflaterOutputStream def = null; OutputStreamEncryption ose = null; if (crypto != null && !crypto.IsEmbeddedFilesOnly()) { fout = ose = crypto.GetEncryptionStream(fout); } if (toCompress && (allowCompression || userDefinedCompression)) { UpdateCompressionFilter(pdfStream); fout = def = new DeflaterOutputStream(fout, pdfStream.GetCompressionLevel(), 0x8000); } this.Write((PdfDictionary)pdfStream); WriteBytes(iText.Kernel.Pdf.PdfOutputStream.stream); long beginStreamContent = GetCurrentPos(); byte[] buf = new byte[4192]; while (true) { int n = pdfStream.GetInputStream().Read(buf); if (n <= 0) { break; } fout.Write(buf, 0, n); } if (def != null) { def.Finish(); } if (ose != null) { ose.Finish(); } PdfNumber length = pdfStream.GetAsNumber(PdfName.Length); length.SetValue((int)(GetCurrentPos() - beginStreamContent)); pdfStream.UpdateLength(length.IntValue()); WriteBytes(iText.Kernel.Pdf.PdfOutputStream.endstream); } else { //When document is opened in stamping mode the output stream can be uninitialized. //We have to initialize it and write all data from streams input to streams output. if (pdfStream.GetOutputStream() == null && pdfStream.GetIndirectReference().GetReader() != null) { // If new specific compression is set for stream, // then compressed stream should be decoded and written with new compression settings byte[] bytes = pdfStream.GetIndirectReference().GetReader().ReadStreamBytes(pdfStream, false); if (userDefinedCompression) { bytes = DecodeFlateBytes(pdfStream, bytes); } pdfStream.InitOutputStream(new ByteArrayOutputStream(bytes.Length)); pdfStream.GetOutputStream().Write(bytes); } System.Diagnostics.Debug.Assert(pdfStream.GetOutputStream() != null, "PdfStream lost OutputStream"); ByteArrayOutputStream byteArrayStream; try { if (toCompress && !ContainsFlateFilter(pdfStream) && (allowCompression || userDefinedCompression)) { // compress UpdateCompressionFilter(pdfStream); byteArrayStream = new ByteArrayOutputStream(); DeflaterOutputStream zip = new DeflaterOutputStream(byteArrayStream, pdfStream.GetCompressionLevel()); if (pdfStream is PdfObjectStream) { PdfObjectStream objectStream = (PdfObjectStream)pdfStream; ((ByteArrayOutputStream)objectStream.GetIndexStream().GetOutputStream()).WriteTo(zip); ((ByteArrayOutputStream)objectStream.GetOutputStream().GetOutputStream()).WriteTo(zip); } else { System.Diagnostics.Debug.Assert(pdfStream.GetOutputStream() != null, "Error in outputStream"); ((ByteArrayOutputStream)pdfStream.GetOutputStream().GetOutputStream()).WriteTo(zip); } zip.Finish(); } else { if (pdfStream is PdfObjectStream) { PdfObjectStream objectStream = (PdfObjectStream)pdfStream; byteArrayStream = new ByteArrayOutputStream(); ((ByteArrayOutputStream)objectStream.GetIndexStream().GetOutputStream()).WriteTo(byteArrayStream); ((ByteArrayOutputStream)objectStream.GetOutputStream().GetOutputStream()).WriteTo(byteArrayStream); } else { System.Diagnostics.Debug.Assert(pdfStream.GetOutputStream() != null, "Error in outputStream"); byteArrayStream = (ByteArrayOutputStream)pdfStream.GetOutputStream().GetOutputStream(); } } if (CheckEncryption(pdfStream)) { ByteArrayOutputStream encodedStream = new ByteArrayOutputStream(); OutputStreamEncryption ose = crypto.GetEncryptionStream(encodedStream); byteArrayStream.WriteTo(ose); ose.Finish(); byteArrayStream = encodedStream; } } catch (System.IO.IOException ioe) { throw new PdfException(PdfException.IoException, ioe); } pdfStream.Put(PdfName.Length, new PdfNumber(byteArrayStream.Length)); pdfStream.UpdateLength((int)byteArrayStream.Length); this.Write((PdfDictionary)pdfStream); WriteBytes(iText.Kernel.Pdf.PdfOutputStream.stream); byteArrayStream.WriteTo(this); byteArrayStream.Dispose(); WriteBytes(iText.Kernel.Pdf.PdfOutputStream.endstream); } } catch (System.IO.IOException e) { throw new PdfException(PdfException.CannotWriteToPdfStream, e, pdfStream); } }
private int ReadAndSetCryptoModeForPubSecHandler(PdfDictionary encDict) { int cryptoMode; int length = 0; PdfNumber vValue = encDict.GetAsNumber(PdfName.V); if (vValue == null) { throw new PdfException(PdfException.IllegalVValue); } int v = vValue.IntValue(); switch (v) { case 1: { cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_40; length = 40; break; } case 2: { PdfNumber lengthValue = encDict.GetAsNumber(PdfName.Length); if (lengthValue == null) { throw new PdfException(PdfException.IllegalLengthValue); } length = lengthValue.IntValue(); if (length > 128 || length < 40 || length % 8 != 0) { throw new PdfException(PdfException.IllegalLengthValue); } cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_128; break; } case 4: case 5: { PdfDictionary dic = encDict.GetAsDictionary(PdfName.CF); if (dic == null) { throw new PdfException(PdfException.CfNotFoundEncryption); } dic = (PdfDictionary)dic.Get(PdfName.DefaultCryptFilter); if (dic == null) { throw new PdfException(PdfException.DefaultcryptfilterNotFoundEncryption); } if (PdfName.V2.Equals(dic.Get(PdfName.CFM))) { cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_128; length = 128; } else { if (PdfName.AESV2.Equals(dic.Get(PdfName.CFM))) { cryptoMode = EncryptionConstants.ENCRYPTION_AES_128; length = 128; } else { if (PdfName.AESV3.Equals(dic.Get(PdfName.CFM))) { cryptoMode = EncryptionConstants.ENCRYPTION_AES_256; length = 256; } else { throw new PdfException(PdfException.NoCompatibleEncryptionFound); } } } PdfBoolean em = dic.GetAsBoolean(PdfName.EncryptMetadata); if (em != null && !em.GetValue()) { cryptoMode |= EncryptionConstants.DO_NOT_ENCRYPT_METADATA; } break; } default: { throw new PdfException(PdfException.UnknownEncryptionTypeVEq1, vValue); } } return(SetCryptoMode(cryptoMode, length)); }
private int ReadAndSetCryptoModeForStdHandler(PdfDictionary encDict) { int cryptoMode; int length = 0; PdfNumber rValue = encDict.GetAsNumber(PdfName.R); if (rValue == null) { throw new PdfException(PdfException.IllegalRValue); } int revision = rValue.IntValue(); switch (revision) { case 2: { cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_40; break; } case 3: { PdfNumber lengthValue = encDict.GetAsNumber(PdfName.Length); if (lengthValue == null) { throw new PdfException(PdfException.IllegalLengthValue); } length = lengthValue.IntValue(); if (length > 128 || length < 40 || length % 8 != 0) { throw new PdfException(PdfException.IllegalLengthValue); } cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_128; break; } case 4: { PdfDictionary dic = (PdfDictionary)encDict.Get(PdfName.CF); if (dic == null) { throw new PdfException(PdfException.CfNotFoundEncryption); } dic = (PdfDictionary)dic.Get(PdfName.StdCF); if (dic == null) { throw new PdfException(PdfException.StdcfNotFoundEncryption); } if (PdfName.V2.Equals(dic.Get(PdfName.CFM))) { cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_128; } else { if (PdfName.AESV2.Equals(dic.Get(PdfName.CFM))) { cryptoMode = EncryptionConstants.ENCRYPTION_AES_128; } else { throw new PdfException(PdfException.NoCompatibleEncryptionFound); } } PdfBoolean em = encDict.GetAsBoolean(PdfName.EncryptMetadata); if (em != null && !em.GetValue()) { cryptoMode |= EncryptionConstants.DO_NOT_ENCRYPT_METADATA; } break; } case 5: case 6: { cryptoMode = EncryptionConstants.ENCRYPTION_AES_256; PdfBoolean em5 = encDict.GetAsBoolean(PdfName.EncryptMetadata); if (em5 != null && !em5.GetValue()) { cryptoMode |= EncryptionConstants.DO_NOT_ENCRYPT_METADATA; } break; } default: { throw new PdfException(PdfException.UnknownEncryptionTypeREq1).SetMessageParams(rValue); } } revision = SetCryptoMode(cryptoMode, length); return(revision); }