Beispiel #1
0
        ////////////////////////////////////////////////////////////////////
        // Write object to PDF file
        ////////////////////////////////////////////////////////////////////

        internal override void WriteObjectToPdfFile()
        {
            // shortcut
            PdfBinaryWriter PdfFile = Document.PdfFile;

            // add items to dictionary
            Dictionary.Add("/Subtype", "/Image");
            Dictionary.AddInteger("/Width", Chart.Width);
            Dictionary.AddInteger("/Height", Chart.Height);
            Dictionary.Add("/Filter", "/DCTDecode");
            Dictionary.Add("/ColorSpace", "/DeviceRGB");
            Dictionary.Add("/BitsPerComponent", "8");

            // write dictionary
            Dictionary.WriteToPdfFile();

            // output stream
            PdfFile.WriteString("stream\n");

            // save pdf file position
            Int64 streamStart = PdfFile.BaseStream.Position;

            // debug
            if (Document.Debug)
            {
                PdfFile.WriteString("*** CHART PLACE HOLDER ***");
            }

            // copy image file to output file
            else
            {
                // create memory stream
                MemoryStream MS = new MemoryStream();

                // save image to memory stream
                Chart.SaveImage(MS, ChartImageFormat.Jpeg);

                // image byte array
                Byte[] ByteContents = MS.GetBuffer();

                // encryption
                if (Document.Encryption != null)
                {
                    ByteContents = Document.Encryption.EncryptByteArray(ObjectNumber, ByteContents);
                }

                // write memory stream internal buffer to PDF file
                PdfFile.Write(ByteContents);

                // close and dispose memory stream
                MS.Close();
            }

            // save stream length
            ImageLengthObject.ContentsString.Append(((Int32)(PdfFile.BaseStream.Position - streamStart)).ToString());

            // output stream
            PdfFile.WriteString("\nendstream\nendobj\n");
            return;
        }
Beispiel #2
0
        /// <summary>
        /// PDF embedded file class constructor
        /// </summary>
        /// <param name="Document">Current document</param>
        /// <param name="FileName">File name</param>
        /// <param name="PdfFileName">PDF file name (see remarks)</param>
        /// <remarks>
        /// <para>
        /// FileName is the name of the source file on the disk.
        /// PDFFileName is the name of the as saved within the PDF document file.
        /// If PDFFileName is not given or it is set to null, the class takes
        /// the disk's file name without the path.
        /// </para>
        /// </remarks>
        public PdfEmbeddedFile
        (
            PdfDocument Document,
            String FileName,
            String PdfFileName = null
        ) : base(Document, ObjectType.Dictionary, "/Filespec")
        {
            // save file name
            this.FileName = FileName;

            // test exitance
            if (!File.Exists(FileName))
            {
                throw new ApplicationException("Embedded file " + FileName + " does not exist");
            }

            // get file length
            FileInfo FI = new FileInfo(FileName);

            if (FI.Length > Int32.MaxValue - 4095)
            {
                throw new ApplicationException("Embedded file " + FileName + " too long");
            }
            Int32 FileLength = (Int32)FI.Length;

            // translate file extension to mime type string
            MimeType = ExtToMime.TranslateExtToMime(FI.Extension);

            // create embedded file object
            PdfObject EmbeddedFile = new PdfObject(Document, ObjectType.Stream, "/EmbeddedFile");

            // save uncompressed file length
            EmbeddedFile.Dictionary.AddFormat("/Params", "<</Size {0}>>", FileLength);

            // file data content byte array
            Byte[] FileData = new Byte[FileLength];

            // load all the file's data
            FileStream DataStream = null;

            try
            {
                // open the file
                DataStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);

                // read all the file
                if (DataStream.Read(FileData, 0, FileLength) != FileLength)
                {
                    throw new Exception();
                }
            }

            // loading file failed
            catch (Exception)
            {
                throw new ApplicationException("Invalid media file: " + FileName);
            }

            // close the file
            DataStream.Close();

            // compress the data
            Byte[] FileDataComp = CompressStream(FileData);
            if (FileDataComp != null)
            {
                FileData = FileDataComp;
                EmbeddedFile.Dictionary.Add("/Filter", "/FlateDecode");
            }

            // encryption
            if (Document.Encryption != null)
            {
                FileData = Document.Encryption.EncryptByteArray(EmbeddedFile.ObjectNumber, FileData);
            }

            // add compressed file length
            EmbeddedFile.Dictionary.AddInteger("/Length", FileData.Length);

            // shortcut
            PdfBinaryWriter PdfFile = Document.PdfFile;

            // save file position for this object
            EmbeddedFile.FilePosition = PdfFile.BaseStream.Position;

            // write object header
            PdfFile.WriteFormat("{0} 0 obj\n", EmbeddedFile.ObjectNumber);

            // write dictionary
            EmbeddedFile.Dictionary.WriteToPdfFile();

            // output stream
            PdfFile.WriteString("stream\n");

            // debug
            if (Document.Debug)
            {
                PdfFile.WriteString("*** MEDIAFILE PLACE HOLDER ***");
            }

            // output embedded font
            else
            {
                PdfFile.Write(FileData);
            }

            // output stream
            PdfFile.WriteString("\nendstream\n");

            // output object trailer
            PdfFile.WriteString("endobj\n");

            // file spec object type
            Dictionary.Add("/Type", "/Filespec");

            // PDF file name
            if (String.IsNullOrWhiteSpace(PdfFileName))
            {
                PdfFileName = FI.Name;
            }
            Dictionary.AddPdfString("/F", PdfFileName);
            Dictionary.AddPdfString("/UF", PdfFileName);

            // add reference
            Dictionary.AddFormat("/EF", "<</F {0} 0 R /UF {0} 0 R>>", EmbeddedFile.ObjectNumber);
            return;
        }
