123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- using ComPDFKit.Measure;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFPage;
- using ComPDFKit.Tool.Help;
- using System.Collections.Generic;
- using System.Linq;
- using static ComPDFKit.PDFAnnotation.CTextAttribute;
- namespace ComPDFKit.Tool.UndoManger
- {
- public class PolygonMeasureAnnotHistory : AnnotHistory
- {
- public override int GetAnnotIndex()
- {
- if (CurrentParam != null)
- {
- return CurrentParam.AnnotIndex;
- }
- return base.GetAnnotIndex();
- }
- public override int GetPageIndex()
- {
- if (CurrentParam != null)
- {
- return CurrentParam.PageIndex;
- }
- return base.GetPageIndex();
- }
- public override void SetAnnotIndex(int newIndex)
- {
- if (CurrentParam != null)
- {
- CurrentParam.AnnotIndex = newIndex;
- }
- if (PreviousParam != null)
- {
- PreviousParam.AnnotIndex = newIndex;
- }
- }
- internal override bool Add()
- {
- PolygonMeasureParam currentParam = CurrentParam as PolygonMeasureParam;
- if (CurrentParam == null || PDFDoc == null || !PDFDoc.IsValid())
- {
- return false;
- }
- CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
- CPDFPolygonAnnotation polygonAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON) as CPDFPolygonAnnotation;
- if (polygonAnnot != null)
- {
- int annotIndex = pdfPage.GetAnnotCount() - 1;
- if (currentParam.HasFillColor)
- {
- if (currentParam.FillColor != null && currentParam.FillColor.Length == 3)
- {
- polygonAnnot.SetBgColor(currentParam.FillColor);
- }
- }
- if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
- {
- polygonAnnot.SetLineColor(currentParam.LineColor);
- }
- polygonAnnot.SetTransparency(currentParam.Transparency);
- polygonAnnot.SetLineWidth(currentParam.LineWidth);
- polygonAnnot.SetPoints(currentParam.SavePoints);
- polygonAnnot.SetAnnotBorderEffector(currentParam.BorderEffector);
- polygonAnnot.SetRect(currentParam.ClientRect);
- if (currentParam.LineDash != null)
- {
- if (currentParam.LineDash.Length == 0)
- {
- polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
- }
- else
- {
- List<float> floatArray = new List<float>();
- foreach (float num in currentParam.LineDash)
- {
- floatArray.Add(num);
- }
- polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
- }
- }
- if(polygonAnnot.IsMeasured())
- {
- CTextAttribute textAttribute = new CTextAttribute();
- textAttribute.FontColor = currentParam.FontColor;
- textAttribute.FontSize = (float)currentParam.FontSize;
- textAttribute.FontName = CFontNameHelper.ObtainFontName(CFontNameHelper.GetFontType(currentParam.FontName),
- currentParam.IsBold,
- currentParam.IsItalic);
- polygonAnnot.SetTextAttribute(textAttribute);
- if (currentParam.measureInfo != null)
- {
- CPDFAreaMeasure polygonMeasure = polygonAnnot.GetAreaMeasure();
- if (polygonMeasure != null)
- {
- polygonMeasure.SetMeasureInfo(currentParam.measureInfo);
- polygonMeasure.SetMeasureScale(currentParam.measureInfo.RulerBase, currentParam.measureInfo.RulerBaseUnit,
- currentParam.measureInfo.RulerTranslate, currentParam.measureInfo.RulerTranslateUnit);
- polygonMeasure.UpdateAnnotMeasure();
- }
- }
- }
-
- if (!string.IsNullOrEmpty(currentParam.Author))
- {
- polygonAnnot.SetAuthor(currentParam.Author);
- }
- if (!string.IsNullOrEmpty(currentParam.Content))
- {
- polygonAnnot.SetContent(currentParam.Content);
- }
- polygonAnnot.SetIsLocked(currentParam.Locked);
- polygonAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
- polygonAnnot.UpdateAp();
- polygonAnnot.ReleaseAnnot();
- if (currentParam != null)
- {
- currentParam.AnnotIndex = annotIndex;
- }
- if (PreviousParam != null)
- {
- PreviousParam.AnnotIndex = annotIndex;
- }
- return true;
- }
- return false;
- }
- internal override bool Update(bool isUndo)
- {
- if (CurrentParam as PolygonMeasureParam == null || PreviousParam as PolygonMeasureParam == null)
- {
- return false;
- }
- if (MakeAnnotValid(CurrentParam))
- {
- CPDFPolygonAnnotation polygonAnnot = Annot as CPDFPolygonAnnotation;
- if (polygonAnnot == null || !polygonAnnot.IsValid())
- {
- return false;
- }
- PolygonMeasureParam updateParam = (isUndo ? PreviousParam : CurrentParam) as PolygonMeasureParam;
- PolygonMeasureParam checkParam = (isUndo ? CurrentParam : PreviousParam) as PolygonMeasureParam;
- if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
- {
- if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
- {
- polygonAnnot.SetLineColor(updateParam.LineColor);
- }
- }
- if (!CheckArrayEqual(updateParam.FillColor, checkParam.FillColor))
- {
- if (updateParam.HasFillColor)
- {
- if (updateParam.FillColor != null && updateParam.FillColor.Length == 3)
- {
- polygonAnnot.SetBgColor(updateParam.FillColor);
- }
- }
- else
- {
- polygonAnnot.ClearBgColor();
- }
- }
- if (updateParam.Transparency != checkParam.Transparency)
- {
- polygonAnnot.SetTransparency((byte)updateParam.Transparency);
- }
- if (updateParam.LineWidth != checkParam.LineWidth)
- {
- polygonAnnot.SetLineWidth((byte)updateParam.LineWidth);
- }
- if (!CheckArrayEqual(updateParam.LineDash, checkParam.LineDash))
- {
- if (updateParam.LineDash.Length == 0)
- {
- polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
- }
- else
- {
- List<float> floatArray = new List<float>();
- foreach (float num in updateParam.LineDash)
- {
- floatArray.Add(num);
- }
- polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
- }
- }
- if (!updateParam.SavePoints.SequenceEqual(checkParam.SavePoints))
- {
- polygonAnnot.SetPoints(updateParam.SavePoints);
- }
- if(!updateParam.BorderEffector.Equals(checkParam.BorderEffector))
- {
- polygonAnnot.SetAnnotBorderEffector(updateParam.BorderEffector);
- }
- if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
- {
- polygonAnnot.SetRect(updateParam.ClientRect);
- }
- if (updateParam.Author != checkParam.Author)
- {
- polygonAnnot.SetAuthor(updateParam.Author);
- }
- if (updateParam.Content != checkParam.Content)
- {
- polygonAnnot.SetContent(updateParam.Content);
- }
- if (updateParam.Locked != checkParam.Locked)
- {
- polygonAnnot.SetIsLocked(updateParam.Locked);
- }
- if (polygonAnnot.IsMeasured())
- {
- if (updateParam.measureInfo != checkParam.measureInfo)
- {
- CPDFAreaMeasure polygonMeasure = polygonAnnot.GetAreaMeasure();
- if (polygonMeasure != null)
- {
- polygonMeasure.SetMeasureInfo(updateParam.measureInfo);
- polygonMeasure.SetMeasureScale(updateParam.measureInfo.RulerBase, updateParam.measureInfo.RulerBaseUnit,
- updateParam.measureInfo.RulerTranslate, updateParam.measureInfo.RulerTranslateUnit);
- polygonMeasure.UpdateAnnotMeasure();
- }
- }
- }
- polygonAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
- polygonAnnot.UpdateAp();
- return true;
- }
- return false;
- }
- internal override bool Remove()
- {
- if (MakeAnnotValid(CurrentParam))
- {
- Annot.RemoveAnnot();
- return true;
- }
- return false;
- }
- }
- }
|