Example #1
0
        public void Dispose_Test()
        {
            IStreamFilter[]   filters = _usefilters;
            PDFIndirectObject indobj  = new PDFIndirectObject(this);
            StreamProxy       proxy   = new StreamProxy(filters, indobj);
            PDFStream         target  = proxy;

            target.Dispose();
            //As the target owns the inner stream it should be disposed.
            Assert.IsNull(proxy.InnerStreamProxy());

            MemoryStream ms         = new MemoryStream();
            bool         ownsStream = false;

            proxy  = new StreamProxy(ms, filters, indobj, ownsStream);
            target = proxy;

            target.Dispose();
            Assert.IsNull(proxy.InnerStreamProxy()); //reference should have gone from the stream

            ms.WriteByte(12);                        //this will fail if the stream has been disposed.

            ms.Dispose();
            indobj.Dispose();
        }
Example #2
0
        public void WriteStream(PDFStream val)
        {
            this.WriteLn(val.Dict);
            this.WriteRaw("stream");
            this.WriteEOL();
            this.WriteRaw("\n");
            if (val.IsExternal)
            {
                goto Label_0099;
            }
            byte[] numArray1 = new byte[4096];
            Stream stream1   = val.Decrypt();

            if (this.Encryption != null)
            {
                stream1 = this.Encryption.EncryptStream(stream1, val.Indirect.Id, val.Indirect.Generation);
            }
            int num1 = 0;

            this.mWriter.Flush();
            goto Label_008A;
Label_007C:
            this.mStream.Write(numArray1, 0, num1);
Label_008A:
            num1 = stream1.Read(numArray1, 0, numArray1.Length);
            if (num1 != 0)
            {
                goto Label_007C;
            }
Label_0099:
            this.WriteEOL();
            this.WriteRaw("endstream");
        }
Example #3
0
        public static MediaClipData Create(PDFStream str, Rect rect)
        {
            MediaClipData data1 = MediaClipData.Create(true);

            data1.MediaData = XObjectForm.Create(str, rect);
            return(data1);
        }
Example #4
0
        public void InsertStream(PDFStream s, int nr)
        {
            PDFArray  array1;
            PDFObject obj2;
            PDFObject obj1 = this.mPageDict["Contents"];

            if (obj1 == null)
            {
                obj2 = Library.CreateArray(0);
                this.mPageDict["Contents"] = obj2;
                obj1 = obj2;
            }
            if (obj1.PDFType == PDFObjectType.tPDFStream)
            {
                array1 = Library.CreateArray(0);
                if (nr > 1)
                {
                    throw new IndexOutOfRangeException("Invalid contents stream index");
                }
                this.mPageDict["Contents"] = array1;
                array1.Add(((nr == 1) ? obj1 : s));
                array1.Add(((nr == 1) ? s : obj1));
                return;
            }
            if (obj1.PDFType == PDFObjectType.tPDFArray)
            {
                ((PDFArray)obj1).Insert(nr, s);
                return;
            }
            throw new PDFSyntaxException("Invalid CONTENTS entry");
        }
Example #5
0
        public byte[] GenerateReportForUserActivity(string physicalPath, string userName)
        {
            GenerateReportBase();
            l1.Add(HeaderLogo(physicalPath));
            l1.Add(SubjectBlock(new Paragraph("Report Name: User Activity with Dates and Times")));
            l1.Add(UserName(new Paragraph("User Name:  " + userName)));
            PdfPTable table = new PdfPTable(2);

            table.AddCell(CellHeader("User Name"));
            table.AddCell(CellHeader("Date Time"));
            AuthenticationsAndAuthorizationsOperations aNaOps  = new AuthenticationsAndAuthorizationsOperations();
            List <UserLoginAuditTrail> userActivityAuditTrails = aNaOps.GetUserActivityAuditTrailsBySpecificUser(aNaOps.GetUserIDByUserName(userName));
            int recordsCount = 0;

            foreach (UserLoginAuditTrail item in userActivityAuditTrails)
            {
                table.AddCell(CellData(item.UserName));
                table.AddCell(CellData(item.DateTimeLogged.ToString()));
                if (recordsCount >= pdfReportRecordCount)
                {
                    break;
                }
                recordsCount++;
            }
            l1.Add(table);
            FooterLines.Add("DateTime: " + DateTime.Now.ToString());
            l1.Close();
            DocumentBytes = PDFStream.GetBuffer();
            return(DocumentBytes);
        }