Beispiel #3
0
        ////////////////////////////////////////////////////////////////////
        // Write object to PDF file
        ////////////////////////////////////////////////////////////////////

        internal override void WriteObjectToPdfFile()
        {
            // shortcut
            PdfBinaryWriter PdfFile = Document.PdfFile;

            // add items to dictionary
            Dictionary.Add("/Subtype", "/Image");
            Dictionary.AddInteger("/Width", WidthPix);
            Dictionary.AddInteger("/Height", HeightPix);
            Dictionary.Add("/Filter", "/DCTDecode");
            Dictionary.Add("/ColorSpace", "/DeviceRGB");
            Dictionary.Add("/BitsPerComponent", "8");

            // write dictionary
            Dictionary.WriteToPdfFile();

            // output stream
            PdfFile.WriteString("stream\n");

            // save pdf file position
            Int64 streamStart = PdfFile.BaseStream.Position;

            // debug
            if (Document.Debug)
            {
                PdfFile.WriteString("*** IMAGE PLACE HOLDER ***");
            }

            // copy image file to output file
            else
            {
                // create memory stream
                MemoryStream MS = new MemoryStream();

                // image quality is not 100
                if (ImageQuality != 100)
                {
                    // build EncoderParameter object for image quality
                    EncoderParameters EncoderParameters = new EncoderParameters(1);
                    EncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ImageQuality);

                    // save in jpeg format with specified quality
                    Picture.Save(MS, GetEncoderInfo("image/jpeg"), EncoderParameters);
                }

                else
                {
                    // save in jpeg format with 100% quality
                    Picture.Save(MS, ImageFormat.Jpeg);
                }

                // image byte array
                Byte[] ByteContents = MS.GetBuffer();

                // encryption
                if (Document.Encryption != null)
                {
                    ByteContents = Document.Encryption.EncryptByteArray(ObjectNumber, ByteContents);
                }

                // write memory stream internal buffer to PDF file
                PdfFile.Write(ByteContents);

                // close and dispose memory stream
                MS.Close();
                MS = null;

                // dispose of the image
                Dispose();
            }

            // save stream length
            ImageLengthObject.ContentsString.Append(((Int32)(PdfFile.BaseStream.Position - streamStart)).ToString());

            // output stream
            PdfFile.WriteString("\nendstream\nendobj\n");
            return;
        }
Beispiel #4
0
        ////////////////////////////////////////////////////////////////////
        // Write object to PDF file
        // Called by PdfDocument.CreateFile(FileName) method
        // to output one indirect PDF object.
        // It is a virtual method. Derived classes can overwrite it.
        ////////////////////////////////////////////////////////////////////

        internal virtual void WriteObjectToPdfFile()
        {
            // shortcut
            PdfBinaryWriter PdfFile = Document.PdfFile;

            // we have contents stream
            if (ContentsString != null && ContentsString.Length > 0)
            {
                // contents in bytes
                Byte[] ByteContents = new Byte[ContentsString.Length];

                // convert content from string to binary
                // do not use Encoding.ASCII.GetBytes(...)
                for (Int32 Index = 0; Index < ContentsString.Length; Index++)
                {
                    ByteContents[Index] = (Byte)ContentsString[Index];
                }

                // if we have contents and dictionary we have a stream
                if (Dictionary != null)
                {
                    // for testing compression is disabled
                    if (!Document.Debug)
                    {
                        // compress stream
                        Byte[] CompContents = CompressStream(ByteContents);
                        if (CompContents != null)
                        {
                            ByteContents = CompContents;
                            Dictionary.Add("/Filter", "/FlateDecode");
                        }
                    }

                    // encryption
                    if (Document.Encryption != null)
                    {
                        ByteContents = Document.Encryption.EncryptByteArray(ObjectNumber, ByteContents);
                    }

                    // stream length
                    Dictionary.AddInteger("/Length", ByteContents != null ? ByteContents.Length : 0);

                    // write dictionary
                    Dictionary.WriteToPdfFile();

                    // write stream reserved word
                    PdfFile.WriteString("stream\n");

                    // write content to pdf file
                    if (ByteContents != null)
                    {
                        PdfFile.Write(ByteContents);
                    }

                    // write end of stream
                    PdfFile.WriteString("\nendstream\n");
                }

                // we have contents but no dictionary
                else
                {
                    // write content to pdf file
                    PdfFile.Write(ByteContents);

                    // final terminator
                    PdfFile.Write('\n');
                }
            }

            // no contents
            else
            {
                // we have a dictionary
                if (Dictionary != null)
                {
                    // write dictionary
                    Dictionary.WriteToPdfFile();
                }

                // no contents and no dictionary
                else
                {
                    throw new ApplicationException("Empty object");
                }
            }

            // output object trailer
            PdfFile.WriteString("endobj\n");
            return;
        }