コード例 #1
0
ファイル: PdfEmbeddedFile.cs プロジェクト: AxlOnGit/Catalist
        /// <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;
        }
コード例 #2
0
        private PdfEmbeddedFile
        (
            PdfDocument Document,
            string FileName,
            string PdfFileName
        ) : 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
            var FI = new FileInfo(FileName);

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

            var FileLength = (int)FI.Length;

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

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

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

            // file data content byte array
            EmbeddedFile.ObjectValueArray = 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(EmbeddedFile.ObjectValueArray, 0, FileLength) != FileLength)
                {
                    throw new Exception();
                }
            }

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

            // close the file
            DataStream.Close();

            // debug
            if (Document.Debug)
            {
                EmbeddedFile.ObjectValueArray = Document.TextToByteArray("*** MEDIA FILE PLACE HOLDER ***");
            }

            // write stream
            EmbeddedFile.WriteObjectToPdfFile();

            // 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);
        }