NextToken() public method

public NextToken ( ) : bool
return bool
Esempio n. 1
0
 public static int[] CheckObjectStart(byte[] line)
 {
     try {
         PRTokeniser tk  = new PRTokeniser(line);
         int         num = 0;
         int         gen = 0;
         if (!tk.NextToken() || tk.TokenType != TK_NUMBER)
         {
             return(null);
         }
         num = tk.IntValue;
         if (!tk.NextToken() || tk.TokenType != TK_NUMBER)
         {
             return(null);
         }
         gen = tk.IntValue;
         if (!tk.NextToken())
         {
             return(null);
         }
         if (!tk.StringValue.Equals("obj"))
         {
             return(null);
         }
         return(new int[] { num, gen });
     }
     catch {
     }
     return(null);
 }
Esempio n. 2
0
 public static long[] CheckObjectStart(byte[] line)
 {
     try {
         PRTokeniser tk  = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(line)));
         int         num = 0;
         int         gen = 0;
         if (!tk.NextToken() || tk.TokenType != TokType.NUMBER)
         {
             return(null);
         }
         num = tk.IntValue;
         if (!tk.NextToken() || tk.TokenType != TokType.NUMBER)
         {
             return(null);
         }
         gen = tk.IntValue;
         if (!tk.NextToken())
         {
             return(null);
         }
         if (!tk.StringValue.Equals("obj"))
         {
             return(null);
         }
         return(new long[] { num, gen });
     }
     catch {
     }
     return(null);
 }
 /**
  * Reads the next token skipping over the comments.
  * @return <CODE>true</CODE> if a token was read, <CODE>false</CODE> if the end of content was reached
  * @throws IOException on error
  */
 public bool NextValidToken()
 {
     while (tokeniser.NextToken())
     {
         if (tokeniser.TokenType == PRTokeniser.TK_COMMENT)
         {
             continue;
         }
         return(true);
     }
     return(false);
 }
Esempio n. 4
0
        virtual public bool CompareInnerText(String path1, String path2) {
            PdfReader reader1 = new PdfReader(path1);
            byte[] streamBytes1 = reader1.GetPageContent(1);
            PRTokeniser tokenizer1 =
                new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(streamBytes1)));



            PdfReader reader2 = new PdfReader(path2);
            byte[] streamBytes2 = reader2.GetPageContent(1);
            PRTokeniser tokenizer2 =
                new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(streamBytes2)));

            try {
                while (tokenizer1.NextToken()) {
                    if (!tokenizer2.NextToken())
                        return false;
                    else {
                        if (tokenizer1.TokenType != tokenizer2.TokenType)
                            return false;
                        else {
                            if (tokenizer1.TokenType == tokenizer2.TokenType && tokenizer2.TokenType == PRTokeniser.TokType.NUMBER) {
                                if (Math.Abs(float.Parse(tokenizer1.StringValue, CultureInfo.InvariantCulture)
                                             - float.Parse(tokenizer2.StringValue, CultureInfo.InvariantCulture)) > 0.001)
                                    return false;
                            } else if (!tokenizer1.StringValue.Equals(tokenizer2.StringValue))
                                return false;
                        }

                    }
                }
                return true;
            }
            finally {
                reader1.Close();
                reader2.Close();
            }
        }
