Files
PDFPatcher/App/Processor/ContentProcessors/RemoveAnnotationProcessor.cs
2024-10-07 16:17:27 +08:00

64 lines
1.5 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text.pdf;
namespace PDFPatcher.Processor
{
sealed class RemoveAnnotationProcessor : IPageProcessor
{
readonly PdfName _AnnotationType;
int _processedPageCount;
public RemoveAnnotationProcessor(PdfName annotationType) {
_AnnotationType = annotationType;
}
#region IPageProcessor 成员
public string Name => "删除批注";
public void BeginProcess(DocProcessorContext context) {
_processedPageCount = 0;
}
public bool EndProcess(PdfReader pdf) {
Tracker.TraceMessage(Tracker.Category.Notice, Name + "功能:");
Tracker.TraceMessage("  删除了 " + _processedPageCount + " 页的批注。");
return false;
}
public int EstimateWorkload(PdfReader pdf) {
return pdf.NumberOfPages;
}
public bool Process(PageProcessorContext context) {
Tracker.IncrementProgress(1);
var anns = context.Page.GetAsArray(PdfName.ANNOTS);
if (anns == null) {
return false;
}
if (_AnnotationType == null) {
context.Page.Remove(PdfName.ANNOTS);
return true;
}
bool removed = false;
var l = anns.Size;
for (int i = l - 1; i >= 0; i--) {
if (PdfReader.GetPdfObject(anns[i]) is not PdfDictionary ann) {
continue;
}
if (_AnnotationType.Equals(ann.GetAsName(PdfName.SUBTYPE)) == false) {
continue;
}
anns.Remove(i);
removed = true;
}
if (anns.Size == 0) {
context.Page.Remove(PdfName.ANNOTS);
}
if (removed) {
_processedPageCount++;
}
return removed;
}
#endregion
}
}