mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-07-16 06:32:06 +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
|
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]
|
[Fact]
|
||||||
public void MarkedOptionalContent()
|
public void MarkedOptionalContent()
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using UglyToad.PdfPig.Tokenization.Scanner;
|
||||||
using UglyToad.PdfPig.Tokens;
|
using UglyToad.PdfPig.Tokens;
|
||||||
|
|
||||||
namespace UglyToad.PdfPig.Content
|
namespace UglyToad.PdfPig.Content
|
||||||
@ -37,16 +38,16 @@ namespace UglyToad.PdfPig.Content
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public MarkedContentElement MarkedContent { get; }
|
public MarkedContentElement MarkedContent { get; }
|
||||||
|
|
||||||
internal OptionalContentGroupElement(MarkedContentElement markedContentElement)
|
internal OptionalContentGroupElement(MarkedContentElement markedContentElement, IPdfTokenScanner pdfTokenScanner)
|
||||||
{
|
{
|
||||||
MarkedContent = markedContentElement;
|
MarkedContent = markedContentElement;
|
||||||
|
|
||||||
// Type - Required
|
// Type - Required
|
||||||
if (markedContentElement.Properties.TryGet(NameToken.Type, out NameToken type))
|
if (markedContentElement.Properties.TryGet(NameToken.Type, pdfTokenScanner, out NameToken type))
|
||||||
{
|
{
|
||||||
Type = type.Data;
|
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;
|
Type = typeStr.Data;
|
||||||
}
|
}
|
||||||
@ -59,11 +60,11 @@ namespace UglyToad.PdfPig.Content
|
|||||||
{
|
{
|
||||||
case "OCG": // Optional content group dictionary
|
case "OCG": // Optional content group dictionary
|
||||||
// Name - Required
|
// Name - Required
|
||||||
if (markedContentElement.Properties.TryGet(NameToken.Name, out NameToken name))
|
if (markedContentElement.Properties.TryGet(NameToken.Name, pdfTokenScanner, out NameToken name))
|
||||||
{
|
{
|
||||||
Name = name.Data;
|
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;
|
Name = nameStr.Data;
|
||||||
}
|
}
|
||||||
@ -73,15 +74,15 @@ namespace UglyToad.PdfPig.Content
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Intent - Optional
|
// 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 };
|
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 };
|
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>();
|
List<string> intentList = new List<string>();
|
||||||
foreach (var token in intentArray.Data)
|
foreach (var token in intentArray.Data)
|
||||||
@ -108,7 +109,7 @@ namespace UglyToad.PdfPig.Content
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Usage - Optional
|
// 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;
|
this.Usage = usage.Data;
|
||||||
}
|
}
|
||||||
@ -116,25 +117,25 @@ namespace UglyToad.PdfPig.Content
|
|||||||
|
|
||||||
case "OCMD":
|
case "OCMD":
|
||||||
// OCGs - Optional
|
// OCGs - Optional
|
||||||
if (markedContentElement.Properties.TryGet(NameToken.Ocgs, out DictionaryToken ocgsD))
|
if (markedContentElement.Properties.TryGet(NameToken.Ocgs, pdfTokenScanner, out DictionaryToken ocgsD))
|
||||||
{
|
{
|
||||||
// dictionary or array
|
// dictionary or array
|
||||||
throw new NotImplementedException($"{NameToken.Ocgs}");
|
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
|
// dictionary or array
|
||||||
throw new NotImplementedException($"{NameToken.Ocgs}");
|
throw new NotImplementedException($"{NameToken.Ocgs}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// P - Optional
|
// 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}");
|
throw new NotImplementedException($"{NameToken.P}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// VE - Optional
|
// 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}");
|
throw new NotImplementedException($"{NameToken.VE}");
|
||||||
}
|
}
|
||||||
|
@ -199,14 +199,13 @@
|
|||||||
/// Gets any optional content on the page.
|
/// Gets any optional content on the page.
|
||||||
/// <para>Does not handle XObjects and annotations for the time being.</para>
|
/// <para>Does not handle XObjects and annotations for the time being.</para>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IDictionary<string, IReadOnlyList<OptionalContentGroupElement>> GetOptionalContents()
|
public IReadOnlyDictionary<string, IReadOnlyList<OptionalContentGroupElement>> GetOptionalContents()
|
||||||
{
|
{
|
||||||
|
List<OptionalContentGroupElement> mcesOptional = new List<OptionalContentGroupElement>();
|
||||||
|
|
||||||
// 4.10.2
|
// 4.10.2
|
||||||
// Optional content in content stream
|
// Optional content in content stream
|
||||||
var mc = page.Content?.GetMarkedContents();
|
GetOptionalContentsRecursively(page.Content?.GetMarkedContents(), ref mcesOptional);
|
||||||
|
|
||||||
List<OptionalContentGroupElement> mcesOptional = new List<OptionalContentGroupElement>();
|
|
||||||
GetOptionalContentsRecursively(mc, ref mcesOptional);
|
|
||||||
|
|
||||||
// Optional content in XObjects and annotations
|
// Optional content in XObjects and annotations
|
||||||
// TO DO
|
// TO DO
|
||||||
@ -217,11 +216,16 @@
|
|||||||
|
|
||||||
private void GetOptionalContentsRecursively(IReadOnlyList<MarkedContentElement> markedContentElements, ref List<OptionalContentGroupElement> mcesOptional)
|
private void GetOptionalContentsRecursively(IReadOnlyList<MarkedContentElement> markedContentElements, ref List<OptionalContentGroupElement> mcesOptional)
|
||||||
{
|
{
|
||||||
|
if (markedContentElements.Count == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var mce in markedContentElements)
|
foreach (var mce in markedContentElements)
|
||||||
{
|
{
|
||||||
if (mce.Tag == "OC")
|
if (mce.Tag == "OC")
|
||||||
{
|
{
|
||||||
mcesOptional.Add(new OptionalContentGroupElement(mce));
|
mcesOptional.Add(new OptionalContentGroupElement(mce, page.pdfScanner));
|
||||||
// we don't recurse
|
// we don't recurse
|
||||||
}
|
}
|
||||||
else if (mce.Children?.Count > 0)
|
else if (mce.Children?.Count > 0)
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
namespace UglyToad.PdfPig.Content
|
namespace UglyToad.PdfPig.Content
|
||||||
{
|
{
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Core;
|
using Core;
|
||||||
using Filters;
|
using Filters;
|
||||||
using Graphics;
|
using Graphics;
|
||||||
using Graphics.Operations;
|
using Graphics.Operations;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using Tokenization.Scanner;
|
using Tokenization.Scanner;
|
||||||
using XObjects;
|
using XObjects;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user