public virtual void CreatePdfStreamByInputStream() { String filename = destinationFolder + "createPdfStreamByInputStream.pdf"; FileStream fos = new FileStream(filename, FileMode.Create); PdfWriter writer = new PdfWriter(fos); PdfDocument document = new PdfDocument(writer); document.GetDocumentInfo().SetAuthor("Alexander Chingarev").SetCreator("iText 6").SetTitle("Empty iText 6 Document" ); PdfPage page = document.AddNewPage(); page.Flush(); String streamContent = "Some text content with strange symbols ∞²"; PdfStream stream = new PdfStream(document, new MemoryStream(streamContent.GetBytes())); stream.Flush(); int streamIndirectNumber = stream.GetIndirectReference().GetObjNumber(); document.Close(); // com.itextpdf.text.pdf.PdfReader reader = new PdfReader(filename); // Assert.assertEquals("Rebuilt", false, reader.isRebuilt()); // Assert.assertNotNull(reader.getPageN(1)); // String date = reader.getDocumentInfo().get("CreationDate"); // Calendar cl = com.itextpdf.text.pdf.PdfDate.decode(date); // long diff = new GregorianCalendar().getTimeInMillis() - cl.getTimeInMillis(); // String message = "Unexpected creation date. Different from now is " + (float)diff/1000 + "s"; // Assert.assertTrue(message, diff < 5000); // reader.close(); PdfReader reader6 = new PdfReader(filename); document = new PdfDocument(reader6); NUnit.Framework.Assert.AreEqual(false, reader6.HasRebuiltXref(), "Rebuilt"); NUnit.Framework.Assert.AreEqual(false, reader6.HasFixedXref(), "Fixed"); PdfStream pdfStream = (PdfStream)document.GetXref().Get(streamIndirectNumber).GetRefersTo(); NUnit.Framework.Assert.AreEqual(streamContent.GetBytes(), pdfStream.GetBytes(), "Stream by InputStream"); document.Close(); }
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); } }