Example #6
0
        public static Sound Create(string filename, int sampling_rate)
        {
            PDFStream stream1 = Library.CreateStream(new FileStream(filename, FileMode.Open));

            stream1.Dict["R"] = Library.CreateFixed(((double)sampling_rate));
            return(new Sound(stream1));
        }
Example #7
0
        public PDFStream CreateStream(PDFStream.Filter filter)
        {
            PDFStream stream = new PDFStream(GetNextObjectNumber(), filter);

            indirectObjects.Add(stream);
            return(stream);
        }
Example #8
0
        public byte[] GenerateReportForDocumentsDownloadedBySpecificUser(string physicalPath, string userName)
        {
            GenerateReportBase();
            l1.Add(HeaderLogo(physicalPath));
            l1.Add(SubjectBlock(new Paragraph("Report Name: Documents Downloaded with Dates and Times")));
            l1.Add(UserName(new Paragraph("User Name:  " + userName)));
            PdfPTable table = new PdfPTable(2);

            table.AddCell(CellHeader("Document Name"));
            table.AddCell(CellHeader("Date Time"));
            List <FilesDownloadAuditTrail> filesDownloadedList = (new AuditTrailOperations()).GetFilesDownloadedAuditTrailsBySpecificUser(userName);
            int recordsCount = 0;

            foreach (FilesDownloadAuditTrail item in filesDownloadedList)
            {
                table.AddCell(CellData(item.FileName));
                table.AddCell(CellData(item.DateTimeDownloaded.ToString()));
                if (recordsCount >= pdfReportRecordCount)
                {
                    break;
                }
                recordsCount++;
            }
            l1.Add(table);
            FooterLines.Add("DateTime: " + DateTime.Now.ToString());
            l1.Close();
            DocumentBytes = PDFStream.GetBuffer();
            return(DocumentBytes);
        }
        protected override void ReleaseIndirectObject(IIndirectObject obj)
        {
            base.ReleaseIndirectObject(obj);
            if (obj is PDFIndirectObject)
            {
                if (this.TraceLog.ShouldLog(PoolTraceLevel))
                {
                    this.TraceLog.Add(PoolTraceLevel, PoolTraceCategory, "Writing object data for " + obj.ToString() + " and releasing back onto the pool");
                }

                PDFIndirectObject known = (PDFIndirectObject)obj;

                if (known.Written == false)
                {
                    this.WriteAnIndirectObject(known);
                }

                PDFStream data    = known.ObjectData;
                PDFStream content = known.HasStream ? known.Stream : null;

                if (data != null)
                {
                    this.ReleaseAndRePool(data);
                }

                if (content != null)
                {
                    this.ReleaseAndRePool(content);
                }

                known.ReleaseStreams(false);
            }
        }
Example #10
0
        static void Main(string[] args)
        {
            Console.WriteLine("Image Embed ICC Profile sample:");

            using (Library lib = new Library())
            {
                Console.WriteLine("Initialized the library.");

                String sInput      = "../../Resources/Sample_Input/ducky.pdf";
                String profileName = "../../Resources/Sample_Input/Probev1_ICCv2.icc";

                Console.WriteLine("Input file: " + sInput + " will have profile " + profileName + " applied.");

                if (args.Length > 0)
                {
                    sInput = args[0];
                }

                if (args.Length > 1)
                {
                    profileName = args[1];
                }

                Document doc = new Document(sInput);

                ExportDocumentImages expdoc = new ExportDocumentImages();

                FileStream profileFileStream = new FileStream(profileName, FileMode.Open);
                PDFStream  profilePDFStream  = new PDFStream(profileFileStream, doc, null, null);

                expdoc.export_doc_images(doc, profilePDFStream);
            }
        }
