Example #1
0
 public void UpdateObject(IPdfObject obj, string trailerName = null)
 {
     if (!gUpdatePdf)
     {
         throw new PBException("error object update is not activate \"{0}\"", gw.File);
     }
     if (!gXref.ContainsKey(obj.id))
     {
         throw new PBException("error update object {0}, object dont exist", obj.id);
     }
     gw.Seek(gUpdatePosition);
     gXref[obj.id] = new PdfXref {
         objectId = obj.id, generationNumber = obj.generationNumber, filePosition = gUpdatePosition
     };
     if (trailerName != null)
     {
         gTrailer[trailerName] = new PdfNValue {
             name = trailerName, value = new PdfValueObjectRef {
                 valueObjectId = obj.id, valueObjectGenerationNumber = obj.generationNumber
             }
         }
     }
     ;
     _WriteObject(obj);
     gUpdatePosition = (int)gw.Position;
 }
Example #2
0
 public void WriteObject(IPdfObject obj, string trailerName = null)
 {
     if (gXref.ContainsKey(obj.id))
     {
         throw new PBException("error object already in file id {0}", obj.id);
     }
     gXref.Add(obj.id, new PdfXref {
         objectId = obj.id, generationNumber = obj.generationNumber, filePosition = gw.Position
     });
     if (trailerName != null)
     {
         gTrailer[trailerName] = new PdfNValue {
             name = trailerName, value = new PdfValueObjectRef {
                 valueObjectId = obj.id, valueObjectGenerationNumber = obj.generationNumber
             }
         }
     }
     ;
     _WriteObject(obj);
     //gw.Write("{0} {1} obj\n", obj.id, obj.generationNumber);
     //WriteValue(obj.value);
     //gw.Write("\n");
     //if (obj.stream != null)
     //{
     //    gw.Write("stream\n");
     //    gw.Write(obj.stream);
     //    gw.Write("\n");
     //    gw.Write("endstream\n");
     //}
     //gw.Write("endobj\n");
 }