Esempio n. 5
0
 virtual protected internal void ReadObjStm(PRStream stream, IntHashtable map) {
     if (stream == null) return;
     int first = stream.GetAsNumber(PdfName.FIRST).IntValue;
     int n = stream.GetAsNumber(PdfName.N).IntValue;
     byte[] b = GetStreamBytes(stream, tokens.File);
     PRTokeniser saveTokens = tokens;
     tokens = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(b)));
     try {
         int[] address = new int[n];
         int[] objNumber = new int[n];
         bool ok = true;
         for (int k = 0; k < n; ++k) {
             ok = tokens.NextToken();
             if (!ok)
                 break;
             if (tokens.TokenType != PRTokeniser.TokType.NUMBER) {
                 ok = false;
                 break;
             }
             objNumber[k] = tokens.IntValue;
             ok = tokens.NextToken();
             if (!ok)
                 break;
             if (tokens.TokenType != PRTokeniser.TokType.NUMBER) {
                 ok = false;
                 break;
             }
             address[k] = tokens.IntValue + first;
         }
         if (!ok)
             throw new InvalidPdfException(MessageLocalization.GetComposedMessage("error.reading.objstm"));
         for (int k = 0; k < n; ++k) {
             if (map.ContainsKey(k)) {
                 tokens.Seek(address[k]);
                 tokens.NextToken();
                 PdfObject obj;
                 if (tokens.TokenType == PRTokeniser.TokType.NUMBER) {
             	    obj = new PdfNumber(tokens.StringValue);
                 }
                 else {
             	    tokens.Seek(address[k]);
             	    obj = ReadPRObject();
                 }
                 xrefObj[objNumber[k]] = obj;
             }
         }            
     }
     finally {
         tokens = saveTokens;
     }
 }
 public static Object[] SplitDAelements(String da)
 {
     PRTokeniser tk = new PRTokeniser(PdfEncodings.ConvertToBytes(da, null));
     ArrayList stack = new ArrayList();
     Object[] ret = new Object[3];
     while (tk.NextToken()) {
         if (tk.TokenType == PRTokeniser.TK_COMMENT)
             continue;
         if (tk.TokenType == PRTokeniser.TK_OTHER) {
             String oper = tk.StringValue;
             if (oper.Equals("Tf")) {
                 if (stack.Count >= 2) {
                     ret[DA_FONT] = stack[stack.Count - 2];
                     ret[DA_SIZE] = float.Parse((String)stack[stack.Count - 1], System.Globalization.NumberFormatInfo.InvariantInfo);
                 }
             }
             else if (oper.Equals("g")) {
                 if (stack.Count >= 1) {
                     float gray = float.Parse((String)stack[stack.Count - 1], System.Globalization.NumberFormatInfo.InvariantInfo);
                     if (gray != 0)
                         ret[DA_COLOR] = new GrayColor(gray);
                 }
             }
             else if (oper.Equals("rg")) {
                 if (stack.Count >= 3) {
                     float red = float.Parse((String)stack[stack.Count - 3], System.Globalization.NumberFormatInfo.InvariantInfo);
                     float green = float.Parse((String)stack[stack.Count - 2], System.Globalization.NumberFormatInfo.InvariantInfo);
                     float blue = float.Parse((String)stack[stack.Count - 1], System.Globalization.NumberFormatInfo.InvariantInfo);
                     ret[DA_COLOR] = new Color(red, green, blue);
                 }
             }
             else if (oper.Equals("k")) {
                 if (stack.Count >= 4) {
                     float cyan = float.Parse((String)stack[stack.Count - 4], System.Globalization.NumberFormatInfo.InvariantInfo);
                     float magenta = float.Parse((String)stack[stack.Count - 3], System.Globalization.NumberFormatInfo.InvariantInfo);
                     float yellow = float.Parse((String)stack[stack.Count - 2], System.Globalization.NumberFormatInfo.InvariantInfo);
                     float black = float.Parse((String)stack[stack.Count - 1], System.Globalization.NumberFormatInfo.InvariantInfo);
                     ret[DA_COLOR] = new CMYKColor(cyan, magenta, yellow, black);
                 }
             }
             stack.Clear();
         }
         else
             stack.Add(tk.StringValue);
     }
     return ret;
 }
Esempio n. 7
0
		public static long[] CheckObjectStart (byte[] line) {
            try {
                PRTokeniser tk = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(line)));
                int num = 0;
                int gen = 0;
                if (!tk.NextToken() || tk.TokenType != TokType.NUMBER)
                    return null;
                num = tk.IntValue;
                if (!tk.NextToken() || tk.TokenType != TokType.NUMBER)
                    return null;
                gen = tk.IntValue;
                if (!tk.NextToken())
                    return null;
                if (!tk.StringValue.Equals("obj"))
                    return null;
                return new long[]{num, gen};
            }
            catch {
            }
            return null;
        }
Esempio n. 8
0
		virtual protected internal PdfObject ReadOneObjStm (PRStream stream, int idx) {
            int first = stream.GetAsNumber(PdfName.FIRST).IntValue;
            byte[] b = GetStreamBytes(stream, tokens.File);
            PRTokeniser saveTokens = tokens;
            tokens = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(b)));
            try {
				int address = 0;
                bool ok = true;
                ++idx;
                for (int k = 0; k < idx; ++k) {
                    ok = tokens.NextToken();
                    if (!ok)
                        break;
                    if (tokens.TokenType != PRTokeniser.TokType.NUMBER) {
                        ok = false;
                        break;
                    }
                    ok = tokens.NextToken();
                    if (!ok)
                        break;
                    if (tokens.TokenType != PRTokeniser.TokType.NUMBER) {
                        ok = false;
                        break;
                    }
                    address = tokens.IntValue + first;
                }
                if (!ok)
                    throw new InvalidPdfException(MessageLocalization.GetComposedMessage("error.reading.objstm"));
                tokens.Seek(address);
                tokens.NextToken();
                PdfObject obj;
                if (tokens.TokenType == PRTokeniser.TokType.NUMBER) {
                    obj = new PdfNumber(tokens.StringValue);
                }
                else {
                    tokens.Seek(address);
                    obj = ReadPRObject();
                }
                return obj;
            }
            finally {
                tokens = saveTokens;
            }
        }