Example #11
0
        public PDFStream InsertStream(Stream s, int nr)
        {
            PDFStream stream1 = Library.CreateStream();

            this.InsertStream(stream1, nr);
            stream1.Encode(s);
            return(stream1);
        }
Example #12
0
        public static XObjectForm Create(PDFStream str, Rect bbox)
        {
            str.Dict["Subtype"] = Library.CreateName("Form");
            XObjectForm form1 = (Resources.Get(str, typeof(XObjectForm)) as XObjectForm);

            form1.BBox = bbox;
            return(form1);
        }
Example #13
0
        internal static Resource Factory(PDFDirect d)
        {
            if (d.PDFType != PDFObjectType.tPDFDict)
            {
                goto Label_0061;
            }
            PDFDict dict1 = ((PDFDict)d);
            int     num1  = ((PDFInteger)dict1["FunctionType"]).Int32Value;
            int     num3  = num1;

            switch (num3)
            {
            case 2:
            {
                goto Label_003C;
            }

            case 3:
            {
                goto Label_0043;
            }
            }
            goto Label_004A;
Label_003C:
            return(new FunctionType2(d));

Label_0043:
            return(new FunctionType3(d));

Label_004A:
            throw new PDFSyntaxException(d, string.Format("Unknown function type: {0}", num1));
Label_0061:
            if (d.PDFType != PDFObjectType.tPDFStream)
            {
                goto Label_00C0;
            }
            PDFStream stream1 = ((PDFStream)d);
            int       num2    = ((PDFInteger)stream1.Dict["FunctionType"]).Int32Value;

            num3 = num2;
            if (num3 != 0)
            {
                if (num3 == 4)
                {
                    goto Label_00A2;
                }
                goto Label_00A9;
            }
            return(new FunctionType0(d));

Label_00A2:
            return(new FunctionType4(d));

Label_00A9:
            throw new PDFSyntaxException(d, string.Format("Unknown function type: {0}", num2));
Label_00C0:
            throw new PDFSyntaxException(d, "Invalid Function");
        }
Example #14
0
        private PDFStream CreateContentStream(PDFStream.Filter filter)
        {
            PDFStream stream = CreateStream(filter);

            // Each page needs this translate
            stream.WriteData("1 0 0 1 0 841.8898 cm\r\n");

            return(stream);
        }
Example #15
0
        public static XObjectPS Create(Stream ps)
        {
            PDFDict dict1 = Library.CreateDict();

            dict1["Type"]    = PDF.N("XObject");
            dict1["Subtype"] = PDF.N("PS");
            PDFStream stream1 = Library.CreateStream(ps, dict1);

            return(Resources.Get(stream1, typeof(XObjectPS)) as XObjectPS);
        }
Example #16
0
        public PDFStream AppendStream(Stream s_before, Stream s_after, int nr)
        {
            Stream           stream2;
            InputStreamArray array1;

            Stream[] streamArray1;
            if (this.Count == 0)
            {
                this.AddStream(Stream.Null);
            }
            PDFStream stream1 = this.GetPDFStream(nr);

            if (stream1 == null)
            {
                return(null);
            }
            if ((s_before == null) && (s_after == null))
            {
                return(stream1);
            }
            if (stream1.Dict["Filter"] != null)
            {
                stream2 = stream1.Decode();
                stream1.Dict.Remove("Filter");
                stream1.Dict.Remove("DecodeParms");
                stream1.Encode(stream2);
            }
            if (s_before != null)
            {
                if (s_after != null)
                {
                    streamArray1    = new Stream[3];
                    streamArray1[0] = s_before;
                    streamArray1[1] = this.GetPDFStream(nr).Decode();
                    streamArray1[2] = s_after;
                    array1          = new InputStreamArray(streamArray1);
                }
                else
                {
                    streamArray1    = new Stream[2];
                    streamArray1[0] = s_before;
                    streamArray1[1] = this.GetPDFStream(nr).Decode();
                    array1          = new InputStreamArray(streamArray1);
                }
            }
            else
            {
                streamArray1    = new Stream[2];
                streamArray1[0] = this.GetPDFStream(nr).Decode();
                streamArray1[1] = s_after;
                array1          = new InputStreamArray(streamArray1);
            }
            this.GetPDFStream(nr).Encode(array1);
            return(this.GetPDFStream(nr));
        }