Example #3
0
        public IPdfValue ReadPdfValue(ref string s)
        {
            // 1 0 R
            // /OneColumn
            // /Producer (FPDF 1.6)
            // /CreationDate (D:20121205105056)
            // [3 0 R /FitH null]
            // [0 0 907.09 1292.59]
            while (s.Length == 0)
            {
                s = gpsr.ReadLine();
            }
            Regex rg_value_name       = new Regex(@"^/([a-zA-Z0-9_\-\+]+) *", RegexOptions.Compiled);
            Regex rg_value_string     = new Regex(@"^\((?:(D):([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}))?([^)]*)\)", RegexOptions.Compiled);
            Regex rg_end_array        = new Regex(@"^ *\] *", RegexOptions.Compiled);
            Regex rg_value_bool       = new Regex(@"^ *(true|false) *", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex rg_value_object_ref = new Regex("^ *([0-9]+) +([0-9]+) +R *", RegexOptions.Compiled);
            Regex rg_value_double     = new Regex(@"^ *(-?[0-9]+\.[0-9]+) *", RegexOptions.Compiled);
            Regex rg_value_int        = new Regex(@"^ *(-?[0-9]+) *", RegexOptions.Compiled);
            Regex rg_value_null       = new Regex(@"^ *null *", RegexOptions.Compiled);
            Match match;

            if (s[0] == '/')
            {
                match = rg_value_name.Match(s);
                if (!match.Success)
                {
                    throw new PBException("error reading value {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
                }
                s = s.Substring(match.Length);
                return(new PdfValueName {
                    valueName = match.Groups[1].Value
                });
            }
            // /Producer (FPDF 1.6)
            if (s[0] == '(')
            {
                match = rg_value_string.Match(s);
                if (!match.Success)
                {
                    throw new PBException("error reading value {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
                }
                s = s.Substring(match.Length);
                switch (match.Groups[1].Value)
                {
                case "D":
                    return(new PdfValueDateTime {
                        valueDateTime = new DateTime(int.Parse(match.Groups[2].Value), int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value),
                                                     int.Parse(match.Groups[5].Value), int.Parse(match.Groups[6].Value), int.Parse(match.Groups[7].Value))
                    });

                case "":
                    return(new PdfValueString {
                        valueString = match.Groups[8].Value
                    });
                }
            }
            if (s[0] == '[')
            {
                s = s.Substring(1);
                List <IPdfValue> values = new List <IPdfValue>();
                while (true)
                {
                    match = rg_end_array.Match(s);
                    if (match.Success)
                    {
                        s = s.Substring(match.Length);
                        break;
                    }
                    values.Add(ReadPdfValue(ref s));
                }
                return(new PdfValueArray {
                    arrayValues = values.ToArray()
                });
            }
            if (s.StartsWith("<<"))
            {
                PdfValueObject obj = new PdfValueObject();
                if (s.Length > 2)
                {
                    s = s.Substring(2);
                }
                else
                {
                    s = gpsr.ReadLine();
                }
                while (true)
                {
                    if (s.StartsWith(">>"))
                    {
                        s = s.Substring(2);
                        break;
                    }
                    PdfNValue value = ReadPdfNValue(ref s);
                    obj[value.name] = value;
                    if (s == "")
                    {
                        s = gpsr.ReadLine();
                    }
                }
                return(obj);
            }
            match = rg_value_bool.Match(s);
            if (match.Success)
            {
                s = s.Substring(match.Length);
                return(new PdfValueBool {
                    valueBool = match.Groups[1].Value.ToLower() == "true" ? true : false
                });
            }
            match = rg_value_object_ref.Match(s);
            if (match.Success)
            {
                s = s.Substring(match.Length);
                return(new PdfValueObjectRef {
                    valueObjectId = int.Parse(match.Groups[1].Value), valueObjectGenerationNumber = int.Parse(match.Groups[2].Value)
                });
            }
            match = rg_value_double.Match(s);
            if (match.Success)
            {
                s = s.Substring(match.Length);
                return(new PdfValueDouble {
                    valueDouble = double.Parse(match.Groups[1].Value)
                });
            }
            match = rg_value_int.Match(s);
            if (match.Success)
            {
                s = s.Substring(match.Length);
                return(new PdfValueInt {
                    valueInt = int.Parse(match.Groups[1].Value)
                });
            }
            match = rg_value_null.Match(s);
            if (match.Success)
            {
                s = s.Substring(match.Length);
                return(new PdfValueNull());
            }
            throw new PBException("error reading value {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
        }
Example #4
0
        public IPdfObject ReadObject(int id)
        {
            // trailer
            // <<
            // /Size 5274
            // /Root 5273 0 R
            // /Info 5272 0 R
            // >>

            // 5273 0 obj
            // <<
            // /Type /Catalog
            // /Pages 1 0 R
            // /OpenAction [3 0 R /FitH null]
            // /PageLayout /OneColumn
            // >>
            // endobj

            // 3 0 obj
            // <</Type /Page
            // /Parent 1 0 R
            // /MediaBox [0 0 907.09 1292.59]
            // /Resources 2 0 R
            // /Contents 4 0 R>>
            // endobj

            if (!gXref.ContainsKey(id))
            {
                throw new PBException("error unknow object {0}", id);
            }
            PdfXref xref = gXref[id];

            gpsr.StartReadObject(string.Format("object {0}", id));
            gpsr.Seek(xref.filePosition);
            // 5273 0 obj
            Regex rg_object_begin = new Regex("^([0-9]+) ([0-9]+) obj$", RegexOptions.Compiled);
            PdfObjectDictionary o = new PdfObjectDictionary(id, xref.generationNumber);
            string s     = gpsr.ReadLine();
            Match  match = rg_object_begin.Match(s);

            if (!match.Success || int.Parse(match.Groups[1].Value) != id || int.Parse(match.Groups[2].Value) != xref.generationNumber)
            {
                throw new PBException("error reading {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
            }
            s = gpsr.ReadLine();

            o.value = ReadPdfValue(ref s);

            if (s == "")
            {
                s = gpsr.ReadLine();
            }
            if (s == "stream")
            {
                int       length      = 0;
                PdfNValue lengthValue = null;
                if (o.value.isObject())
                {
                    lengthValue = o.value["Length"];
                }
                if (lengthValue == null)
                {
                    throw new PBException("error stream without /Length reading {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
                }
                if (lengthValue.value.isInt())
                {
                    length = lengthValue.value.valueInt;
                }
                else if (lengthValue.value.isObjectRef())
                {
                    long            position     = gpsr.Position;
                    PdfObjectReader or           = new PdfObjectReader(this);
                    IPdfObject      lengthObject = or.ReadObject(lengthValue.value.valueObjectId);
                    gpsr.Seek(position);
                    if (lengthObject.value.isInt())
                    {
                        length = lengthObject.value.valueInt;
                    }
                }
                if (length != 0)
                {
                    o.stream = gpsr.ReadStream(length);
                }
                else
                {
                    o.stream = gpsr.ReadStream();
                }
                s = gpsr.ReadLine();
            }
            if (s != "endobj")
            {
                throw new PBException("error endobj not found reading {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
            }
            o.source = gpsr.GetObjectSource();
            return(o);
        }
Example #5
0
 public void UpdateObject(IPdfObject obj, string trailerName = null)
 {
     if (!gUpdatePdf) throw new PBException("error object update is not activate \"{0}\"", gw.File);
     if (!gXref.ContainsKey(obj.id)) throw new PBException("error update object {0}, object dont exist", obj.id);
     gw.Seek(gUpdatePosition);
     gXref[obj.id] = new PdfXref { objectId = obj.id, generationNumber = obj.generationNumber, filePosition = gUpdatePosition };
     if (trailerName != null)
         gTrailer[trailerName] = new PdfNValue { name = trailerName, value = new PdfValueObjectRef { valueObjectId = obj.id, valueObjectGenerationNumber = obj.generationNumber } };
     _WriteObject(obj);
     gUpdatePosition = (int)gw.Position;
 }
Example #6
0
 public void WriteObject(IPdfObject obj, string trailerName = null)
 {
     if (gXref.ContainsKey(obj.id)) throw new PBException("error object already in file id {0}", obj.id);
     gXref.Add(obj.id, new PdfXref { objectId = obj.id, generationNumber = obj.generationNumber, filePosition = gw.Position });
     if (trailerName != null)
         gTrailer[trailerName] = new PdfNValue { name = trailerName, value = new PdfValueObjectRef { valueObjectId = obj.id, valueObjectGenerationNumber = obj.generationNumber } };
     _WriteObject(obj);
     //gw.Write("{0} {1} obj\n", obj.id, obj.generationNumber);
     //WriteValue(obj.value);
     //gw.Write("\n");
     //if (obj.stream != null)
     //{
     //    gw.Write("stream\n");
     //    gw.Write(obj.stream);
     //    gw.Write("\n");
     //    gw.Write("endstream\n");
     //}
     //gw.Write("endobj\n");
 }