forked from ststeiger/PdfSharpCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfStringObject.cs
More file actions
149 lines (137 loc) · 5.2 KB
/
PdfStringObject.cs
File metadata and controls
149 lines (137 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#region PDFsharp - A .NET library for processing PDF
//
// Authors:
// Stefan Lange
//
// Copyright (c) 2005-2016 empira Software GmbH, Cologne Area (Germany)
//
// http://www.PdfSharp.com
// http://sourceforge.net/projects/pdfsharp
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion
using System.Diagnostics;
using PdfSharpCore.Pdf.IO;
using PdfSharpCore.Pdf.Internal;
namespace PdfSharpCore.Pdf
{
/// <summary>
/// Represents an indirect text string value. This type is not used by PdfSharpCore. If it is imported from
/// an external PDF file, the value is converted into a direct object.
/// </summary>
[DebuggerDisplay("({Value})")]
public sealed class PdfStringObject : PdfObject
{
/// <summary>
/// Initializes a new instance of the <see cref="PdfStringObject"/> class.
/// </summary>
public PdfStringObject()
{
_flags = PdfStringFlags.RawEncoding;
}
/// <summary>
/// Initializes a new instance of the <see cref="PdfStringObject"/> class.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="value">The value.</param>
public PdfStringObject(PdfDocument document, string value)
: base(document)
{
_value = value;
_flags = PdfStringFlags.RawEncoding;
}
/// <summary>
/// Initializes a new instance of the <see cref="PdfStringObject"/> class.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="encoding">The encoding.</param>
public PdfStringObject(string value, PdfStringEncoding encoding)
{
_value = value;
//if ((flags & PdfStringFlags.EncodingMask) == 0)
// flags |= PdfStringFlags.PDFDocEncoding;
_flags = (PdfStringFlags)encoding;
}
internal PdfStringObject(string value, PdfStringFlags flags)
{
_value = value;
//if ((flags & PdfStringFlags.EncodingMask) == 0)
// flags |= PdfStringFlags.PDFDocEncoding;
_flags = flags;
}
/// <summary>
/// Gets the number of characters in this string.
/// </summary>
public int Length
{
get { return _value == null ? 0 : _value.Length; }
}
/// <summary>
/// Gets or sets the encoding.
/// </summary>
public PdfStringEncoding Encoding
{
get { return (PdfStringEncoding)(_flags & PdfStringFlags.EncodingMask); }
set { _flags = (_flags & ~PdfStringFlags.EncodingMask) | ((PdfStringFlags)value & PdfStringFlags.EncodingMask); }
}
/// <summary>
/// Gets a value indicating whether the string is a hexadecimal literal.
/// </summary>
public bool HexLiteral
{
get { return (_flags & PdfStringFlags.HexLiteral) != 0; }
set { _flags = value ? _flags | PdfStringFlags.HexLiteral : _flags & ~PdfStringFlags.HexLiteral; }
}
PdfStringFlags _flags;
/// <summary>
/// Gets or sets the value as string
/// </summary>
public string Value
{
get { return _value ?? ""; }
set { _value = value ?? ""; }
}
string _value;
/// <summary>
/// Gets or sets the string value for encryption purposes.
/// </summary>
internal byte[] EncryptionValue
{
// TODO: Unicode case is not handled!
get { return _value == null ? new byte[0] : PdfEncoders.RawEncoding.GetBytes(_value); }
set { _value = PdfEncoders.RawEncoding.GetString(value, 0, value.Length); }
}
/// <summary>
/// Returns the string.
/// </summary>
public override string ToString()
{
return _value;
}
/// <summary>
/// Writes the string literal with encoding DOCEncoded.
/// </summary>
internal override void WriteObject(PdfWriter writer)
{
writer.WriteBeginObject(this);
writer.Write(new PdfString(_value, _flags));
writer.WriteEndObject();
}
}
}