Esempio n. 1
0
        public virtual void TokenValueEqualsToTest()
        {
            String data = "SomeString";
            RandomAccessSourceFactory factory = new RandomAccessSourceFactory();
            PdfTokenizer tok = new PdfTokenizer(new RandomAccessFileOrArray(factory.CreateSource(data.GetBytes())));

            tok.NextToken();
            NUnit.Framework.Assert.IsTrue(tok.TokenValueEqualsTo(data.GetBytes()));
        }
Esempio n. 2
0
        public virtual void EncodingTest()
        {
            RandomAccessSourceFactory factory;
            PdfTokenizer tok;
            PdfString    pdfString;
            // hex string parse and check
            String testHexString = "<0D0A09557365729073204775696465>";

            factory = new RandomAccessSourceFactory();
            tok     = new PdfTokenizer(new RandomAccessFileOrArray(factory.CreateSource(testHexString.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1
                                                                                                               ))));
            tok.NextToken();
            pdfString = new PdfString(tok.GetByteContent(), tok.IsHexString());
            NUnit.Framework.Assert.AreEqual("\r\n\tUser\u0090s Guide", pdfString.GetValue());
            String testUnicodeString = "ΑΒΓΗ€•♣⋅";

            pdfString = new PdfString(PdfEncodings.ConvertToBytes(testUnicodeString, PdfEncodings.UNICODE_BIG), false);
            NUnit.Framework.Assert.AreEqual(testUnicodeString, pdfString.ToUnicodeString());
            pdfString = new PdfString("FEFF041F04400438043204350442".GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1),
                                      true);
            NUnit.Framework.Assert.AreEqual("\u041F\u0440\u0438\u0432\u0435\u0442", pdfString.ToUnicodeString());
            pdfString = new PdfString("FEFF041F04400438043204350442".GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1),
                                      false);
            NUnit.Framework.Assert.AreEqual("FEFF041F04400438043204350442", pdfString.ToUnicodeString());
            String specialCharacter = "\r\n\t\\n\\r\\t\\f";

            pdfString = new PdfString(specialCharacter.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("\n\t\n\r\t\f", pdfString.ToUnicodeString());
            String symbol = "\u0001\u0004\u0006\u000E\u001F";

            pdfString = new PdfString(symbol.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual(symbol, pdfString.ToUnicodeString());
            String testString1 = "These\\\n two\\\r strings\\\n are the same";

            pdfString = new PdfString(testString1.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("These two strings are the same", pdfString.GetValue());
            String testString2 = "This string contains \\245two octal characters\\307";

            pdfString = new PdfString(testString2.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("This string contains \u00A5two octal characters\u00C7", pdfString.GetValue
                                                ());
            String testString3 = "\\0053";

            pdfString = new PdfString(testString3.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("\u00053", pdfString.GetValue());
            String testString4 = "\\053";

            pdfString = new PdfString(testString4.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1), false);
            NUnit.Framework.Assert.AreEqual("+", pdfString.GetValue());
            byte[] b = new byte[] { (byte)46, (byte)56, (byte)40 };
            pdfString = new PdfString(b, false);
            NUnit.Framework.Assert.AreEqual(iText.IO.Util.JavaUtil.GetStringForBytes(b), pdfString.GetValue());
        }
Esempio n. 3
0
        /// <exception cref="System.Exception"/>
        private void CheckTokenTypes(String data, params PdfTokenizer.TokenType[] expectedTypes)
        {
            RandomAccessSourceFactory factory = new RandomAccessSourceFactory();
            PdfTokenizer tok = new PdfTokenizer(new RandomAccessFileOrArray(factory.CreateSource(data.GetBytes())));

            for (int i = 0; i < expectedTypes.Length; i++)
            {
                tok.NextValidToken();
                //System.out.println(tok.getTokenType() + " -> " + tok.getStringValue());
                NUnit.Framework.Assert.AreEqual(expectedTypes[i], tok.GetTokenType(), "Position " + i);
            }
        }
Esempio n. 4
0
 /// <exception cref="System.IO.IOException"/>
 public virtual RandomAccessFileOrArray GetMetricsFile()
 {
     isBuiltInFont = false;
     if (StandardFonts.IsStandardFont(afmPath))
     {
         isBuiltInFont = true;
         byte[] buf      = new byte[1024];
         Stream resource = null;
         try {
             String resourcePath = FontResources.AFMS + afmPath + ".afm";
             resource = ResourceUtil.GetResourceStream(resourcePath);
             if (resource == null)
             {
                 throw new iText.IO.IOException("{0} was not found as resource.").SetMessageParams(resourcePath);
             }
             MemoryStream stream = new MemoryStream();
             int          read;
             while ((read = resource.Read(buf)) >= 0)
             {
                 stream.Write(buf, 0, read);
             }
             buf = stream.ToArray();
         }
         finally {
             if (resource != null)
             {
                 try {
                     resource.Dispose();
                 }
                 catch (Exception) {
                 }
             }
         }
         return(new RandomAccessFileOrArray(sourceFactory.CreateSource(buf)));
     }
     else
     {
         if (afmPath != null)
         {
             if (afmPath.ToLowerInvariant().EndsWith(".afm"))
             {
                 return(new RandomAccessFileOrArray(sourceFactory.CreateBestSource(afmPath)));
             }
             else
             {
                 if (afmPath.ToLowerInvariant().EndsWith(".pfm"))
                 {
                     MemoryStream            ba = new MemoryStream();
                     RandomAccessFileOrArray rf = new RandomAccessFileOrArray(sourceFactory.CreateBestSource(afmPath));
                     Pfm2afm.Convert(rf, ba);
                     rf.Close();
                     return(new RandomAccessFileOrArray(sourceFactory.CreateSource(ba.ToArray())));
                 }
                 else
                 {
                     throw new iText.IO.IOException(iText.IO.IOException._1IsNotAnAfmOrPfmFontFile).SetMessageParams(afmPath);
                 }
             }
         }
         else
         {
             if (afmData != null)
             {
                 RandomAccessFileOrArray rf = new RandomAccessFileOrArray(sourceFactory.CreateSource(afmData));
                 if (IsAfmFile(rf))
                 {
                     return(rf);
                 }
                 else
                 {
                     MemoryStream ba = new MemoryStream();
                     try {
                         Pfm2afm.Convert(rf, ba);
                     }
                     catch (Exception) {
                         throw new iText.IO.IOException("Invalid afm or pfm font file.");
                     }
                     finally {
                         rf.Close();
                     }
                     return(new RandomAccessFileOrArray(sourceFactory.CreateSource(ba.ToArray())));
                 }
             }
             else
             {
                 throw new iText.IO.IOException("Invalid afm or pfm font file.");
             }
         }
     }
 }
Esempio n. 5
0
        public virtual void PrimitivesTest()
        {
            String data = "<</Size 70." + "/Value#20 .1" + "/Root 46 0 R" + "/Info 44 0 R" + "/ID[<736f6d652068657820737472696e672>(some simple string )<8C2547D58D4BD2C6F3D32B830BE3259D2>-70.1--0.2]"
                          + "/Name1 --15" + "/Prev ---116.23 >>";
            RandomAccessSourceFactory factory = new RandomAccessSourceFactory();
            PdfTokenizer tok = new PdfTokenizer(new RandomAccessFileOrArray(factory.CreateSource(data.GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1
                                                                                                               ))));

            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.StartDic);
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name);
            PdfName name = new PdfName(tok.GetByteContent());

            NUnit.Framework.Assert.AreEqual("Size", name.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number);
            PdfNumber num = new PdfNumber(tok.GetByteContent());

            NUnit.Framework.Assert.AreEqual("70.", num.ToString());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name);
            name = new PdfName(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("Value ", name.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number);
            num = new PdfNumber(tok.GetByteContent());
            NUnit.Framework.Assert.AreNotSame("0.1", num.ToString());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name);
            name = new PdfName(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("Root", name.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Ref);
            PdfIndirectReference @ref = new PdfIndirectReference(null, tok.GetObjNr(), tok.GetGenNr());

            NUnit.Framework.Assert.AreEqual("46 0 R", @ref.ToString());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name);
            name = new PdfName(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("Info", name.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Ref);
            @ref = new PdfIndirectReference(null, tok.GetObjNr(), tok.GetGenNr());
            NUnit.Framework.Assert.AreEqual("44 0 R", @ref.ToString());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name);
            name = new PdfName(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("ID", name.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.StartArray);
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.String);
            NUnit.Framework.Assert.AreEqual(tok.IsHexString(), true);
            PdfString str = new PdfString(tok.GetByteContent(), tok.IsHexString());

            NUnit.Framework.Assert.AreEqual("some hex string ", str.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.String);
            NUnit.Framework.Assert.AreEqual(tok.IsHexString(), false);
            str = new PdfString(tok.GetByteContent(), tok.IsHexString());
            NUnit.Framework.Assert.AreEqual("some simple string ", str.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.String);
            NUnit.Framework.Assert.AreEqual(tok.IsHexString(), true);
            str = new PdfString(tok.GetByteContent(), tok.IsHexString());
            NUnit.Framework.Assert.AreEqual("\u008C%G\u00D5\u008DK\u00D2\u00C6\u00F3\u00D3+\u0083\u000B\u00E3%\u009D "
                                            , str.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number);
            num = new PdfNumber(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("-70.1", num.ToString());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number);
            num = new PdfNumber(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("-0.2", num.ToString());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.EndArray);
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name);
            name = new PdfName(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("Name1", name.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number);
            num = new PdfNumber(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("0", num.ToString());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Name);
            name = new PdfName(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("Prev", name.GetValue());
            tok.NextValidToken();
            NUnit.Framework.Assert.AreEqual(tok.GetTokenType(), PdfTokenizer.TokenType.Number);
            num = new PdfNumber(tok.GetByteContent());
            NUnit.Framework.Assert.AreEqual("-116.23", num.ToString());
        }
Esempio n. 6
0
        /// <summary>Returns the underlying source.</summary>
        /// <returns>The underlying source</returns>
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual IRandomAccessSource GetUnderlyingSource()
        {
            RandomAccessSourceFactory fac = new RandomAccessSourceFactory();

            return(raf == null?fac.CreateSource(bout) : fac.CreateSource(raf));
        }
Esempio n. 7
0
 /// <exception cref="System.IO.IOException"/>
 public virtual RandomAccessFileOrArray GetMetricsFile()
 {
     isBuiltInFont = false;
     if (FontConstants.BUILTIN_FONTS_14.Contains(afmPath))
     {
         isBuiltInFont = true;
         byte[] buf      = new byte[1024];
         Stream resource = null;
         try {
             String resourcePath = FontConstants.AFM_RESOURCE_PATH + afmPath + ".afm";
             resource = ResourceUtil.GetResourceStream(resourcePath);
             if (resource == null)
             {
                 throw new iText.IO.IOException("1.not.found.as.resource").SetMessageParams(resourcePath);
             }
             MemoryStream stream = new MemoryStream();
             int          read;
             while ((read = resource.Read(buf)) >= 0)
             {
                 stream.Write(buf, 0, read);
             }
             buf = stream.ToArray();
         }
         finally {
             if (resource != null)
             {
                 try {
                     resource.Close();
                 }
                 catch (Exception) {
                 }
             }
         }
         return(new RandomAccessFileOrArray(sourceFactory.CreateSource(buf)));
     }
     else
     {
         if (afmPath != null)
         {
             if (afmPath.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".afm"))
             {
                 return(new RandomAccessFileOrArray(sourceFactory.CreateBestSource(afmPath)));
             }
             else
             {
                 if (afmPath.ToLower(System.Globalization.CultureInfo.InvariantCulture).EndsWith(".pfm"))
                 {
                     MemoryStream            ba = new MemoryStream();
                     RandomAccessFileOrArray rf = new RandomAccessFileOrArray(sourceFactory.CreateBestSource(afmPath));
                     Pfm2afm.Convert(rf, ba);
                     rf.Close();
                     return(new RandomAccessFileOrArray(sourceFactory.CreateSource(ba.ToArray())));
                 }
                 else
                 {
                     throw new iText.IO.IOException(iText.IO.IOException._1IsNotAnAfmOrPfmFontFile).SetMessageParams(afmPath);
                 }
             }
         }
         else
         {
             if (afmData != null)
             {
                 RandomAccessFileOrArray rf = new RandomAccessFileOrArray(sourceFactory.CreateSource(afmData));
                 if (IsAfmFile(rf))
                 {
                     return(rf);
                 }
                 else
                 {
                     MemoryStream ba = new MemoryStream();
                     try {
                         Pfm2afm.Convert(rf, ba);
                     }
                     catch (Exception) {
                         throw new iText.IO.IOException("invalid.afm.or.pfm.font.file");
                     }
                     finally {
                         rf.Close();
                     }
                     return(new RandomAccessFileOrArray(sourceFactory.CreateSource(ba.ToArray())));
                 }
             }
             else
             {
                 throw new iText.IO.IOException("invalid.afm.or.pfm.font.file");
             }
         }
     }
 }
        public virtual void InnerArraysInContentStreamTest()
        {
            String      inputFileName = sourceFolder + "innerArraysInContentStream.pdf";
            PdfDocument pdfDocument   = new PdfDocument(new PdfReader(inputFileName));

            byte[] docInBytes = pdfDocument.GetFirstPage().GetContentBytes();
            RandomAccessSourceFactory factory = new RandomAccessSourceFactory();
            PdfTokenizer      tokeniser       = new PdfTokenizer(new RandomAccessFileOrArray(factory.CreateSource(docInBytes)));
            PdfResources      resources       = pdfDocument.GetPage(1).GetResources();
            PdfCanvasParser   ps       = new PdfCanvasParser(tokeniser, resources);
            IList <PdfObject> actual   = ps.Parse(null);
            IList <PdfObject> expected = new List <PdfObject>();

            expected.Add(new PdfString("Cyan"));
            expected.Add(new PdfArray(new int[] { 1, 0, 0, 0 }));
            expected.Add(new PdfString("Magenta"));
            expected.Add(new PdfArray(new int[] { 0, 1, 0, 0 }));
            expected.Add(new PdfString("Yellow"));
            expected.Add(new PdfArray(new int[] { 0, 0, 1, 0 }));
            PdfArray cmpArray = new PdfArray(expected);

            NUnit.Framework.Assert.IsTrue(new CompareTool().CompareArrays(cmpArray, (((PdfDictionary)actual[1]).GetAsArray
                                                                                         (new PdfName("ColorantsDef")))));
        }