internal void                WriteStream(byte[] streamData, int dataLength)
 {
     WriteByteStr(bsStreamBegin);
     WriteByteArray(streamData, 0, dataLength);
     WriteByteArray(bsStreamEnd);
     _previousValue = PdfValueType.StreamEnd;
 }
 internal void                WriteReference(PdfWriterReference reference)
 {
     WriteInteger(reference.Id);
     WriteInteger(0);
     WriteByteArray(bsRelative);
     _previousValue = PdfValueType.Reference;
 }
        internal void                WriteName(string name)
        {
            WriteByte((byte)'/');

            for (int i = 0; i < name.Length; ++i)
            {
                char chr = name[i];

                if (chr > (char)255)
                {
                    throw new PdfExceptionWriter("Invalid character in name.");
                }

                if (chr < (char)33 || chr > (char)126)
                {
                    WriteByte((byte)'#');
                    WriteHex((byte)chr);
                }
                else
                {
                    WriteByte((byte)chr);
                }
            }

            _previousValue = PdfValueType.Name;
        }
 internal void                WriteEOF(int pos)
 {
     WriteByteArray(PdfStreamWriter.bsStartXref);
     _previousValue = PdfValueType.None;
     WriteInteger(pos);
     WriteNewLine();
     WriteByteArray(PdfStreamWriter.bsFileEOF);
 }
        internal void                WriteStringRaw(byte[] value)
        {
            WriteByte((byte)'(');
            WriteByteArray(value, 0, value.Length);
            WriteByte((byte)')');

            _previousValue = PdfValueType.String;
        }
 internal void                WriteXrefHeader(int cnt)
 {
     WriteByteArray(PdfStreamWriter.bsXref);
     _previousValue = PdfValueType.None;
     WriteInteger(0);
     WriteInteger(cnt);
     WriteNewLine();
 }
