public void TryGetValue_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);

            PDFName key2 = new PDFName("Item2");

            target.Add(key2, value);

            bool expected = true;
            bool actual;

            IFileObject valueExpected = value;

            actual = target.TryGetValue(key, out value);

            Assert.AreEqual(valueExpected, value);
            Assert.AreEqual(expected, actual);

            expected = false;
            key      = new PDFName("NotFound");
            actual   = target.TryGetValue(key, out value);

            Assert.AreEqual(expected, actual);
            Assert.IsNull(value);
        }
        public void Remove_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);

            PDFName key2 = new PDFName("Item2");

            target.Add(key2, value);
            Assert.AreEqual(2, target.Count);

            bool expected = true;
            bool actual;

            actual = target.Remove(key);

            Assert.AreEqual(expected, actual);
            Assert.AreEqual(1, target.Count);

            expected = false;
            key      = new PDFName("NotFound");
            actual   = target.Remove(key);

            Assert.AreEqual(expected, actual);
            Assert.AreEqual(1, target.Count);
        }
        public void Add_Test1()
        {
            ICollection <KeyValuePair <PDFName, IFileObject> > target = new PDFDictionary();
            PDFName     key   = new PDFName("Item1");
            IFileObject value = new PDFNumber(1);
            KeyValuePair <PDFName, IFileObject> item = new KeyValuePair <PDFName, IFileObject>(key, value); // TODO: Initialize to an appropriate value

            target.Add(item);
            Assert.AreEqual(1, target.Count);

            PDFName key2 = new PDFName("Item2");

            item = new KeyValuePair <PDFName, IFileObject>(key2, value);
            target.Add(item);
            Assert.AreEqual(2, target.Count);
        }
        public void Clear_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);

            PDFName key2 = new PDFName("Item2");

            target.Add(key2, value);
            Assert.AreEqual(2, target.Count);

            target.Clear();
            Assert.AreEqual(0, target.Count);
        }
        public void Item_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);

            PDFName     key2   = new PDFName("Item2");
            IFileObject value2 = new PDFNumber(2);

            target.Add(key2, value2);


            IFileObject actual;

            actual = target[key];

            Assert.AreEqual(value, actual);

            actual = target[key2];
            Assert.AreEqual(value2, actual);

            target[key2] = value;
            actual       = target[key2];
            Assert.AreEqual(value, actual);

            PDFName newkey = new PDFName("NewKey");

            try
            {
                actual = target[newkey];
                Assert.Fail("Did not raise an exception for an invalid key");
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
                this.TestContext.WriteLine("Successfully caught the argument exception");
            }

            PDFNumber newValue = new PDFNumber(5);

            target[newkey] = newValue;
            actual         = target[newkey];

            Assert.AreEqual(newValue, actual);
        }
        public void Count_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);

            PDFName key2 = new PDFName("Item2");

            target.Add(key2, value);

            int expected = 2;
            int actual;

            actual = target.Count;
            Assert.AreEqual(expected, actual, "Count returned the  wrong value");
        }
        public void Contains_Test()
        {
            ICollection <KeyValuePair <PDFName, IFileObject> > target = new PDFDictionary();
            PDFName     key   = new PDFName("Item1");
            IFileObject value = new PDFNumber(1);
            KeyValuePair <PDFName, IFileObject> item = new KeyValuePair <PDFName, IFileObject>(key, value);

            target.Add(item);

            PDFName key2 = new PDFName("Item2");

            item = new KeyValuePair <PDFName, IFileObject>(key2, value);
            target.Add(item);

            bool expected = true;
            bool actual   = target.Contains(item);

            Assert.AreEqual(expected, actual);
        }
        public void Keys_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);

            PDFName     key2   = new PDFName("Item2");
            IFileObject value2 = new PDFNumber(2);

            target.Add(key2, value2);

            ICollection <PDFName> actual;

            actual = target.Keys;

            Assert.IsNotNull(actual);
            Assert.AreEqual(2, actual.Count);

            bool hasone = false;
            bool hastwo = false;

            foreach (PDFName item in actual)
            {
                if (item.Equals(key))
                {
                    hasone = true;
                }
                else if (item.Equals(key2))
                {
                    hastwo = true;
                }
                else
                {
                    Assert.Fail("Unknown key returned");
                }
            }

            Assert.IsTrue(hasone);
            Assert.IsTrue(hastwo);
        }
        public void Values_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);

            PDFName     key2   = new PDFName("Item2");
            IFileObject value2 = new PDFNumber(2);

            target.Add(key2, value2);

            ICollection <IFileObject> actual;

            actual = target.Values;

            bool hasone = false;
            bool hastwo = false;

            foreach (IFileObject item in actual)
            {
                if (item.Equals(value))
                {
                    hasone = true;
                }
                else if (item.Equals(value2))
                {
                    hastwo = true;
                }
                else
                {
                    Assert.Fail("Unknown value returned");
                }
            }

            Assert.IsTrue(hasone);
            Assert.IsTrue(hastwo);
        }
        public void GetEnumerator_Test1()
        {
            ICollection <KeyValuePair <PDFName, IFileObject> > target = new PDFDictionary();
            PDFName     key   = new PDFName("Item1");
            IFileObject value = new PDFNumber(1);
            KeyValuePair <PDFName, IFileObject> item = new KeyValuePair <PDFName, IFileObject>(key, value);

            target.Add(item);

            PDFName key2 = new PDFName("Item2");

            item = new KeyValuePair <PDFName, IFileObject>(key2, value);
            target.Add(item);

            int index = 0;

            foreach (KeyValuePair <PDFName, IFileObject> pair in target)
            {
                index++;
            }

            Assert.AreEqual(2, index);
        }
        public void GetEnumerator_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);

            PDFName key2 = new PDFName("Item2");

            target.Add(key2, value);
            Assert.AreEqual(2, target.Count);


            int expected = 2;
            int actual   = 0;

            foreach (KeyValuePair <PDFName, IFileObject> pair in target)
            {
                actual++;
            }
            Assert.AreEqual(expected, actual);
        }
        public void Add_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);
            Assert.AreEqual(1, target.Count);

            PDFName key2 = new PDFName("Item2");

            target.Add(key2, value);
            Assert.AreEqual(2, target.Count);

            try
            {
                target.Add(key, value);
                Assert.Fail("Did not raise argument exception for duplicate key");
            }
            catch (ArgumentException)
            {
                TestContext.WriteLine("Successfully caught a duplicate key exception");
            }
        }
        public void ContainsKey_Test()
        {
            PDFDictionary target = new PDFDictionary();
            PDFName       key    = new PDFName("Item1");
            IFileObject   value  = new PDFNumber(1);

            target.Add(key, value);

            PDFName key2 = new PDFName("Item2");

            target.Add(key2, value);
            Assert.AreEqual(2, target.Count);

            bool expected = true;
            bool actual;

            actual = target.ContainsKey(key);
            Assert.AreEqual(expected, actual);

            key      = new PDFName("NotIncluded");
            expected = false;
            actual   = target.ContainsKey(key);
            Assert.AreEqual(expected, actual);
        }