Example #17
0
        public static EmbeddedFileStream Create(bool indirect, Stream _str)
        {
            PDFStream          stream1 = Library.CreateStream(_str);
            EmbeddedFileStream stream2 = (Resources.Get(stream1, typeof(EmbeddedFileStream)) as EmbeddedFileStream);

            if (indirect)
            {
                Library.CreateIndirect(stream1);
            }
            return(stream2);
        }
Example #18
0
        public static FunctionType4 Create(double[] domain, double[] range, string expression)
        {
            byte[]    numArray1 = Encoding.ASCII.GetBytes(expression);
            PDFStream stream1   = Library.CreateStream(numArray1, 0, numArray1.Length, false, null);
            PDFDict   dict1     = stream1.Dict;

            dict1["FunctionType"] = Library.CreateInteger(((long)4));
            dict1["Domain"]       = Library.CreateArray(false, domain);
            dict1["Range"]        = Library.CreateArray(false, range);
            return(new FunctionType4(stream1));
        }
Example #19
0
        // Methods
        internal PDFIndexedCSDoubleArrayStream(PDFStream str, int pos, int ncomponet, byte[] data, int start) : base(ncomponet)
        {
            int num1;

            this.Position    = pos;
            this.mNComponent = ncomponet;
            this.mStr        = str;
            for (num1 = 0; (num1 < ncomponet); num1 += 1)
            {
                this.mArr[num1] = ((double)data[(start + num1)]);
            }
        }
Example #20
0
        public void export_doc_images_type(Document doc, PDFStream profileStream, ImageType imtype)
        {
            exporttype = imtype;
            ColorSpace cs = new ICCBasedColorSpace(profileStream);

            for (int pgno = 0; pgno < doc.NumPages; pgno++)
            {
                Page pg = doc.GetPage(pgno);

                Export_Image(pg.Content, cs, pg, pgno);
            }
        }
Example #21
0
        public static ColorSpaceICCBased Create(Document doc, bool indirect, Stream profile, params string[] encoding)
        {
            PDFArray array2;
            int      num2;
            PDFArray array1 = Library.CreateArray(2);

            array1[0] = PDF.N("ICCBased");
            PDFStream stream1 = Library.CreateStream();

            byte[] numArray1 = new byte[30];
            profile.Position = ((long)0);
            int num1 = profile.Read(numArray1, 0, 30);

            if (num1 == 0)
            {
                throw new Exception("Empty ICC profile");
            }
            string text1 = Utils.BytesToString(numArray1);

            if (text1.IndexOf("GRAY") != -1)
            {
                stream1.Dict["N"] = Library.CreateInteger(((long)1));
            }
            if (text1.IndexOf("RGB") != -1)
            {
                stream1.Dict["N"] = Library.CreateInteger(((long)3));
            }
            if (text1.IndexOf("CMYK") != -1)
            {
                stream1.Dict["N"] = Library.CreateInteger(((long)4));
            }
            if ((encoding != null) && (encoding.Length > 0))
            {
                array2 = Library.CreateArray(encoding.Length);
                stream1.Dict["Filter"] = array2;
                for (num2 = 0; (num2 < encoding.Length); num2 += 1)
                {
                    array2[num2] = PDF.N(encoding[num2]);
                }
            }
            stream1.Encode(profile);
            array1[1] = doc.Indirects.New(stream1);
            ColorSpaceICCBased based1 = (Resources.Get(array1, typeof(ColorSpaceICCBased)) as ColorSpaceICCBased);

            if (indirect)
            {
                doc.Indirects.New(array1);
            }
            return(based1);
        }
