PdfiumViewer组件扩展(Pdfium.Net.Free)--注解
项目地址:
Pdfium.Net:https://github.com/1000374/Pdfium.Net
PdfiumViewer:https://github.com/1000374/PdfiumViewer
pdf的注解分很多类型,请查看以下枚举值:
public enum FpdfAnnotSubtypes
{
//Unknown annotation.
UNKNOWN = 0,
//Text annotation type.
TEXT = 1,
//Link annotation type.
LINK = 2,
//Free text annotation type.
FREETEXT = 3,
//Line annotation type.
LINE = 4,
//Square annotatotion type.
SQUARE = 5,
//Circle annotation type.
CIRCLE = 6,
//Polygon annotation type.
POLYGON = 7,
//Plyline annotation type.
POLYLINE = 8,
//Highlight annotation type.
HIGHLIGHT = 9,
//Underline annotation type.
UNDERLINE = 10,
//Squiggle annotation type.
SQUIGGLY = 11,
//Strikeout annotation type.
STRIKEOUT = 12,
//Rubber stamp annotation type.
STAMP = 13,
//Caret annotation type.
CARET = 14,
//Ink annotation type.
INK = 15,
//Popup annotation type.
POPUP = 16,
//File attachment annotation type.
FILEATTACHMENT = 17,
//Sound annotation type.
SOUND = 18,
//Movie annotation type.
MOVIE = 19,
//Widget annotation type.
WIDGET = 20,
//Screen annotation type.
SCREEN = 21,
//Printer mark annotation type.
PRINTERMARK = 22,
//Trap network annotation type.
TRAPNET = 23,
//Watermark annotation type.
WATERMARK = 24,
THREED = 25,
//Rich media annotation type
RICHMEDIA = 26,
XFAWIDGET = 27,
REDACT = 28
}
可通过封装的方法获取常用的注解:
var pathPdf = "./Pdfium.NetTests/resources/annotation_highlight_long_content.pdf";
using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
{
var page0 = doc.Pages[0];
var links = page0.GetPageLinks();
}
也可通过以下方法自行遍历获取,此代码为示例代码、在pdf预览中很多交互未实现或者合并实现,比如填充(WIDGET)pdf等交互,感兴趣的 可以自行研究
var pathPdf = "./Pdfium.NetTests/resources/annotation_highlight_long_content.pdf";
using (var doc = PdfDocument.Load(new MemoryStream(File.ReadAllBytes(pathPdf))))
{
var page0 = doc.Pages[0];
var pageText0 = page0.PageText;
var form = doc.PdfForm;
var annotCount = page0.PageGetAnnotCount();
for (int i = 0; i < annotCount; i++)
{
var annot = page0.PageGetAnnot(i);
var subType = annot.Subtype;
var rect = annot.Rect;
//无用
var flag = annot.Flags;
var boo = annot.IsSupportedSubtype(subType);
switch (subType)
{
case FpdfAnnotSubtypes.HIGHLIGHT:
{
var higthLight = new AnnotData();
var keys = new string[] { FpdfAnnotationKeys.kAuthorKey, FpdfAnnotationKeys.kContents };
foreach (var key in keys)
{
var types = annot.GetValueType(key);
switch (types)
{
case FpdfObjectTypes.FPDF_OBJECT_STRING:
if (key.Equals(FpdfAnnotationKeys.kAuthorKey))
higthLight.Author = annot.GetStringValue(key);
else
higthLight.Content = annot.GetStringValue(key);
break;
case FpdfObjectTypes.FPDF_OBJECT_NUMBER:
if (annot.GetNumberValue(key, out float values))
{
if (key.Equals(FpdfAnnotationKeys.kAuthorKey))
higthLight.Author = values.ToString();
else
higthLight.Content = values.ToString();
}
break;
}
}
if (annot.GetColor(FpdfAnnotColorType.Color, out uint r, out uint g, out uint b, out uint a))
higthLight.BackColor = Color.FromArgb((int)a, (int)r, (int)g, (int)b);
var attcount = annot.AttachmentPointsCount;
for (int j = 0; j < (int)attcount; j++)
{
var quad_points = new FS_QUADPOINTSF();
if (annot.GetAttachmentPoints((ulong)j, quad_points))
{
}
}
var index = pageText0.GetCharIndexAtPos(rect.Left, rect.Top, Math.Abs(rect.Right - rect.Left), Math.Abs(rect.Top - rect.Bottom));
var fontName = pageText0.GetFontInfo(index, out FpdfFxfontFlags flags);
var fontSize = pageText0.GetFontSize(index);
higthLight.FontName = fontName;
higthLight.FontSize = fontSize;
}
break;
case FpdfAnnotSubtypes.LINK:
{
var flink = annot.GetLink();
var action = flink.GetAction();
var acType = action.GetActionType();
switch (acType)
{
case FpdfActionTypes.UNSUPPORTED:
{
var dest = doc.LinkGetDest(flink);
var targetPage = doc.DestGetDestPageIndex(dest);
var uri = doc.ActionGetURIPath(action);
}
break;
case FpdfActionTypes.GOTO:
{
var dest = doc.ActionGetDest(action);
var targetPage = doc.DestGetDestPageIndex(dest);
var uri = doc.ActionGetURIPath(action);
}
break;
case FpdfActionTypes.REMOTEGOTO:
break;
case FpdfActionTypes.URI:
{
var uri = doc.ActionGetURIPath(action);
}
break;
case FpdfActionTypes.LAUNCH:
{
var filePath = action.GetFilePath();
// new RectangleF(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top)
}
break;
case FpdfActionTypes.EMBEDDEDGOTO:
{
var dest = doc.ActionGetDest(action);
var view = dest.GetDestView(out uint paraNum, out float paras);
var filePath = action.GetFilePath();
}
break;
}
}
break;
case FpdfAnnotSubtypes.WIDGET:
{
var formflag = form.AnnotGetFormFieldFlags(annot);
var formName = form.AnnotGetFormFieldName(annot);
var formValue = form.AnnotGetFormFieldValue(annot);
var fontsize = form.AnnotGetFontSize(annot);
var optionCount = form.AnnotGetOptionCount(annot);
for (int ni = 0; ni < optionCount; ni++)
{
var lable = form.AnnotGetOptionLabel(annot, ni);
var iselect = form.AnnotIsOptionSelected(annot, ni);
}
}
break;
case FpdfAnnotSubtypes.POPUP:
{
// Attempting to retrieve |annot|'s "IRT"-linked annotation would fail,
// since "IRT" is not a key in |annot|'s dictionary.
var kIRTKey = "IRT";
if (annot.HasKey(kIRTKey))
{
var linked = annot.GetLinkedAnnot(kIRTKey);
}
if (annot.HasKey(FpdfAnnotationKeys.kP))
{
var types = annot.GetValueType(FpdfAnnotationKeys.kP);
switch (types)
{
case FpdfObjectTypes.FPDF_OBJECT_STRING:
var strValue = annot.GetStringValue(FpdfAnnotationKeys.kP);
break;
case FpdfObjectTypes.FPDF_OBJECT_NUMBER:
var numValue = annot.GetNumberValue(FpdfAnnotationKeys.kP, out float values);
break;
}
}
}
break;
case FpdfAnnotSubtypes.SQUARE:
break;
}
}
}
注解预览展示:现在交互支持打开url、页面跳转、打开内置链接、鼠标悬浮标注展示注释


浙公网安备 33010602011771号