Exemple #7
0
        public PdfValue                    ReadToken(PdfValueType expectedType)
        {
            PdfValue token = ReadToken();

            if (token.Type != expectedType)
            {
                throw new PdfExceptionReader("PDF stream corrupt: expect '" + expectedType.ToString() + "' read '" + token.Type.ToString() + "'");
            }

            return(token);
        }
        internal void                WriteStringHex(byte[] value)
        {
            WriteByte((byte)'<');

            for (int i = 0; i < value.Length; ++i)
            {
                WriteHex(value[i]);
            }

            WriteByte((byte)'>');

            _previousValue = PdfValueType.HexadecimalString;
        }
        internal void                WriteInteger(int value)
        {
            switch (_previousValue)
            {
            case PdfValueType.None:
            case PdfValueType.DictionaryBegin:
            case PdfValueType.ArrayBegin:
            case PdfValueType.ObjectEnd:
                break;

            default:
                WriteSeparator();
                break;
            }

            if (value != 0)
            {
                bool   sign = false;
                byte[] buf  = new byte[16];
                int    pos  = 16;

                if (value < 0)
                {
                    sign  = true;
                    value = -value;
                }

                while (value > 0)
                {
                    buf[--pos] = (byte)('0' + (value % 10));
                    value     /= 10;
                }

                if (sign)
                {
                    buf[--pos] = (byte)'-';
                }

                WriteByteArray(buf, pos, 16 - pos);
            }
            else
            {
                WriteByte((byte)'0');
            }

            _previousValue = PdfValueType.Integer;
        }
        internal void                WriteBoolean(bool value)
        {
            switch (_previousValue)
            {
            case PdfValueType.None:
            case PdfValueType.DictionaryBegin:
            case PdfValueType.ArrayBegin:
                break;

            default:
                WriteSeparator();
                break;
            }

            WriteByteArray((value) ? bsTrue : bsFalse);
            _previousValue = PdfValueType.Boolean;
        }
        internal void                WriteNull()
        {
            switch (_previousValue)
            {
            case PdfValueType.None:
            case PdfValueType.DictionaryBegin:
            case PdfValueType.ArrayBegin:
                break;

            default:
                WriteSeparator();
                break;
            }

            WriteByteArray(bsNull);
            _previousValue = PdfValueType.Null;
        }
        internal void                WriteByteStr(byte[] str)
        {
            switch (_previousValue)
            {
            case PdfValueType.None:
            case PdfValueType.DictionaryBegin:
            case PdfValueType.ArrayBegin:
            case PdfValueType.DictionaryEnd:
            case PdfValueType.ArrayEnd:
            case PdfValueType.ObjectBegin:
            case PdfValueType.ObjectEnd:
                break;

            default:
                WriteSeparator();
                break;
            }

            WriteByteArray(str);
            _previousValue = PdfValueType.ByteStr;
        }
        internal void                WriteStream(Stream stream, int dataLength)
        {
            byte[] buf = new byte[4096];

            WriteByteStr(bsStreamBegin);

            while (dataLength > 0)
            {
                int size = (dataLength > buf.Length) ? buf.Length : dataLength;

                if (stream.Read(buf, 0, size) != size)
                {
                    throw new PdfExceptionWriter("EOF read on object stream.");
                }

                WriteByteArray(buf, 0, size);

                dataLength -= size;
            }

            WriteByteArray(bsStreamEnd);
            _previousValue = PdfValueType.StreamEnd;
        }
        internal PdfValueType            pdfReadChildren(PdfStreamReader reader, PdfValueType endValueType)
        {
            PdfValue token;

            while ((token = reader.ReadToken()).Type != endValueType)
            {
                switch (token.Type)
                {
                case PdfValueType.Reference:            pdfReadReference(reader);           break;

                case PdfValueType.ArrayBegin:           pdfReadArray(reader);               break;

                case PdfValueType.DictionaryBegin:      pdfReadDictionary(reader);          break;

                case PdfValueType.ObjectBegin:  // This is a syntax error in the stream but eDocPrint somtimes fails to write endobj.....
                case PdfValueType.StreamBegin:
                    if (endValueType == PdfValueType.ObjectEnd)
                    {
                        return(token.Type);
                    }
                    goto error;

                case PdfValueType.ObjectEnd:
                case PdfValueType.DictionaryEnd:
                case PdfValueType.ArrayEnd:
                case PdfValueType.StreamEnd:
                    error :              throw new PdfException("Unexpected token " + token.Type.ToString() + " in stream.");

                default:
                    base.Add(token);
                    break;
                }
            }

            return(token.Type);
        }
 internal PdfStreamWriter     ResetState()
 {
     _previousValue = PdfValueType.None;
     return(this);
 }
 internal void                WriteObjEnd()
 {
     WriteByteArray(bsObjEnd);
     _previousValue = PdfValueType.ObjectEnd;
 }
 internal void                WriteObjBegin()
 {
     WriteByteStr(bsObjBegin);
     _previousValue = PdfValueType.ObjectBegin;
 }
 internal void                WriteDictionaryEnd()
 {
     WriteByteArray(bsDictionaryEnd);
     _previousValue = PdfValueType.DictionaryEnd;
 }
 public PdfStreamWriter(Stream pdfStream)
 {
     _pdfStream     = (pdfStream is MemoryStream) ? pdfStream : new BufferedStream(pdfStream);
     _pdfPosition   = 0;
     _previousValue = PdfValueType.None;
 }
 internal void                WriteNewLine()
 {
     WriteByte((byte)'\n');
     _previousValue = PdfValueType.None;
 }
        internal void                WriteNumber(double value)
        {
            switch (_previousValue)
            {
            case PdfValueType.None:
            case PdfValueType.DictionaryBegin:
            case PdfValueType.ArrayBegin:
                break;

            default:
                WriteSeparator();
                break;
            }

            bool sign = false;
            int  prec = 2;

            byte[] buf = new byte[16];
            int    pos = buf.Length;
            bool   f   = false;

            if (value < 0)
            {
                sign  = true;
                value = -value;
            }

            switch (prec)
            {
            case 0:
            case 1:     value *= 10.0;      break;

            case 2:     value *= 100.0;      break;

            case 3:     value *= 1000.0;      break;

            case 4:     value *= 10000.0;      break;

            case 5:     value *= 100000.0;      break;
            }

            Int64 IntValue = (Int64)(value + 0.5);

            if (IntValue == 0)
            {
                buf[--pos] = (byte)'0';
            }
            else
            {
                ++prec;

                while (IntValue > 0 || prec > 0)
                {
                    if (prec > 0)
                    {
                        if (--prec == 0)
                        {
                            if (f)
                            {
                                buf[--pos] = (byte)'.';
                            }

                            f = true;
                        }
                    }

                    if (f || (IntValue % 10) != 0)
                    {
                        buf[--pos] = (byte)('0' + (IntValue % 10));
                        f          = true;
                    }

                    IntValue /= 10;
                }

                if (sign)
                {
                    buf[--pos] = (byte)'-';
                }
            }

            WriteByteArray(buf, pos, buf.Length - pos);

            _previousValue = PdfValueType.Number;
        }
 internal void                WriteSeparator()
 {
     WriteByte((byte)' ');
     _previousValue = PdfValueType.None;
 }
 internal void                WriteArrayEnd()
 {
     WriteByte((byte)']');
     _previousValue = PdfValueType.ArrayEnd;
 }
        internal void                WriteArrayBegin()
        {
            WriteByte((byte)'[');

            _previousValue = PdfValueType.ArrayBegin;
        }
        internal void                WriteString(string value)
        {
            int  Length  = value.Length;
            bool unicode = false;

            {
                for (int i = 0; i < Length; ++i)
                {
                    if (value[i] > (char)126)
                    {
                        unicode = true;
                        break;
                    }
                }
            }

            if (unicode)
            {
                WriteByte((byte)'<');
                WriteByteArray(bsUnicodePrefix, 0, bsUnicodePrefix.Length);

                for (int i = 0; i < Length; ++i)
                {
                    WriteHex(value[i]);
                }

                WriteByte((byte)'>');
                _previousValue = PdfValueType.HexadecimalString;
            }
            else
            {
                WriteByte((byte)'(');

                for (int i = 0; i < Length; ++i)
                {
                    char chr = value[i];

                    if (chr < (char)32)
                    {
                        WriteByte((byte)'\\');

                        switch (chr)
                        {
                        case '\n':  WriteByte((byte)'n');       break;

                        case '\r':  WriteByte((byte)'r');       break;

                        case '\t':  WriteByte((byte)'t');       break;

                        case '\b':  WriteByte((byte)'b');       break;

                        case '\f':  WriteByte((byte)'f');       break;

                        default:
                            WriteByte((byte)('0' + ((chr >> 6) & 0x7)));
                            WriteByte((byte)('0' + ((chr >> 3) & 0x7)));
                            WriteByte((byte)('0' + ((chr) & 0x7)));
                            break;
                        }
                    }
                    else
                    {
                        if (chr == '(' || chr == ')' || chr == '\\')
                        {
                            WriteByte((byte)'\\');
                        }

                        WriteByte((byte)chr);
                    }
                }

                WriteByte((byte)')');
                _previousValue = PdfValueType.String;
            }
        }
 internal void                WriteDictionaryBegin()
 {
     WriteByteArray(bsDictionaryBegin);
     _previousValue = PdfValueType.DictionaryBegin;
 }
 public PdfToken(PdfValueType type)
 {
     this._type = type;
 }
 internal void                WriteTrailer()
 {
     WriteByteArray(PdfStreamWriter.bsTrailer);
     _previousValue = PdfValueType.None;
 }