public virtual void PrimitivesTest() { String data = "<</Size 70." + "/Value#20 .1" + "/Root 46 0 R" + "/Info 44 0 R" + "/ID[<736f6d652068657820737472696e672>(some simple string )<8C2547D58D4BD2C6F3D32B830BE3259D2>-70.1--0.2]" + "/Name1 --15" + "/Prev ---116.23 >>"; RandomAccessSourceFactory factory = new RandomAccessSourceFactory(); PdfTokenizer tok = new PdfTokenizer(new RandomAccessFileOrArray(factory.CreateSource(data.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1 )))); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.StartDic); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name); PdfName name = new PdfName(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("Size", name.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number); PdfNumber num = new PdfNumber(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("70.", num.ToString()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name); name = new PdfName(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("Value ", name.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number); num = new PdfNumber(tok.GetByteContent()); NUnit.Framework.Assert.AreNotSame("0.1", num.ToString()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name); name = new PdfName(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("Root", name.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Ref); PdfIndirectReference @ref = new PdfIndirectReference(null, tok.GetObjNr(), tok.GetGenNr()); NUnit.Framework.Assert.AreEqual("46 0 R", @ref.ToString()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name); name = new PdfName(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("Info", name.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Ref); @ref = new PdfIndirectReference(null, tok.GetObjNr(), tok.GetGenNr()); NUnit.Framework.Assert.AreEqual("44 0 R", @ref.ToString()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name); name = new PdfName(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("ID", name.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.StartArray); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.String); NUnit.Framework.Assert.AreEqual(tok.IsHexString(), true); PdfString str = new PdfString(tok.GetByteContent(), tok.IsHexString()); NUnit.Framework.Assert.AreEqual("some hex string ", str.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.String); NUnit.Framework.Assert.AreEqual(tok.IsHexString(), false); str = new PdfString(tok.GetByteContent(), tok.IsHexString()); NUnit.Framework.Assert.AreEqual("some simple string ", str.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.String); NUnit.Framework.Assert.AreEqual(tok.IsHexString(), true); str = new PdfString(tok.GetByteContent(), tok.IsHexString()); NUnit.Framework.Assert.AreEqual("\u008C%G\u00D5\u008DK\u00D2\u00C6\u00F3\u00D3+\u0083\u000B\u00E3%\u009D " , str.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number); num = new PdfNumber(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("-70.1", num.ToString()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number); num = new PdfNumber(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("-0.2", num.ToString()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.EndArray); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name); name = new PdfName(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("Name1", name.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number); num = new PdfNumber(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("0", num.ToString()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name); name = new PdfName(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("Prev", name.GetValue()); tok.NextValidToken(); NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number); num = new PdfNumber(tok.GetByteContent()); NUnit.Framework.Assert.AreEqual("-116.23", num.ToString()); }
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); } }