Example #1
0
 private void Init()
 {
     ReadXrefPosition();
     ReadXrefHeaders();
     ReadXref();
     gObjectReader = new PdfObjectReader(gpsr, gXref);
     gTrailer      = gObjectReader.ReadTrailer(gTrailerPosition);
 }
Example #2
0
 public PdfWriter(string file, FileMode mode = FileMode.CreateNew, FileShare share = FileShare.Read, Encoding encoding = null, bool updatePdf = false)
 {
     gUpdatePdf = updatePdf;
     if (updatePdf)
         mode = FileMode.Open;
     gfs = new FileStream(file, mode, FileAccess.ReadWrite, share);
     gw = new Writer(gfs, encoding);
     if (updatePdf)
     {
         //gpr = new PdfReader(file);
         gpr = new PdfReader(gfs, encoding);
         gTrailer = gpr.Trailer;
         gXref = gpr.Xref;
         gUpdatePosition = gpr.XrefPosition;
     }
     else
         WriteHeader();
 }
Example #3
0
 public PdfWriter(string file, FileMode mode = FileMode.CreateNew, FileShare share = FileShare.Read, Encoding encoding = null, bool updatePdf = false)
 {
     gUpdatePdf = updatePdf;
     if (updatePdf)
     {
         mode = FileMode.Open;
     }
     //gfs = new FileStream(file, mode, FileAccess.ReadWrite, share);
     gfs = zFile.Open(file, mode, FileAccess.ReadWrite, share);
     gw  = new Writer(gfs, encoding);
     if (updatePdf)
     {
         //gpr = new PdfReader(file);
         gpr             = new PdfReader(gfs, encoding);
         gTrailer        = gpr.Trailer;
         gXref           = gpr.Xref;
         gUpdatePosition = gpr.XrefPosition;
     }
     else
     {
         WriteHeader();
     }
 }
Example #4
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 #5
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 #6
0
 private void Init()
 {
     ReadXrefPosition();
     ReadXrefHeaders();
     ReadXref();
     gObjectReader = new PdfObjectReader(gpsr, gXref);
     gTrailer = gObjectReader.ReadTrailer(gTrailerPosition);
 }
Example #7
0
 public PdfInstructionInilineImage ReadInlineImage()
 {
     PdfInstructionInilineImage instruction = new PdfInstructionInilineImage();
     PdfObjectReader por = new PdfObjectReader(new PdfStreamReader(gr));
     PdfValueObject prm = new PdfValueObject();
     while (true)
     {
         string s = gr.ReadLine();
         if (s == null) throw new PBException("error reading inline image ID not found");
         if (s == "ID") break;
         PdfNValue value = por.ReadPdfNValue(ref s);
         prm[value.name] = value;
         if (s != "") throw new PBException("error reading inline image wrong parameter \"{0}\"", s);
     }
     instruction.prm = prm;
     instruction.stream = gr.ReadBytesUntil("\nEI\n");
     return instruction;
 }