Example #22
0
        /// <summary>
        /// Ends the current object data stream and pops it from the stack.
        /// </summary>
        /// <returns>The (optionally filtered) length of the stream</returns>
        public override long EndStream()
        {
            PDFStream str = this.Stack.Pop();

            str.Flush();
            if (str.HasFilters)
            {
                return(str.FilteredLength);
            }
            else
            {
                return(str.Length);
            }
        }
Example #23
0
        public PDFStream AppendStream(string s, int nr)
        {
            Stream    stream2;
            PDFStream stream1 = this.GetPDFStream(nr);

            if (stream1.Dict["Filter"] != null)
            {
                stream2 = stream1.Decode();
                stream1.Dict.Remove("Filter");
                stream1.Dict.Remove("DecodeParms");
                stream1.Encode(stream2);
            }
            return(this.AppendStream(new MemoryStream(Utils.StringToBytes(s)), nr));
        }
Example #24
0
        static void Main(string[] args)
        {
            if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform
                                                                               .OSX) && !File.Exists("/usr/local/lib/libgdiplus.dylib"))
            {
                Console.WriteLine("Please install libgdiplus first to access the System.Drawing namespace on macOS.");
                return;
            }

            if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform
                                                                               .Linux) && !System.IO.File.Exists("/usr/lib64/libgdiplus.so") &&
                !System.IO.File.Exists("/usr/lib/libgdiplus.so"))
            {
                Console.WriteLine("Please install libgdiplus first to access the System.Drawing namespace on Linux.");
                return;
            }

            Console.WriteLine("Image Embed ICC Profile sample:");

            // ReSharper disable once UnusedVariable
            using (Library lib = new Library())
            {
                Console.WriteLine("Initialized the library.");

                String sInput      = Library.ResourceDirectory + "Sample_Input/ducky.pdf";
                String profileName = Library.ResourceDirectory + "Sample_Input/Probev1_ICCv2.icc";

                Console.WriteLine("Input file: " + sInput + " will have profile " + profileName + " applied.");

                if (args.Length > 0)
                {
                    sInput = args[0];
                }

                if (args.Length > 1)
                {
                    profileName = args[1];
                }

                Document doc = new Document(sInput);

                ExportDocumentImages expdoc = new ExportDocumentImages();

                FileStream profileFileStream = new FileStream(profileName, FileMode.Open);
                PDFStream  profilePDFStream  = new PDFStream(profileFileStream, doc, null, null);

                expdoc.export_doc_images(doc, profilePDFStream);
            }
        }
Example #25
0
        /// <summary>
        /// Adds a new page to the PDF and creates the needed PDF objects.
        /// </summary>
        public void AddPage()
        {
            currentContentStream = CreateContentStream(PDFStream.Filter.None);
            pageContentStreams.Add(currentContentStream);

            currentPageDictionary = CreateIndirectDictionary();
            currentPageDictionary.Put("Type", new PDFName("Page"));
            currentPageDictionary.Put("Parent", pagesDictionary);
            currentPageDictionary.Put("Contents", currentContentStream);
            currentPageDictionary.Put("MediaBox", mediaBox);
            currentPageDictionary.Put("Resources", _commonResources);

            pageDictionaries.Add(currentPageDictionary);
            pageKidsArray.Array.Add(currentPageDictionary);
        }