예제 #14
0
        static void Main(string[] args)
        {
            PDFSize paper = new PDFSize(842, 595); //A4 paper size 210mm×297mm
            // Create a document object and initialize it
            PDFDocument doc = new PDFDocument();

            doc.VersionMinor = 4;

            // Create a catalog
            Catalog cat = new Catalog();
            // Create an indirect obejct pointing to the catalog
            // Look how we get the id number from the document
            IndirectObject ioCat = new IndirectObject(cat, doc.NextObjectID());

            doc.IndirectObjects.Add(ioCat);
            doc.Trailer.Root = new Reference(ioCat);
            // Create pages object
            Pages          pages   = new Pages();
            IndirectObject ioPages = new IndirectObject(pages, doc.NextObjectID());

            doc.IndirectObjects.Add(ioPages);
            Reference refPages = new Reference(ioPages);

            // Create a reference to the Pages object and add it to teh catalog object
            cat.Pages = refPages;
            Page page = new Page();

            // define the dimention of the pages
            page.MediaBox = new PDFArray()
            {
                new Number(0), new Number(0), new Number(paper.Width), new Number(paper.Height)
            };
            // Set the page objects parent
            page.Parent = refPages;
            // Create indirect object for pages
            IndirectObject ioPage = new IndirectObject(page, doc.NextObjectID());

            doc.IndirectObjects.Add(ioPage);
            pages.AddPageRef(new Reference(ioPage));
            // Create resources object for each pages
            page.Resources = new Resources();


            PDFFontDescriptor fontDescriptor = new PDFFontDescriptor();

            fontDescriptor.FontBBox = new PDFArray()
            {
                new Number(0), new Number(0), new Number(0), new Number(0)
            };
            IndirectObject _fontDescriptorID = new IndirectObject(fontDescriptor, doc.NextObjectID());

            doc.IndirectObjects.Add(_fontDescriptorID);
            Reference fontDescriptorRef = new Reference(_fontDescriptorID);

            PDFFont        descendantFont   = new PDFFont();
            IndirectObject descendantFontID = new IndirectObject(descendantFont, doc.NextObjectID());

            doc.IndirectObjects.Add(descendantFontID);
            descendantFont.SubType  = new Name("CIDFontType2");
            descendantFont.BaseFont = new Name("SimSun");

            descendantFont.FontDescriptor = fontDescriptorRef;
            descendantFont.CIDSystemInfo  = new PDFDictionary();
            descendantFont.CIDSystemInfo.Add(new Name("Registry"), "(Adobe)");
            descendantFont.CIDSystemInfo.Add(new Name("Ordering"), "(GB1)");

            // Create font andf initialize it
            PDFFont ascii = new PDFFont();

            ascii.Name     = new Name("F2");
            ascii.Encoding = new Name("WinAnsiEncoding");
            ascii.BaseFont = new Name("Helvetica");
            ascii.SubType  = new Name("TrueType");
            IndirectObject arialID = new IndirectObject(ascii, doc.NextObjectID());

            doc.IndirectObjects.Add(arialID);


            // Create font and initialize it
            PDFFont gb = new PDFFont();

            gb.SubType         = new Name("Type0");
            gb.Name            = new Name("F1");
            gb.BaseFont        = new Name("SimSun");
            gb.Encoding        = new Name("UniGB-UCS2-H");
            gb.FontDescriptor  = fontDescriptorRef;
            gb.DescendantFonts = new PDFArray()
            {
                new Reference(descendantFontID)
            };
            IndirectObject fontID = new IndirectObject(gb, doc.NextObjectID());

            doc.IndirectObjects.Add(fontID);


            // add the font dictionary to the resources
            PDFDictionary dicFont = new PDFDictionary();

            dicFont.CarriageReturn = string.Empty;
            dicFont.Add(ascii.Name, new Reference(arialID));
            dicFont.Add(gb.Name, new Reference(fontID));
            page.Resources.Add(new Name("Font"), dicFont);
            // Create the procedure set
            ProcSet procset = new ProcSet();

            procset.Add(new Name("PDF"));
            procset.Add(new Name("Text"));
            IndirectObject ioProcset = new IndirectObject(procset, doc.NextObjectID());

            doc.IndirectObjects.Add(ioProcset);
            // add the proc set to the resources
            page.Resources.Add(new Name("ProcSet"), new Reference(ioProcset));

            PDFStream      content  = new PDFStream();
            IndirectObject ioStream = new IndirectObject(content, doc.NextObjectID());

            doc.IndirectObjects.Add(ioStream);
            page.Content = new Reference(ioStream);
            Info           info   = new Info();
            IndirectObject ioInfo = new IndirectObject(info, doc.NextObjectID());

            doc.IndirectObjects.Add(ioInfo);
            doc.Trailer.Info = new Reference(ioInfo);

            //draw
            double      unit = 2.834;
            PDFGraphics g    = new PDFGraphics();

            g.state(1, 0, 0, 1, 0, 0);
            g.SetLineWidth(1f);
            g.SetRGBFillColor(Color.SteelBlue);
            g.SetRGBStrokeColor(Color.Black);
            g.DrawRectangle(20 * unit, paper.Height - 20 * unit, 10 * unit, 10 * unit);
            g.FillAndStrokePath();
            content.Write(g);

            //Set font and write
            // create the text object to write to
            TextObject text = new TextObject();

            text.SetFont(gb, 8);
            text.SetRGBFillColor(Color.Blue);
            text.SetRGBStrokeColor(Color.Blue);
            text.Move(10 * unit, paper.Height - 5 * unit);
            text.SetRGBFillColor(Color.Black);
            text.SetRGBStrokeColor(Color.Black);
            text.SetFont(gb, 9);
            Func <string, string> str2code = (string s) =>
            {
                StringBuilder o = new StringBuilder("<");
                foreach (var c in s)
                {
                    o.Append(Convert.ToString(c, 16).ToUpper().PadLeft(4, '0'));
                }
                o.Append('>');
                return(o.ToString());
            };

            text.WriteBinary(str2code("绘制一个大小边长1厘米的正方形"));
            content.Write(text);

            doc.Save("demo.pdf");
        }