PdfDocumentPage: Parse content actions

This commit is contained in:
2016-06-20 01:15:22 +02:00
parent e3b1f9c2b2
commit 3b0bdf8a66
4 changed files with 77 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
using System.Collections.Generic;
namespace VAR.PdfTools
{
public class PdfContentAction
{
#region Declarations
string _token = null;
private List<IPdfElement> _parameters = null;
#endregion
#region Properties
public string Token { get { return _token; } }
public List<IPdfElement> Parameters { get { return _parameters; } }
#endregion
#region Life cycle
public PdfContentAction(string token, List<IPdfElement> parameters)
{
_token = token;
_parameters = parameters;
}
#endregion
}
}

View File

@@ -15,6 +15,8 @@ namespace VAR.PdfTools
private Dictionary<string, PdfDictionary> _fonts = new Dictionary<string, PdfDictionary>();
private List<PdfContentAction> _contentActions = null;
#endregion
#region Properties
@@ -25,6 +27,8 @@ namespace VAR.PdfTools
public Dictionary<string, PdfDictionary> Fonts { get { return _fonts; } }
public List<PdfContentAction> ContentActions { get { return _contentActions; } }
#endregion
#region Life cycle
@@ -38,8 +42,8 @@ namespace VAR.PdfTools
throw new Exception(string.Format("PdfDocumentPage: Expected dictionary of type:\"Page\". Found: {0}", type));
}
// Get content, resources and fonts
_content = _baseData.GetParamAsStream("Contents");
if (_baseData.Values.ContainsKey("Resources") == false)
{
_resources = prevDocPage._resources;
@@ -56,6 +60,16 @@ namespace VAR.PdfTools
_fonts.Add(pair.Key, pair.Value as PdfDictionary);
}
}
// Parse content
if (_content != null)
{
PdfParser parser = new PdfParser(_content);
_contentActions = parser.ParseContent();
}else
{
_contentActions = new List<PdfContentAction>();
}
}
#endregion

View File

@@ -628,6 +628,11 @@ namespace VAR.PdfTools
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();
@@ -862,6 +867,29 @@ namespace VAR.PdfTools
return streamObjects;
}
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();
PdfContentAction action = new PdfContentAction(token, elems);
elems = new List<IPdfElement>();
actions.Add(action);
}
} while (IsEndOfStream() == false);
return actions;
}
public bool IsEndOfStream()
{
return _streamPosition >= _stream.Length;

View File

@@ -40,6 +40,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PdfContentAction.cs" />
<Compile Include="PdfDocument.cs" />
<Compile Include="PdfDocumentPage.cs" />
<Compile Include="PdfElements.cs" />