Example #26
0
        static void Main(string[] args)
        {
            using (Library lib = new Library())
            {
                String sInput  = Library.ResourceDirectory + "Sample_Input/sRGB_IEC61966-2-1_noBPC.icc";
                String sOutput = "../ICCBased-out.pdf";

                if (args.Length > 0)
                {
                    sInput = args[0];
                }
                if (args.Length > 1)
                {
                    sOutput = args[1];
                }

                Console.WriteLine("Writing to output " + sOutput);

                Document doc     = new Document();
                Page     page    = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72));
                Content  content = page.Content;
                Font     font    = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset);

                FileStream stream    = new FileStream(sInput, FileMode.Open);
                PDFStream  pdfStream = new PDFStream(stream, doc, null, null);

                ColorSpace   cs = new ICCBasedColorSpace(pdfStream, 3);
                GraphicState gs = new GraphicState();
                gs.FillColor = new Color(cs, new Double[] { 1.0, 0.0, 0.0 });


                Matrix textMatrix = new Matrix(24, 0, 0, 24,    // Set font width and height to 24 point size
                                               1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2"

                TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix);
                Text    text    = new Text();
                text.AddRun(textRun);
                content.AddElement(text);
                page.UpdateContent();

                doc.EmbedFonts();
                doc.Save(SaveFlags.Full, sOutput);
            }
        }
Example #27
0
        public static XObjectForm Create(string StrBody, Rect BBox)
        {
            int num1;

            byte[] numArray1 = new byte[StrBody.Length];
            for (num1 = 0; (num1 < StrBody.Length); num1 += 1)
            {
                numArray1[num1] = ((byte)StrBody[num1]);
            }
            MemoryStream stream1 = new MemoryStream(numArray1);
            PDFStream    stream2 = Library.CreateStream(stream1);

            stream2.Dict["Subtype"] = Library.CreateName("Form");
            XObjectForm form1 = new XObjectForm(stream2);

            form1.BBox = BBox;
            form1.CTM  = new CTM(1f, 0f, 0f, 1f, 0f, 0f);
            return(form1);
        }
Example #28
0
        public PDFStream AppendStream(PDFStream s, int nr)
        {
            Stream stream2;

            if (this.Count == 0)
            {
                this.AddStream(Stream.Null);
            }
            PDFStream stream1 = this.GetPDFStream(nr);

            if (stream1.Dict["Filter"] != null)
            {
                stream2 = stream1.Decode();
                stream1.Dict.Remove("Filter");
                stream1.Dict.Remove("DecodeParms");
                stream1.Encode(stream2);
            }
            return(this.AppendStream(s.Decode(), nr));
        }
Example #29
0
        public static XObjectForm Create()
        {
            int    num1;
            string text1 = "/DeviceRGB cs 0.1 1.0 1.0 sc 0.0 4.00 40.00 20.00 re B";

            byte[] numArray1 = new byte[text1.Length];
            for (num1 = 0; (num1 < text1.Length); num1 += 1)
            {
                numArray1[num1] = ((byte)text1[num1]);
            }
            MemoryStream stream1 = new MemoryStream(numArray1);
            PDFStream    stream2 = Library.CreateStream(stream1);

            stream2.Dict["Subtype"] = Library.CreateName("Form");
            XObjectForm form1 = new XObjectForm(stream2);

            form1.BBox = new Rect(0f, 0f, 50f, 50f);
            form1.CTM  = new CTM(1f, 0f, 0f, 1f, 0f, 0f);
            return(form1);
        }
        public override PDFStream CreateStream(IStreamFilter[] filters, IIndirectObject forobject)
        {
            if (this._available.Count > 0 && forobject is PDFIndirectObject)
            {
                if (this.TraceLog.ShouldLog(PoolTraceLevel))
                {
                    this.TraceLog.Add(PoolTraceLevel, PoolTraceCategory, "Dequeued a previously allocated PDFStream");
                }

                int       index = this._available.Count - 1;
                PDFStream last  = this._available[index];
                last.Filters        = filters;
                last.IndirectObject = (PDFIndirectObject)forobject;
                this._available.RemoveAt(index);

                return(last);
            }
            else
            {
                return(base.CreateStream(filters, forobject));
            }
        }