public byte[] GetParamAsStream(string name) { if (Values.ContainsKey(name) == false) { return(null); } IPdfElement value = Values[name]; if (value is PdfArray) { PdfArray array = value as PdfArray; MemoryStream memStream = new MemoryStream(); foreach (IPdfElement elem in array.Values) { PdfStream stream = elem as PdfStream; if (stream == null) { continue; } memStream.Write(stream.Data, 0, stream.Data.Length); } if (memStream.Length > 0) { return(memStream.ToArray()); } return(null); } if (value is PdfStream) { return(((PdfStream)value).Data); } return(null); }
private PdfArray ParseArray() { if (PeekChar() != '[') { return(null); } NextChar(); SkipWhitespace(); PdfArray array = new PdfArray(); do { byte character = PeekChar(); if (character == ']') { NextChar(); break; } else { IPdfElement obj = ParseElement(); if (obj == null) { break; //throw new Exception(string.Format("ParseArray: Error parsing item at: {0}", _streamPosition)); } array.Values.Add(obj); } SkipWhitespace(); } while (IsEndOfStream() == false); return(array); }
private PdfDictionary ParseDictionary() { if (PeekChar() != '<' || PeekNextChar() != '<') { return(null); } NextChar(); NextChar(); SkipWhitespace(); PdfName previousName = null; PdfDictionary dict = new PdfDictionary(); do { byte character = PeekChar(); byte nextCharacter = PeekNextChar(); if (character == '>' && nextCharacter == '>') { NextChar(); NextChar(); break; } else if (character == '/') { PdfName name = ParseName(); previousName = name; SkipWhitespace(); IPdfElement obj = ParseElement(); if (obj is PdfNull) { dict.Values.Remove(name.Value); } else { previousName = name; if (dict.Values.ContainsKey(name.Value)) { dict.Values[name.Value] = obj; } else { dict.Values.Add(name.Value, obj); } } SkipWhitespace(); } else { throw new Exception(string.Format("Error parsing Dictionary at: {0}", _streamPosition)); } } while (IsEndOfStream() == false); return(dict); }
public void CreatePage(bool noQueueClear = false) { double maxHeight = 0, lastx = _margins.X; foreach (IPdfElement item in _queue) { item.CalculateSize(_gfx, lastx, _margins); if (item.IsFixed) { item.Render(_page, _gfx, null); } else { int PreviousIndex = _queue.IndexOf(item) - 1; if (PreviousIndex > -1) { IPdfElement prevItem = _queue.ElementAt(PreviousIndex); lastx = prevItem.X + prevItem.Width + 1; if (lastx + item.Width < _page.Width - _margins.X) // W lastx już mamy margines, więc liczymy dodatkowo tylko prawy. { item.Render(_page, _gfx, new XPoint(lastx, prevItem.Y)); if (item.Height + 1 > maxHeight) { maxHeight = item.Height + 1; } } else { lastx = _margins.X; item.Render(_page, _gfx, new XPoint(lastx, prevItem.Y + maxHeight)); maxHeight = item.Height + 1; } } else { item.Render(_page, _gfx, _margins); maxHeight = item.Height + 1; } } } if (!noQueueClear) { _queue.Clear(); } }
public static long GetInt(IPdfElement elem, long defaultValue) { if (elem == null) { return(defaultValue); } if (elem is PdfInteger) { return(((PdfInteger)elem).Value); } if (elem is PdfReal) { return((long)((PdfReal)elem).Value); } return(defaultValue); }
public static string GetString(IPdfElement elem, string defaultValue) { if (elem == null) { return(defaultValue); } if (elem is PdfString) { return(((PdfString)elem).Value); } if (elem is PdfName) { return(((PdfName)elem).Value); } return(defaultValue); }
public static double GetReal(IPdfElement elem, double defaultValue) { if (elem == null) { return(defaultValue); } if (elem is PdfInteger) { return(((PdfInteger)elem).Value); } if (elem is PdfReal) { return(((PdfReal)elem).Value); } return(defaultValue); }
public long?GetParamAsInt(string name) { if (Values.ContainsKey(name) == false) { return(null); } IPdfElement value = Values[name]; if (value is PdfArray) { value = ((PdfArray)value).Values[0]; } if (value is PdfInteger) { return(((PdfInteger)value).Value); } return(null); }
public List <PdfContentAction> ParseContent() { List <PdfContentAction> actions = new List <PdfContentAction>(); List <IPdfElement> elems = new List <IPdfElement>(); do { SkipWhitespace(); IPdfElement elem = ParseElement(); if (elem != null) { elems.Add(elem); } else { string token = ParseToken(); if (string.IsNullOrEmpty(token)) { break; } PdfContentAction action = new PdfContentAction(token, elems); elems = new List <IPdfElement>(); actions.Add(action); if (action.Token == "ID") { // Embbed inline image byte lineFeed = 0x0A; byte carriageReturn = 0x0D; long distToObject = MeasureToMarkers(new char[][] { new char[] { (char)lineFeed, 'E', 'I' }, new char[] { (char)carriageReturn, (char)lineFeed, 'E', 'I' }, }); byte[] imageBody = GetRawData(distToObject); SkipEndOfLine(); string endToken = ParseToken(); action.Parameters.Add(new PdfStream { OriginalData = imageBody, }); } } } while (IsEndOfStream() == false); return(actions); }
private IPdfElement ParseNumberOrReference() { IPdfElement obj = ParseNumber(); PdfInteger number = obj as PdfInteger; if (number == null) { return(obj); } // Try to get an indirect object reference long streamPosition = _streamPosition; SkipWhitespace(); if (char.IsDigit((char)PeekChar()) == false) { _streamPosition = streamPosition; return(obj); } IPdfElement objectGeneration = ParseNumber(); if ((objectGeneration is PdfInteger) == false) { _streamPosition = streamPosition; return(obj); } SkipWhitespace(); if (PeekChar() != 'R') { _streamPosition = streamPosition; return(obj); } NextChar(); PdfObjectReference objRef = new PdfObjectReference(); objRef.ObjectID = (int)number.Value; objRef.ObjectGeneration = (int)((PdfInteger)objectGeneration).Value; return(objRef); }
public List <PdfObject> ParseObjectStream(int number, long first) { var streamObjects = new List <PdfObject>(); var objectIds = new List <long>(); for (int i = 0; i < number; i++) { SkipWhitespace(); IPdfElement objectId = ParseElement(); if (objectId is PdfInteger) { objectIds.Add(((PdfInteger)objectId).Value); } else { throw new System.Exception(string.Format("Unexpected element parsing ObjectStream at: {0}", _streamPosition)); } SkipWhitespace(); ParseElement(); } _streamPosition = (int)first; for (int i = 0; i < number; i++) { SkipWhitespace(); IPdfElement elem = ParseElement(); if (elem == null) { throw new System.Exception(string.Format("Unexpected error parsing ObjectStream at: {0}", _streamPosition)); } PdfObject objAux = new PdfObject(); objAux.ObjectGeneration = 0; objAux.ObjectID = (int)objectIds[i]; objAux.Data = elem; streamObjects.Add(objAux); } return(streamObjects); }
public string GetParamAsString(string name) { if (Values.ContainsKey(name) == false) { return(null); } IPdfElement value = Values[name]; if (value is PdfArray) { value = ((PdfArray)value).Values[0]; } if (value is PdfName) { return(((PdfName)value).Value); } if (value is PdfString) { return(((PdfString)value).Value); } return(null); }
private IPdfElement ParseElement() { IPdfElement obj = null; byte character = PeekChar(); byte nextCharacter = PeekNextChar(); if (character == 't' || character == 'f') { obj = ParseBoolean(); } if (character == 'n') { obj = ParseNull(); } else if (IsDigit(character) || character == '+' || character == '-' || character == '.') { obj = ParseNumberOrReference(); } else if (character == '(' || (character == '<' && nextCharacter != '<')) { obj = ParseString(); } else if (character == '/') { obj = ParseName(); } else if (character == '[') { obj = ParseArray(); } else if (character == '<' && nextCharacter == '<') { obj = ParseDictionary(); } return(obj); }
public PdfPanel(IPdfElement element) : this() { Content.Add(element); }
public void Add(IPdfElement element) => _wholeQueue.Add(element);
public void Add(IPdfElement element) => _queue.Add(element);
public Dictionary <char, string> ParseToUnicode() { var toUnicode = new Dictionary <char, string>(); long skip = MeasureToMarkers(new char[][] { new char[] { 'b', 'e', 'g', 'i', 'n', 'c', 'm', 'a', 'p' }, }); _streamPosition = skip; var stack = new List <IPdfElement>(); do { SkipWhitespace(); IPdfElement elem = ParseElement(); if (elem != null) { stack.Add(elem); } else { string token = ParseToken(); if (token == "begincodespacerange") { PdfInteger numCodespaces = stack.Last() as PdfInteger; if (numCodespaces == null) { throw new Exception(string.Format("ParseToUnicode: \"begincodespacerange\" found without preceding count at: {0}", _streamPosition)); } for (int i = 0; i < numCodespaces.Value; i++) { // Skip CodeSpaceRanges SkipWhitespace(); PdfString strStart = ParseString(); SkipWhitespace(); PdfString strEnd = ParseString(); } SkipWhitespace(); string endToken = ParseToken(); if (endToken != "endcodespacerange") { throw new Exception(string.Format("ParseToUnicode: Expected \"endcodespacerange\", found \"{0}\", at: {1}", endToken, _streamPosition)); } } else if (token == "beginbfrange") { PdfInteger numRanges = stack.Last() as PdfInteger; if (numRanges == null) { throw new Exception(string.Format("ParseToUnicode: \"beginbfrange\" found without preceding count at: {0}", _streamPosition)); } for (int i = 0; i < numRanges.Value; i++) { SkipWhitespace(); PdfString pdfStrStart = ParseString(); SkipWhitespace(); PdfString pdfStrEnd = ParseString(); SkipWhitespace(); IPdfElement pdfElemDest = ParseElement(); char chStart = ReencodeStringToUTF16BE(pdfStrStart.Value)[0]; char chEnd = ReencodeStringToUTF16BE(pdfStrEnd.Value)[0]; if (chStart == chEnd && pdfElemDest is PdfString) { string strDst = ReencodeStringToUTF16BE(((PdfString)pdfElemDest).Value); toUnicode.Add(chStart, strDst); continue; } if (chEnd > chStart && pdfElemDest is PdfString) { string strDst = ReencodeStringToUTF16BE(((PdfString)pdfElemDest).Value); char[] chsDest = strDst.ToArray(); for (char c = chStart; c <= chEnd; c++) { toUnicode.Add(c, new string(chsDest)); chsDest[chsDest.Length - 1]++; } continue; } if (chEnd > chStart && pdfElemDest is PdfArray) { PdfArray array = pdfElemDest as PdfArray; int length = chEnd - chStart; for (int j = 0; j <= length; j++) { char c = (char)(chStart + j); string strDst = ReencodeStringToUTF16BE(((PdfString)array.Values[j]).Value); toUnicode.Add(c, strDst); } continue; } } SkipWhitespace(); string endToken = ParseToken(); if (endToken != "endbfrange") { throw new Exception(string.Format("ParseToUnicode: Expected \"endbfrange\", found \"{0}\", at: {1}", endToken, _streamPosition)); } } else if (token == "beginbfchar") { PdfInteger numChars = stack.Last() as PdfInteger; if (numChars == null) { throw new Exception(string.Format("ParseToUnicode: \"beginbfchar\" found without preceding count at: {0}", _streamPosition)); } for (int i = 0; i < numChars.Value; i++) { SkipWhitespace(); PdfString pdfStrOrig = ParseString(); SkipWhitespace(); PdfString pdfStrDest = ParseString(); char chOrig = ReencodeStringToUTF16BE(pdfStrOrig.Value)[0]; string strDst = ReencodeStringToUTF16BE(((PdfString)pdfStrDest).Value); toUnicode.Add(chOrig, strDst); } SkipWhitespace(); string endToken = ParseToken(); if (endToken != "endbfchar") { throw new Exception(string.Format("ParseToUnicode: Expected \"endbfchar\", found \"{0}\", at: {1}", endToken, _streamPosition)); } } else { // Ignore rest of tokens } } } while (IsEndOfStream() == false); return(toUnicode); }
public PdfObject ParseObject(List <PdfObject> knownObjects) { PdfObject obj = null; long startPosition = _streamPosition; do { SkipWhitespace(); byte character = PeekChar(); if (character == '%') { SkipComment(); } else if (IsDigit(character)) { IPdfElement objectID = ParseNumber(); SkipWhitespace(); IPdfElement objectGeneration = ParseNumber(); SkipWhitespace(); string token = ParseToken(); if (token == "obj") { SkipWhitespace(); IPdfElement element = ParseElement(); string endToken = ParseToken(); // Intercept streams if (endToken == "stream") { PdfDictionary streamDict = element as PdfDictionary; if (streamDict == null) { throw new Exception(string.Format("Stream after a not dictionary element at: {0}", _streamPosition)); } SkipEndOfLine(); // Find the length of the stream long length = -1; if (streamDict.Values.ContainsKey("Length")) { length = PdfElementUtils.GetInt(streamDict.Values["Length"], -1); if (length == -1 && streamDict.Values["Length"] is PdfObjectReference) { IPdfElement lenghtObj = SearchObjectID(knownObjects, ((PdfObjectReference)streamDict.Values["Length"]).ObjectID); length = PdfElementUtils.GetInt(lenghtObj, -1); } } if (length == -1) { byte lineFeed = 0x0A; byte carriageReturn = 0x0D; length = MeasureToMarkers(new char[][] { new char[] { (char)carriageReturn, (char)lineFeed, 'e', 'n', 'd', 's', 't', 'r', 'e', 'a', 'm' }, new char[] { (char)lineFeed, 'e', 'n', 'd', 's', 't', 'r', 'e', 'a', 'm' }, new char[] { 'e', 'n', 'd', 's', 't', 'r', 'e', 'a', 'm', (char)lineFeed }, new char[] { 'e', 'n', 'd', 's', 't', 'r', 'e', 'a', 'm', (char)carriageReturn, (char)lineFeed }, }); } // Get the stream byte[] streamBody = GetRawData(length); SkipEndOfLine(); endToken = ParseToken(); if (endToken != "endstream") { throw new Exception(string.Format("Expected \"endstream\" token, \"{0}\" found at: {1}", token, _streamPosition)); } SkipWhitespace(); endToken = ParseToken(); PdfStream stream = new PdfStream(); stream.Dictionary = streamDict; stream.Data = streamBody; element = stream; } if (endToken == "endobj") { obj = new PdfObject(); obj.ObjectID = (int)((PdfInteger)objectID).Value; obj.ObjectGeneration = (int)((PdfInteger)objectGeneration).Value; obj.Data = element; break; } } } else { long streamPosition = _streamPosition; string token = ParseToken(); if (token == "startxref") { // TODO: PdfParser: Ignoring startxref for now SkipEndOfLine(); SkipToEndOfLine(); SkipEndOfLine(); SkipToEndOfLine(); SkipEndOfLine(); SkipWhitespace(); continue; } if (token == "xref") { // TODO: PdfParser: Ignoring xref for now SkipToEndOfLine(); SkipEndOfLine(); do { SkipWhitespace(); IPdfElement objNumber = ParseNumber(); SkipWhitespace(); objNumber = ParseNumber(); SkipEndOfLine(); PdfInteger refNumber = objNumber as PdfInteger; for (int i = 0; i < refNumber.Value; i++) { SkipToEndOfLine(); SkipEndOfLine(); } long currentPosition = _streamPosition; IPdfElement testElem = ParseElement(); _streamPosition = currentPosition; if ((testElem is PdfInteger) == false) { break; } } while (IsEndOfStream() == false); continue; } if (token == "trailer") { // TODO: PdfParser: Ignoring trailer for now SkipEndOfLine(); ParseElement(); SkipWhitespace(); SkipToEndOfLine(); SkipEndOfLine(); SkipToEndOfLine(); SkipEndOfLine(); SkipToEndOfLine(); SkipEndOfLine(); SkipWhitespace(); continue; } // Try to find an object marker byte lineFeed = 0x0A; byte carriageReturn = 0x0D; long distToObject = MeasureToMarkers(new char[][] { new char[] { ' ', 'o', 'b', 'j', (char)lineFeed }, new char[] { ' ', 'o', 'b', 'j', (char)carriageReturn, (char)lineFeed }, }); if (distToObject > 0) { // Object marker found, backtrack and retry long originalPosition = _streamPosition; _streamPosition += distToObject; long marker = _streamPosition; SkipWhitespaceBack(); if (_streamPosition == marker) { // Abort backtrack, skip garbage _streamPosition = originalPosition + distToObject + 4; continue; } marker = _streamPosition; SkipDigitsBack(); if (_streamPosition == marker) { // Abort backtrack, skip garbage _streamPosition = originalPosition + distToObject + 4; continue; } marker = _streamPosition; SkipWhitespaceBack(); if (_streamPosition == marker) { // Abort backtrack, skip garbage _streamPosition = originalPosition + distToObject + 4; continue; } marker = _streamPosition; SkipDigitsBack(); if (_streamPosition == marker) { // Abort backtrack, skip garbage _streamPosition = originalPosition + distToObject + 4; continue; } NextChar(); } else { // No more obj markers found, abort all. _streamPosition = _stream.Length; } } } while (IsEndOfStream() == false); return(obj); }