Skip to content

Commit

Permalink
Add Tag inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
aMytho committed Feb 4, 2024
1 parent b1d92cf commit 05ecb2b
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
134 changes: 134 additions & 0 deletions Pages/Inspector/Nodes/TagNode.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
@using iText.Kernel.Pdf.Tagutils;
@using Pdf_Acc_Toolset.Services.Util;
@using iText.Kernel.Pdf.Tagging;
@using iText.Kernel.Pdf;
@using iText.Layout;

<div class="p-2 border border-l-2 border-y-0 border-r-0 border-solid border-gray-500"
@oncontextmenu:stopPropagation="true" @oncontextmenu:preventDefault="true" @oncontextmenu="ShowTagInfo">
<div class="flex flex-row p-1">
<button @onclick:stopPropagation="true" @onclick:preventDefault="true" @onclick="toggleKids"
class="cursor-pointer border border-solid border-gray-900 p-1">@ShowHideBtn()</button>
<p class="ml-2">@GetTagType() @GetTitle()</p>
</div>

<div class="ml-1 w-min">
@foreach (TagTreePointer tag in GetChildren())
{
<div class=" m-2">
@if (showKids) {
<TagNode node="tag" />
}
</div>
}
</div>

</div>


@code {
[Parameter]
public TagTreePointer node { get; set; }

private bool showKids = true;

private string GetTagType()
{
return TagUtil.CombineTagName(node.GetRole());
}

private bool HasChildTag()
{
return node.GetKidsRoles().Count != 0;
}

private IList<TagTreePointer> GetChildren()
{
// List which will be returned with the tags
List<TagTreePointer> matchingTags = new();

// Start looking
CheckChildElement(node);

// Recursive function to check all tags in the tag tree
void CheckChildElement(TagTreePointer children)
{
// Create a copy of the current tag
PdfStructElem currentTag = children.GetContext().GetPointerStructElem(children);

// Get each child
IList<IStructureNode> kids = currentTag.GetKids();

// Check each child
if (kids != null && kids.Count > 0)
{
for (int i = 0; i < kids.Count; i++)
{
// If the kid is null or content, skip it
if (kids[i] == null || kids[i].GetType() != typeof(PdfStructElem))
{
continue;
}

// Store a copy to check children
PdfStructElem elem = children.GetContext().GetPointerStructElem(children);
TagTreePointer childPointer = children.GetContext().CreatePointerForStructElem(elem);
matchingTags.Add(childPointer.MoveToKid(0));
}
}
}

// Return result
return matchingTags;
}

private void toggleKids()
{
Console.WriteLine("Swapping to " + !this.showKids);
this.showKids = !this.showKids;
}

private string GetTitle()
{
AccessibilityProperties properties = node.GetProperties();
PdfObject titleObject = node.GetContext().GetPointerStructElem(node).GetPdfObject().Get(PdfName.T);
if (titleObject is not null) {
return titleObject.ToString();
}
return "";
}

private void ShowTagInfo()
{
AccessibilityProperties properties = node.GetProperties();
string actualText = properties.GetActualText();
string altText = properties.GetAlternateDescription();
string message = "";
string title = GetTitle();

if (title.Length > 0) {
message += $"Title: {GetTitle()} <br>";
}
if (actualText is not null) {
message += $"Actual Text: {actualText} <br>";
}
if (altText is not null) {
message += $"Alt Text: {altText} <br>";
}

if (message.Length != 0) {
NotificationUtil.Inform(NotificationType.Info, message);
} else {
NotificationUtil.Inform(NotificationType.Warning, "No properties were found.");
}
}

private string ShowHideBtn()
{
if (showKids) {
return "v";
} else {
return "^";
}
}
}
19 changes: 19 additions & 0 deletions Pages/Inspector/TagInspector.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

<div class="top relative flex bg-slate-700 rounded-b-md p-2">
<button class="ml-auto" @onclick="CloseInspector">Close</button>
</div>

@if (!pdfVisible) {
<p class="ml-2 mt-2 text-lg text-red-500">
You must upload a PDF before you can view the current tag tree.
</p>
}

@if (pdfVisible) {
<div class="p-4">
<Pdf_Acc_Toolset.Pages.Inspector.Nodes.TagNode node="GetTagTree()" />
</div>
@* <div class="sticky bottom-0 ml-auto w-1/3 h-1/5 bg-slate-700">

</div> *@
}
41 changes: 41 additions & 0 deletions Pages/Inspector/TagInspector.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

using Microsoft.AspNetCore.Components;
using Pdf_Acc_Toolset.Services;
using Pdf_Acc_Toolset.Services.UI;


namespace Pdf_Acc_Toolset.Pages.Inspector;

public partial class TagInspector
{
[Parameter]
public EventCallback<int> OnCloseInspector { get; set; }

private bool pdfVisible = false;

protected override void OnInitialized()
{
TagInspectorService.PdfReady += OnPdfReady;

if (PdfManager.outFile != null) {
pdfVisible = true;
}
base.OnInitialized();
}

private void OnPdfReady()
{
Console.WriteLine("idk");
this.pdfVisible = true;
}

private void CloseInspector()
{
this.OnCloseInspector.InvokeAsync();
}

private iText.Kernel.Pdf.Tagutils.TagTreePointer GetTagTree()
{
return PdfManager.GetTagRoot();
}
}
3 changes: 3 additions & 0 deletions Services/PdfManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using iText.Kernel.Pdf.Tagutils;
using iText.Layout;
using Pdf_Acc_Toolset.Services.Pdf;
using Pdf_Acc_Toolset.Services.UI;
using Pdf_Acc_Toolset.Services.Util;

namespace Pdf_Acc_Toolset.Services;
Expand Down Expand Up @@ -134,6 +135,8 @@ public static void SetOutputFile(Stream input, PdfImportConfig conf)
// Allow it to be downloaded
pdfDownloadable = true;
hasDownloaded = false;
TagInspectorService.NotifyPdfReady();
Console.WriteLine("123");
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions Services/UI/TagInspectorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace Pdf_Acc_Toolset.Services.UI;

public class TagInspectorService
{
public static event Action PdfReady;

public static void NotifyPdfReady()
{
PdfReady?.Invoke();
}
}

0 comments on commit 05ecb2b

Please sign in to comment.