コード例 #1
0
 /**
  * Concatenates a PDF document selecting the pages to keep. The pages are described as
  * ranges. The page ordering can be changed but
  * no page repetitions are allowed.
  * @param reader the PDF document
  * @param ranges the comma separated ranges as described in {@link SequenceList}
  * @throws DocumentException on error
  */
 public void AddDocument(PdfReader reader, String ranges)
 {
     fc.AddDocument(reader, SequenceList.Expand(ranges, reader.NumberOfPages));
 }
コード例 #2
0
        /**
         * Generates a list of numbers from a string.
         * @param ranges the comma separated ranges
         * @param maxNumber the maximum number in the range
         * @return a list with the numbers as <CODE>Integer</CODE>
         */
        public static ArrayList Expand(String ranges, int maxNumber)
        {
            SequenceList parse = new SequenceList(ranges);
            ArrayList    list  = new ArrayList();
            bool         sair  = false;

            while (!sair)
            {
                sair = parse.GetAttributes();
                if (parse.low == -1 && parse.high == -1 && !parse.even && !parse.odd)
                {
                    continue;
                }
                if (parse.low < 1)
                {
                    parse.low = 1;
                }
                if (parse.high < 1 || parse.high > maxNumber)
                {
                    parse.high = maxNumber;
                }
                if (parse.low > maxNumber)
                {
                    parse.low = maxNumber;
                }

                //System.out.Println("low="+parse.low+",high="+parse.high+",odd="+parse.odd+",even="+parse.even+",inverse="+parse.inverse);
                int inc = 1;
                if (parse.inverse)
                {
                    if (parse.low > parse.high)
                    {
                        int t = parse.low;
                        parse.low  = parse.high;
                        parse.high = t;
                    }
                    for (ListIterator it = new ListIterator(list); it.HasNext();)
                    {
                        int n = (int)it.Next();
                        if (parse.even && (n & 1) == 1)
                        {
                            continue;
                        }
                        if (parse.odd && (n & 1) == 0)
                        {
                            continue;
                        }
                        if (n >= parse.low && n <= parse.high)
                        {
                            it.Remove();
                        }
                    }
                }
                else
                {
                    if (parse.low > parse.high)
                    {
                        inc = -1;
                        if (parse.odd || parse.even)
                        {
                            --inc;
                            if (parse.even)
                            {
                                parse.low &= ~1;
                            }
                            else
                            {
                                parse.low -= ((parse.low & 1) == 1 ? 0 : 1);
                            }
                        }
                        for (int k = parse.low; k >= parse.high; k += inc)
                        {
                            list.Add(k);
                        }
                    }
                    else
                    {
                        if (parse.odd || parse.even)
                        {
                            ++inc;
                            if (parse.odd)
                            {
                                parse.low |= 1;
                            }
                            else
                            {
                                parse.low += ((parse.low & 1) == 1 ? 1 : 0);
                            }
                        }
                        for (int k = parse.low; k <= parse.high; k += inc)
                        {
                            list.Add(k);
                        }
                    }
                }
            }
            return(list);
        }