Esempio n. 9
0
		public static long[] CheckObjectStart (byte[] line) {
            try {
                PRTokeniser tk = new PRTokeniser(line);
                int num = 0;
                int gen = 0;
                if (!tk.NextToken() || tk.TokenType != TokType.NUMBER)
                    return null;
                num = tk.IntValue;
                if (!tk.NextToken() || tk.TokenType != TokType.NUMBER)
                    return null;
                gen = tk.IntValue;
                if (!tk.NextToken())
                    return null;
                if (!tk.StringValue.Equals("obj"))
                    return null;
                return new long[]{num, gen};
            }
            catch {
            }
            return null;
        }
        IDictionary<string, IList<object>> ParseDAParam(PdfString DA) {
            IDictionary<string, IList<object>> commandArguments = new Dictionary<string, IList<object>>();

            PRTokeniser tokeniser = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(DA.GetBytes())));
            IList<object> currentArguments = new List<object>();

            while (tokeniser.NextToken()) {
                if (tokeniser.TokenType == PRTokeniser.TokType.OTHER) {
                    String key = tokeniser.StringValue;

                    if (key == "RG" || key == "G" || key == "K") {
                        key = STROKE_COLOR;
                    } else if (key == "rg" || key == "g" || key == "k") {
                        key = FILL_COLOR;
                    }

                    if (commandArguments.ContainsKey(key)) {
                        commandArguments[key] = currentArguments;
                    } else {
                        commandArguments.Add(key, currentArguments);
                    }

                    currentArguments = new List<object>();
                } else {
                    switch (tokeniser.TokenType) {
                        case PRTokeniser.TokType.NUMBER:
                            currentArguments.Add(new PdfNumber(tokeniser.StringValue));
                            break;

                        case PRTokeniser.TokType.NAME:
                            currentArguments.Add(new PdfName(tokeniser.StringValue));
                            break;

                        default:
                            currentArguments.Add(tokeniser.StringValue);
                            break;
                    }
                }
            }

            return commandArguments;
        }
Esempio n. 11
0
 protected internal PdfObject ReadOneObjStm(PRStream stream, int idx) {
     int first = stream.GetAsNumber(PdfName.FIRST).IntValue;
     byte[] b = GetStreamBytes(stream, tokens.File);
     PRTokeniser saveTokens = tokens;
     tokens = new PRTokeniser(b);
     try {
         int address = 0;
         bool ok = true;
         ++idx;
         for (int k = 0; k < idx; ++k) {
             ok = tokens.NextToken();
             if (!ok)
                 break;
             if (tokens.TokenType != PRTokeniser.TK_NUMBER) {
                 ok = false;
                 break;
             }
             ok = tokens.NextToken();
             if (!ok)
                 break;
             if (tokens.TokenType != PRTokeniser.TK_NUMBER) {
                 ok = false;
                 break;
             }
             address = tokens.IntValue + first;
         }
         if (!ok)
             throw new InvalidPdfException(MessageLocalization.GetComposedMessage("error.reading.objstm"));
         tokens.Seek(address);
         return ReadPRObject();
     }
     finally {
         tokens = saveTokens;
     }
 }
Esempio n. 12
0
// ---------------------------------------------------------------------------    
    /**
     * Parses the PDF using PRTokeniser
     * @param src the ]original PDF file
]     */
    public string ParsePdf(byte[] src) {
      PdfReader reader = new PdfReader(src);
      // we can inspect the syntax of the imported page
      byte[] streamBytes = reader.GetPageContent(1);
      StringBuilder sb = new StringBuilder();
      PRTokeniser tokenizer = new PRTokeniser(streamBytes);
      while (tokenizer.NextToken()) {
        if (tokenizer.TokenType == PRTokeniser.TokType.STRING) {
          sb.AppendLine(tokenizer.StringValue);
        }
      }
      return sb.ToString();
    }
