-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "^"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> *@ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |