mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-07-15 20:24:48 +08:00
Add test with no optional content, return IReadOnlyDictionary<>, use pdfScanner in OC constructor
This commit is contained in:
parent
4cd1f60f5b
commit
3b1a7f79ca
@ -6,6 +6,18 @@ namespace UglyToad.PdfPig.Tests.Integration
|
||||
{
|
||||
public class OptionalContentTests
|
||||
{
|
||||
[Fact]
|
||||
public void NoMarkedOptionalContent()
|
||||
{
|
||||
using (var document = PdfDocument.Open(IntegrationHelpers.GetDocumentPath("AcroFormsBasicFields.pdf")))
|
||||
{
|
||||
var page = document.GetPage(1);
|
||||
var oc = page.ExperimentalAccess.GetOptionalContents();
|
||||
|
||||
Assert.Equal(0, oc.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MarkedOptionalContent()
|
||||
{
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UglyToad.PdfPig.Tokenization.Scanner;
|
||||
using UglyToad.PdfPig.Tokens;
|
||||
|
||||
namespace UglyToad.PdfPig.Content
|
||||
@ -37,16 +38,16 @@ namespace UglyToad.PdfPig.Content
|
||||
/// </summary>
|
||||
public MarkedContentElement MarkedContent { get; }
|
||||
|
||||
internal OptionalContentGroupElement(MarkedContentElement markedContentElement)
|
||||
internal OptionalContentGroupElement(MarkedContentElement markedContentElement, IPdfTokenScanner pdfTokenScanner)
|
||||
{
|
||||
MarkedContent = markedContentElement;
|
||||
|
||||
// Type - Required
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Type, out NameToken type))
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Type, pdfTokenScanner, out NameToken type))
|
||||
{
|
||||
Type = type.Data;
|
||||
}
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Type, out StringToken typeStr))
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Type, pdfTokenScanner, out StringToken typeStr))
|
||||
{
|
||||
Type = typeStr.Data;
|
||||
}
|
||||
@ -59,11 +60,11 @@ namespace UglyToad.PdfPig.Content
|
||||
{
|
||||
case "OCG": // Optional content group dictionary
|
||||
// Name - Required
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Name, out NameToken name))
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Name, pdfTokenScanner, out NameToken name))
|
||||
{
|
||||
Name = name.Data;
|
||||
}
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Name, out StringToken nameStr))
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Name, pdfTokenScanner, out StringToken nameStr))
|
||||
{
|
||||
Name = nameStr.Data;
|
||||
}
|
||||
@ -73,15 +74,15 @@ namespace UglyToad.PdfPig.Content
|
||||
}
|
||||
|
||||
// Intent - Optional
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Intent, out NameToken intentName))
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Intent, pdfTokenScanner, out NameToken intentName))
|
||||
{
|
||||
Intent = new string[] { intentName.Data };
|
||||
}
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Intent, out StringToken intentStr))
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Intent, pdfTokenScanner, out StringToken intentStr))
|
||||
{
|
||||
Intent = new string[] { intentStr.Data };
|
||||
}
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Intent, out ArrayToken intentArray))
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Intent, pdfTokenScanner, out ArrayToken intentArray))
|
||||
{
|
||||
List<string> intentList = new List<string>();
|
||||
foreach (var token in intentArray.Data)
|
||||
@ -108,7 +109,7 @@ namespace UglyToad.PdfPig.Content
|
||||
}
|
||||
|
||||
// Usage - Optional
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Usage, out DictionaryToken usage))
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Usage, pdfTokenScanner, out DictionaryToken usage))
|
||||
{
|
||||
this.Usage = usage.Data;
|
||||
}
|
||||
@ -116,25 +117,25 @@ namespace UglyToad.PdfPig.Content
|
||||
|
||||
case "OCMD":
|
||||
// OCGs - Optional
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Ocgs, out DictionaryToken ocgsD))
|
||||
if (markedContentElement.Properties.TryGet(NameToken.Ocgs, pdfTokenScanner, out DictionaryToken ocgsD))
|
||||
{
|
||||
// dictionary or array
|
||||
throw new NotImplementedException($"{NameToken.Ocgs}");
|
||||
}
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Ocgs, out ArrayToken ocgsA))
|
||||
else if (markedContentElement.Properties.TryGet(NameToken.Ocgs, pdfTokenScanner, out ArrayToken ocgsA))
|
||||
{
|
||||
// dictionary or array
|
||||
throw new NotImplementedException($"{NameToken.Ocgs}");
|
||||
}
|
||||
|
||||
// P - Optional
|
||||
if (markedContentElement.Properties.TryGet(NameToken.P, out NameToken p))
|
||||
if (markedContentElement.Properties.TryGet(NameToken.P, pdfTokenScanner, out NameToken p))
|
||||
{
|
||||
throw new NotImplementedException($"{NameToken.P}");
|
||||
}
|
||||
|
||||
// VE - Optional
|
||||
if (markedContentElement.Properties.TryGet(NameToken.VE, out ArrayToken ve))
|
||||
if (markedContentElement.Properties.TryGet(NameToken.VE, pdfTokenScanner, out ArrayToken ve))
|
||||
{
|
||||
throw new NotImplementedException($"{NameToken.VE}");
|
||||
}
|
||||
|
@ -199,14 +199,13 @@
|
||||
/// Gets any optional content on the page.
|
||||
/// <para>Does not handle XObjects and annotations for the time being.</para>
|
||||
/// </summary>
|
||||
public IDictionary<string, IReadOnlyList<OptionalContentGroupElement>> GetOptionalContents()
|
||||
public IReadOnlyDictionary<string, IReadOnlyList<OptionalContentGroupElement>> GetOptionalContents()
|
||||
{
|
||||
List<OptionalContentGroupElement> mcesOptional = new List<OptionalContentGroupElement>();
|
||||
|
||||
// 4.10.2
|
||||
// Optional content in content stream
|
||||
var mc = page.Content?.GetMarkedContents();
|
||||
|
||||
List<OptionalContentGroupElement> mcesOptional = new List<OptionalContentGroupElement>();
|
||||
GetOptionalContentsRecursively(mc, ref mcesOptional);
|
||||
GetOptionalContentsRecursively(page.Content?.GetMarkedContents(), ref mcesOptional);
|
||||
|
||||
// Optional content in XObjects and annotations
|
||||
// TO DO
|
||||
@ -217,11 +216,16 @@
|
||||
|
||||
private void GetOptionalContentsRecursively(IReadOnlyList<MarkedContentElement> markedContentElements, ref List<OptionalContentGroupElement> mcesOptional)
|
||||
{
|
||||
if (markedContentElements.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var mce in markedContentElements)
|
||||
{
|
||||
if (mce.Tag == "OC")
|
||||
{
|
||||
mcesOptional.Add(new OptionalContentGroupElement(mce));
|
||||
mcesOptional.Add(new OptionalContentGroupElement(mce, page.pdfScanner));
|
||||
// we don't recurse
|
||||
}
|
||||
else if (mce.Children?.Count > 0)
|
||||
|
@ -1,12 +1,11 @@
|
||||
namespace UglyToad.PdfPig.Content
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Core;
|
||||
using Filters;
|
||||
using Graphics;
|
||||
using Graphics.Operations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Tokenization.Scanner;
|
||||
using XObjects;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user