Esempio n. 13
0
        public void Process(Crawler crawler, PropertyBag propertyBag)
        {
            AspectF.Define.
                NotNull(crawler, "crawler").
                NotNull(propertyBag, "propertyBag");

            if (propertyBag.StatusCode != HttpStatusCode.OK)
            {
                return;
            }

            if (!IsPdfContent(propertyBag.ContentType))
            {
                return;
            }

            PdfReader pdfReader = new PdfReader(propertyBag.Response);
            try
            {
                object title = pdfReader.Info["Title"];
                if (!title.IsNull())
                {
                    string pdfTitle = Convert.ToString(title, CultureInfo.InvariantCulture).Trim();
                    if (!pdfTitle.IsNullOrEmpty())
                    {
                        propertyBag.Title = pdfTitle;
                    }
                }

                StringBuilder sb = new StringBuilder();
                // Following code from:
                // http://www.vbforums.com/showthread.php?t=475759
                for (int p = 1; p <= pdfReader.NumberOfPages; p++)
                {
                    byte[] pageBytes = pdfReader.GetPageContent(p);

                    if (pageBytes.IsNull())
                    {
                        continue;
                    }

                    PRTokeniser token = new PRTokeniser(pageBytes);
                    while (token.NextToken())
                    {
                        int tknType = token.TokenType;
                        string tknValue = token.StringValue;

                        if (tknType == PRTokeniser.TK_STRING)
                        {
                            sb.Append(token.StringValue);
                            sb.Append(" ");
                        }
                        else if (tknType == 1 && tknValue == "-600")
                        {
                            sb.Append(" ");
                        }
                        else if (tknType == 10 && tknValue == "TJ")
                        {
                            sb.Append(" ");
                        }
                    }
                }

                propertyBag.Text = sb.ToString();
            }
            finally
            {
                pdfReader.Close();
            }
        }
Esempio n. 14
0
        private void OpenPdf()
        {
            _pdfPages.Clear();
            try
            {
                var openFileDialog = new OpenFileDialog
                                         {
                                             DefaultExt = ".pdf",
                                             Filter = "Pdf documents (.pdf)|*.pdf"
                                         };

                bool? result = openFileDialog.ShowDialog();

                if (result == true)
                {
                    string filename = openFileDialog.FileName;
                    var pdfReader = new PdfReader(filename);
                    for (int i = 1; i <= pdfReader.NumberOfPages; i++)
                    {
                        byte[] pagesBytes = pdfReader.GetPageContent(i);
                        var token = new PRTokeniser(pagesBytes);
                        var pageContent = new StringBuilder();
                        while (token.NextToken())
                        {
                            if (token.TokenType == PRTokeniser.TokType.STRING)
                            {
                                pageContent.Append(token.StringValue);
                            }
                        }
                        _pdfPages.Add(pageContent.ToString());
                    }
                }
                RaisePropertyChanged("MaxIndex");
            }
            catch (Exception)
            {
                MessageBox.Show("Fail to load file");
            }
            CurrentIndex = 1;
        }
Esempio n. 15
0
 protected internal void ReadObjStm(PRStream stream, IntHashtable map) {
     int first = stream.GetAsNumber(PdfName.FIRST).IntValue;
     int n = stream.GetAsNumber(PdfName.N).IntValue;
     byte[] b = GetStreamBytes(stream, tokens.File);
     PRTokeniser saveTokens = tokens;
     tokens = new PRTokeniser(b);
     try {
         int[] address = new int[n];
         int[] objNumber = new int[n];
         bool ok = true;
         for (int k = 0; k < n; ++k) {
             ok = tokens.NextToken();
             if (!ok)
                 break;
             if (tokens.TokenType != PRTokeniser.TK_NUMBER) {
                 ok = false;
                 break;
             }
             objNumber[k] = tokens.IntValue;
             ok = tokens.NextToken();
             if (!ok)
                 break;
             if (tokens.TokenType != PRTokeniser.TK_NUMBER) {
                 ok = false;
                 break;
             }
             address[k] = tokens.IntValue + first;
         }
         if (!ok)
             throw new InvalidPdfException("Error reading ObjStm");
         for (int k = 0; k < n; ++k) {
             if (map.ContainsKey(k)) {
                 tokens.Seek(address[k]);
                 PdfObject obj = ReadPRObject();
                 xrefObj[objNumber[k]] = obj;
             }
         }            
     }
     finally {
         tokens = saveTokens;
     }
 }