📄 pdfreader.cs
字号:
set
{
generationNumber = value;
}
}
}
/// <summary>
/// Represents a PDF Null object. See the PDF Reference 3.2.8 Null Object.
/// </summary>
[CLSCompliant(true)]
public class PdfNull: PdfObject
{
/// <summary>
/// Returns "null".
/// </summary>
/// <returns>The string "null".</returns>
public override string ToString()
{
return "null";
}
}
/// <summary>
/// Represents a PDF Array object. See the PDF Reference 3.2.5 Array Objects.
/// </summary>
[CLSCompliant(true)]
public class PdfArray: PdfObject
{
private ArrayList array = new ArrayList();
private static readonly Regex endRegex = new Regex(@"^\s*\]", RegexOptions.Singleline);
/// <summary>
/// Initializes a new PdfArray object.
/// </summary>
/// <param name="input">The input string to be parsed.
/// Must not contain the leading '['. Must contain the trailing ']'.
/// The PdfArray is consumed from the input.</param>
public PdfArray(ref string input)
{
bool match;
input = input.TrimStart(null);
for (match = input.StartsWith("]"); !match && input.Length > 0; match = input.StartsWith("]"))
{
array.Add(PdfObject.GetPdfObject(ref input));
input = input.TrimStart(null);
}
if (match)
{
input = input.Substring(1);
}
}
/// <summary>
/// Initializes a new PdfArray object.
/// </summary>
/// <param name="items">The array of PdfObjects from which to construct the new PdfArray.</param>
public PdfArray(params PdfObject[] items)
{
if (items != null)
{
array.AddRange(items);
}
}
/// <summary>
/// Returns the string representation of this object.
/// </summary>
/// <returns>The string representation of this object.</returns>
public override string ToString()
{
string s = "[ ";
foreach (PdfObject element in array)
{
s += element.ToString() + " ";
}
s += "]";
return s;
}
/// <summary>
/// Gets the elements of the PdfArray.
/// </summary>
/// <value>The elements of the PdfArray.</value>
public ArrayList Elements
{
get
{
return array;
}
}
/// <summary>
/// Indexer for the PdfArray.
/// </summary>
public PdfObject this [int index]
{
get
{
return (PdfObject)array[index];
}
set
{
array[index] = value;
}
}
}
/// <summary>
/// Represents a PDF String object. See the PDF Reference 3.2.3 String Objects.
/// </summary>
/// <remarks>
/// This implementation can parse PDF Strings in either literal or hexadecimal form.
/// It writes strings always in literal form.
/// Unicode (UTF-16) Encoding is supported, bytes in PdfDocEncoding are interpreted as Unicode.
/// Language detection is not supported.
/// </remarks>
[CLSCompliant(true)]
public class PdfString: PdfObject
{
private string s = "";
private static readonly Regex hexRegex = new Regex(@"^([^>]+)>", RegexOptions.Singleline);
private static readonly Regex octRegex = new Regex(@"^([0-7]{3})", RegexOptions.Singleline);
private static readonly Regex wsRegex = new Regex(@"\s+", RegexOptions.Singleline);
private static readonly Encoding encoding = new UnicodeEncoding(true, true);
/// <summary>
/// Initializes a new instance of PdfString.
/// </summary>
/// <param name="hex">Is this string in hexadecimal form?</param>
/// <param name="input">The string to be parsed into a PdfString object. Must not include the leading '('.
/// Must include the trailing ')'. Consumes the PDF String object from the input.
/// </param>
public PdfString(bool hex, ref string input)
{
if (hex)
{
Match hexMatch = hexRegex.Match(input);
if (!hexMatch.Success)
{
throw new Exception("Cannot parse hexadecimal string at '" + input + "'");
}
string hexString = hexMatch.Groups[1].Value;
hexString = wsRegex.Replace(hexString, "");
input = input.Substring(hexMatch.Index + hexMatch.Length);
if ((hexString.Length & 1) == 1) // odd number of characters, append 0 according to PDF spec
{
hexString += "0";
}
byte[] hexBytes = new byte[hexString.Length / 2];
for (int i = 0; i < hexString.Length; i += 2)
{
byte b = Byte.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture);
hexBytes[i / 2] = b;
}
if (hexBytes.Length >= 2 && hexBytes[0] == 0xfe && hexBytes[1] == 0xff) // utf16 encoding
{
s = encoding.GetString(hexBytes, 2, hexBytes.Length - 2);
}
else
{
char[] chars = new char[hexBytes.Length];
for (int i = 0; i < hexBytes.Length; i++)
{
chars[i] = (char)hexBytes[i];
}
s = new String(chars);
}
}
else
{
int nestLevel = 1;
bool unicode = input.Length >= 2
&& ((input[0] == (char)0xfe && input[1] == (char)0xff)
|| input.StartsWith("\\376\\377"));
if (unicode) // strip byte-order marker
{
input = input.Substring(input[0] == '\\' ? 8 : 2);
}
while (nestLevel > 0 && input.Length > 0)
{
if (input[0] == ')' && nestLevel == 1)
{
nestLevel = 0;
input = input.Substring(1);
break;
}
string currentChars = GetNextChar(ref input, unicode);
if (currentChars.Equals(""))
{
continue;
}
char currentChar = currentChars[0];
if (currentChar == '(')
{
nestLevel++;
}
else if (currentChar == ')')
{
nestLevel--;
}
if (currentChar == '\\' && currentChars.Length == 2) // can only be "\(" or "\)"
{
currentChar = currentChars[1];
}
s += currentChar;
}
}
}
/// <summary>
/// Initializes a new PdfString object.
/// </summary>
/// <param name="s">A string from which to initialize the PdfString object.</param>
public PdfString(string s)
{
this.s = s;
}
private string GetNextInputChar(ref string input)
{
int len = 1;
char currentChar = input[0];
char nextCharacter = input[1];
string returnChar = "";
if (input.Length >= 2
&& currentChar == '\\')
{
len += 1; // strip the \
switch (nextCharacter)
{
case ')':
case '(':
case '\\':
returnChar += "\\" + nextCharacter;
break;
case 'n':
returnChar += '\n';
break;
case 'r':
returnChar += '\r';
break;
case 't':
returnChar += '\t';
break;
case 'b':
returnChar += '\b';
break;
case 'f':
returnChar += '\f';
break;
}
Match octMatch = octRegex.Match(input.Substring(1));
if (octMatch.Success)
{
len += 2; // strip two more characters from input
int num = Convert.ToInt32(octMatch.Groups[1].Value, 8);
returnChar += (char)num;
}
}
else if (input.Length >= 2
&& (currentChar == '\n' || currentChar == '\r'))
{
returnChar += "\n";
if (nextCharacter == '\n' || nextCharacter == '\r') // two-byte line ending
{
len += 1;
}
}
else
{
returnChar += currentChar;
}
input = input.Substring(len);
return returnChar;
}
private string GetNextChar(ref string input, bool unicode)
{
string nextCharacter = GetNextInputChar(ref input);
if (nextCharacter.Equals(""))
{
return "";
}
if (unicode)
{
try
{
string c2s;
do
{
c2s = GetNextInputChar(ref input);
}
while (c2s.Equals(""));
char c2 = c2s[0];
nextCharacter = "" + encoding.GetChars(new byte[] { (byte)nextCharacter[0], (byte)c2 }, 0, 2)[0];
}
catch (Exception ex)
{
throw new Exception("error parsing unicode string", ex);
}
}
return nextCharacter;
}
/// <summary>
/// Returns the PdfString as a string.
/// </summary>
public string Text
{
get
{
return s;
}
set
{
s = value;
}
}
/// <summary>
/// Returns the encoded representation of the PdfString object. Automatically chooses Unicode or PdfDocEncoding.
/// Returned string is always in literal format.
/// </summary>
/// <returns>The encoded string.</returns>
public override string ToString()
{
string output = s.Replace("(", "\\(");
output = output.Replace(")", "\\)");
bool unicode = false;
foreach (char c in output.ToCharArray())
{
if ((UInt16)c > 255)
{
unicode = true;
break;
}
}
if (unicode)
{
string str = "(" + (char)0xfe + (char)0xff;
byte[] bytes = encoding.GetBytes(output);
foreach (byte b in bytes)
{
str += (char)b;
}
str += ")";
return str;
}
else
{
return "(" + output + ")";
}
}
}
/// <summary>
/// Represents a PDF Name object. See the PDF Reference 3.2.4 Name Objects.
/// </summary>
[CLSCompliant(true)]
public class PdfName: PdfObject
{
private string name;
private static readonly Regex hexRegex = new Regex(@"#([A-Fa-f0-9]{2})", RegexOptions.Singleline);
private static readonly Regex specialRegex = new Regex(@"[\x00-\x20\x7f-\xff\s/%\(\)<>\{\}\[\]#]", RegexOptions.Singleline); // PDF special characters, to be encoded as #xx
private string HexMatchEvaluator(Match match)
{
byte b = Byte.Parse(match.Groups[1].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
return "" + (char)b;
}
private string SpecialMatchEvaluator(Match match)
{
byte b = (byte)match.Groups[0].Value[0];
return "#" + b.ToString("x2", CultureInfo.InvariantCulture);
}
/// <summary>
/// Initializes a new PdfName instance.
/// </summary>
/// <param name="name">The string to be parsed into a PdfName, e.g. "/AP".</param>
public PdfName(string name)
{
if (!name.StartsWith("/"))
{
throw new Exception("PdfName does not start with '/': '" + name + "'");
}
this.name = hexRegex.Replace(name.Substring(1), new MatchEvaluator(HexMatchEvaluator));
}
/// <summary>
/// Returns the string representation of this object.
/// </summary>
/// <returns>The string representation of this object.</returns>
public override string ToString()
{
return "/" + specialRegex.Replace(name, new MatchEvaluator(SpecialMatchEvaluator));
}
/// <summary>
/// Determines whether the specified object's value is equal to the current object.
/// </summary>
/// <param name="obj">The object to compare with the current object.</param>
/// <returns></returns>
public override bool Equals(object obj)
{
return obj.GetType() == typeof (PdfName) && ((PdfName)obj).name.Equals(name);
}
/// <summary>
/// Returns a hashcode that is determined by the string representation of the PdfName object.
/// </summary>
/// <returns>A hashcode for the PdfName object.</returns>
public override int GetHashCode()
{
return name.GetHashCode();
}
}
/// <summary>
/// Represents the PDF bool object. See the PDF Reference 3.2.1 Boolean Objects.
/// </summary>
[CLSCompliant(true)]
public class PdfBool: PdfObject
{
private bool val;
/// <summary>
/// Initializes a new PdfBool instance.
/// </summary>
/// <param name="truthValue">The initial value of the PdfBool object.</param>
public PdfBool(bool truthValue)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -