Skip to content

Commit

Permalink
Adding forms
Browse files Browse the repository at this point in the history
  • Loading branch information
KhromovNikita committed Feb 13, 2025
1 parent c796e0a commit b7289d2
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 127 deletions.
14 changes: 13 additions & 1 deletion common/HistoryCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,13 @@
window['AscDFH'].historyitem_type_Pdf_Page = 2230 << 16;
window['AscDFH'].historyitem_type_Pdf_Annot_Stamp = 2231 << 16;
window['AscDFH'].historyitem_type_Pdf_PropLocker = 2232 << 16;
window['AscDFH'].historyitem_type_Pdf_Checkbox_Field = 2233 << 16;
window['AscDFH'].historyitem_type_Pdf_Combobox_Field = 2234 << 16;
window['AscDFH'].historyitem_type_Pdf_Listbox_Field = 2235 << 16;
window['AscDFH'].historyitem_type_Pdf_Button_Field = 2236 << 16;
window['AscDFH'].historyitem_type_Pdf_Radiobutton_Field = 2237 << 16;
window['AscDFH'].historyitem_type_Pdf_Signature_Field = 2238 << 16;
window['AscDFH'].historyitem_type_Pdf_Text_Field = 2239 << 16;

window['AscDFH'].historyitem_type_CustomProperties = 2301 << 16;

Expand Down Expand Up @@ -4017,13 +4024,17 @@
AscDFH.historyitem_Pdf_Form_Changed = AscDFH.historyitem_type_Pdf_Form | 5;
AscDFH.historyitem_Pdf_Form_Parent_Value = AscDFH.historyitem_type_Pdf_Form | 6;
AscDFH.historyitem_Pdf_Form_Format_Value = AscDFH.historyitem_type_Pdf_Form | 7;
AscDFH.historyitem_Pdf_Form_Border_Color = AscDFH.historyitem_type_Pdf_Form | 8;
AscDFH.historyitem_Pdf_Form_BG_Color = AscDFH.historyitem_type_Pdf_Form | 9;
AscDFH.historyitem_Pdf_Form_Border_Style = AscDFH.historyitem_type_Pdf_Form | 10;

AscDFH.historyitem_Pdf_List_Form_Cur_Idxs = AscDFH.historyitem_type_Pdf_List_Form | 1;
AscDFH.historyitem_Pdf_List_Form_Top_Idx = AscDFH.historyitem_type_Pdf_List_Form | 2;
AscDFH.historyitem_Pdf_List_Form_Parent_Cur_Idxs = AscDFH.historyitem_type_Pdf_List_Form | 3;


AscDFH.historyitem_Pdf_Pushbutton_Image = AscDFH.historyitem_type_Pdf_Pushbutton | 1;
AscDFH.historyitem_Pdf_Pushbutton_Image = AscDFH.historyitem_type_Pdf_Pushbutton | 1;
AscDFH.historyitem_Pdf_Pushbutton_Header_Pos = AscDFH.historyitem_type_Pdf_Pushbutton | 2;

//------------------------------------------------------------------------------------------------------------------
// Типы изменений в PDF Annots
Expand Down Expand Up @@ -4581,6 +4592,7 @@
window['AscDFH'].historydescription_Pdf_ChangeFillColor = 0x2b2;
window['AscDFH'].historydescription_Pdf_ChangeOpacity = 0x2b3;
window['AscDFH'].historydescription_Pdf_MovePage = 0x2b4;
window['AscDFH'].historydescription_Pdf_AddField = 0x2b5;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
14 changes: 14 additions & 0 deletions common/TableId.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,17 +448,31 @@

if (editor && editor.isPdfEditor())
{
// drawings
this.m_oFactoryClass[AscDFH.historyitem_type_Shape] = AscPDF.CPdfShape;
this.m_oFactoryClass[AscDFH.historyitem_type_GraphicFrame] = AscPDF.CPdfGraphicFrame;
this.m_oFactoryClass[AscDFH.historyitem_type_ImageShape] = AscPDF.CPdfImage;
this.m_oFactoryClass[AscDFH.historyitem_type_Cnx] = AscPDF.CPdfConnectionShape;

// annots
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_Text] = AscPDF.CAnnotationText;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_Ink] = AscPDF.CAnnotationInk;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_Highlight] = AscPDF.CAnnotationHighlight;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_Underline] = AscPDF.CAnnotationUnderline;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_Strikeout] = AscPDF.CAnnotationStrikeout;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_FreeText] = AscPDF.CAnnotationFreeText;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Annot_Stamp] = AscPDF.CAnnotationStamp;

//forms
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Checkbox_Field] = AscPDF.CCheckBoxField;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Combobox_Field] = AscPDF.CComboBoxField;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Listbox_Field] = AscPDF.CListBoxField;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Button_Field] = AscPDF.CPushButtonField;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Radiobutton_Field] = AscPDF.CRadioButtonField;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Signature_Field] = AscPDF.CSignatureField;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Text_Field] = AscPDF.CTextField;

// other
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_Page] = AscPDF.CPageInfo;
this.m_oFactoryClass[AscDFH.historyitem_type_Pdf_PropLocker] = AscPDF.PropLocker;
}
Expand Down
50 changes: 50 additions & 0 deletions pdf/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,49 @@

};

/////////////////////////////////////////////////////////////
///////// For filed
////////////////////////////////////////////////////////////
PDFEditorApi.prototype.AddTextField = function() {
let oDoc = this.getPDFDoc();

oDoc.DoAction(function() {
let oField = oDoc.CreateTextField();
oDoc.AddField(oField, oDoc.GetCurPage());
}, AscDFH.historydescription_Pdf_AddField, this);
};
PDFEditorApi.prototype.AddDateField = function() {
let oDoc = this.getPDFDoc();

oDoc.DoAction(function() {
let oField = oDoc.CreateTextField(true);
oDoc.AddField(oField, oDoc.GetCurPage());
}, AscDFH.historydescription_Pdf_AddField, this);
};
PDFEditorApi.prototype.AddImageField = function() {
let oDoc = this.getPDFDoc();

oDoc.DoAction(function() {
let oField = oDoc.CreateButtonField(true);
oDoc.AddField(oField, oDoc.GetCurPage());
}, AscDFH.historydescription_Pdf_AddField, this);
};
PDFEditorApi.prototype.AddCheckboxField = function() {
let oDoc = this.getPDFDoc();

oDoc.DoAction(function() {
let oField = oDoc.CreateCheckboxField();
oDoc.AddField(oField, oDoc.GetCurPage());
}, AscDFH.historydescription_Pdf_AddField, this);
};
PDFEditorApi.prototype.AddRadiobuttonField = function() {
let oDoc = this.getPDFDoc();

oDoc.DoAction(function() {
let oField = oDoc.CreateCheckboxField(true);
oDoc.AddField(oField, oDoc.GetCurPage());
}, AscDFH.historydescription_Pdf_AddField, this);
};
/////////////////////////////////////////////////////////////
///////// For drawings
////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3130,6 +3173,13 @@
// freetext
PDFEditorApi.prototype['AddFreeTextAnnot'] = PDFEditorApi.prototype.AddFreeTextAnnot;

// forms
PDFEditorApi.prototype['AddTextField'] = PDFEditorApi.prototype.AddTextField;
PDFEditorApi.prototype['AddDateField'] = PDFEditorApi.prototype.AddDateField;
PDFEditorApi.prototype['AddImageField'] = PDFEditorApi.prototype.AddImageField;
PDFEditorApi.prototype['AddCheckboxField'] = PDFEditorApi.prototype.AddCheckboxField;
PDFEditorApi.prototype['AddRadiobuttonField'] = PDFEditorApi.prototype.AddRadiobuttonField;

// drawings
PDFEditorApi.prototype['AddTextArt'] = PDFEditorApi.prototype.AddTextArt;
PDFEditorApi.prototype['StartAddShape'] = PDFEditorApi.prototype.StartAddShape;
Expand Down
134 changes: 93 additions & 41 deletions pdf/src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ var CPresentation = CPresentation || function(){};
this.mouseDownField = null;
this.mouseDownAnnot = null;

this._id = AscCommon.g_oIdCounter.Get_NewId();
AscCommon.g_oTableId.Add(this, this._id);
this.Id = AscCommon.g_oIdCounter.Get_NewId();
AscCommon.g_oTableId.Add(this, this.Id);

this.History = new AscPDF.History(this);
this.History.Set_LogicDocument(this);
Expand Down Expand Up @@ -414,6 +414,90 @@ var CPresentation = CPresentation || function(){};
oViewer.paint();
}
};
CPDFDoc.prototype.CreateField = function(cName, cFieldType, oCoords) {
let oField;
switch (cFieldType) {
case AscPDF.FIELD_TYPES.button:
oField = new AscPDF.CPushButtonField(cName, oCoords);
break;
case AscPDF.FIELD_TYPES.checkbox:
oField = new AscPDF.CCheckBoxField(cName, oCoords);
break;
case AscPDF.FIELD_TYPES.combobox:
oField = new AscPDF.CComboBoxField(cName, oCoords);
break;
case AscPDF.FIELD_TYPES.listbox:
oField = new AscPDF.CListBoxField(cName, oCoords);
break;
case AscPDF.FIELD_TYPES.radiobutton:
oField = new AscPDF.CRadioButtonField(cName, oCoords);
break;
case AscPDF.FIELD_TYPES.signature:
oField = new AscPDF.CSignatureField(cName, oCoords);
break;
case AscPDF.FIELD_TYPES.text:
oField = new AscPDF.CTextField(cName, oCoords);
break;
case AscPDF.FIELD_TYPES.unknown:
oField = new AscPDF.CBaseField(cName, oCoords);
break;
}

oField.SetDocument(this);
return oField;
};
CPDFDoc.prototype.CreateTextField = function(bDateField) {
let oTextField = this.CreateField('TextField1', AscPDF.FIELD_TYPES.text, [200, 50, 350, 72]);

if (bDateField) {
oTextField.SetActions(AscPDF.FORMS_TRIGGERS_TYPES.Keystroke, [{
"S": AscPDF.ACTIONS_TYPES.JavaScript,
"JS": "AFDate_KeystrokeEx(\"m/d/yy\");"
}]);
oTextField.SetActions(AscPDF.FORMS_TRIGGERS_TYPES.Format, [{
"S": AscPDF.ACTIONS_TYPES.JavaScript,
"JS": "AFDate_FormatEx(\"m/d/yy\");"
}]);
}

return oTextField;
};
CPDFDoc.prototype.CreateButtonField = function(bImage) {
let sName = 'ButtonField1';

let aRect = bImage ? [218, 44, 290, 124] : [49, 83, 121, 103];
let oButtonField = this.CreateField(sName, AscPDF.FIELD_TYPES.button, aRect);

if (bImage) {
oButtonField.SetActions(AscPDF.FORMS_TRIGGERS_TYPES.MouseUp, [{
"S": AscPDF.ACTIONS_TYPES.JavaScript,
"JS": "event.target.buttonImportIcon();"
}]);

oButtonField.SetHeaderPosition(AscPDF.Api.Objects.position['iconOnly']);
oButtonField.SetBorderColor([0.7529]);
}
else {
oButtonField.SetBackgroundColor([0.7529]);
}

return oButtonField;
};
CPDFDoc.prototype.CreateCheckboxField = function(bRadio) {
let sName = 'CheckboxField1';
let nType = bRadio ? AscPDF.FIELD_TYPES.radiobutton : AscPDF.FIELD_TYPES.checkbox;
let aRect = [320, 45, 338, 63];

let oCheckboxField = this.CreateField(sName, nType, aRect);

oCheckboxField.SetBorderColor([0]);

if (bRadio) {
oCheckboxField.SetBorderStyle(AscPDF.BORDER_TYPES.inset);
}

return oCheckboxField;
};
CPDFDoc.prototype.FillFormsParents = function(aParentsInfo) {
let oChilds = this.GetParentsMap();
let oParents = {};
Expand All @@ -425,7 +509,7 @@ var CPresentation = CPresentation || function(){};

let sType = oChilds[nIdx][0].GetType();

let oParent = private_createField(aParentsInfo[i]["name"], sType, undefined, undefined, this);
let oParent = this.CreateField(aParentsInfo[i]["name"], sType);
if (aParentsInfo[i]["value"] != null)
oParent.SetParentValue(aParentsInfo[i]["value"]);
if (aParentsInfo[i]["Parent"] != null)
Expand Down Expand Up @@ -626,10 +710,10 @@ var CPresentation = CPresentation || function(){};
};

CPDFDoc.prototype.GetId = function() {
return this._id;
return this.Id;
};
CPDFDoc.prototype.Get_Id = function() {
return this._id;
CPDFDoc.prototype.GetId = function() {
return this.Id;
};
CPDFDoc.prototype.GetDrawingDocument = function() {
if (!editor || !editor.WordControl)
Expand Down Expand Up @@ -2522,7 +2606,7 @@ var CPresentation = CPresentation || function(){};
if (!oPageInfo)
return null;

let oField = private_createField(cName, cFieldType, nPageNum, aCoords, this);
let oField = this.CreateField(cName, cFieldType, aCoords);
if (!oField)
return null;

Expand Down Expand Up @@ -3452,7 +3536,7 @@ var CPresentation = CPresentation || function(){};
// создаем родительские поля, последнее будет виджет-полем
if (aPartNames.length > 1) {
if (this.rootFields.get(aPartNames[0]) == null) { // root поле
this.rootFields.set(aPartNames[0], private_createField(aPartNames[0], oField.GetType(), oField.GetPage(), []));
this.rootFields.set(aPartNames[0], this.CreateField(aPartNames[0], oField.GetType(), []));
}

let oParentField = this.rootFields.get(aPartNames[0]);
Expand All @@ -3468,7 +3552,7 @@ var CPresentation = CPresentation || function(){};
if (oExistsField)
oParentField = oExistsField;
else {
let oNewParent = private_createField(aPartNames[i], oField.GetType(), oField.GetPage(), []);
let oNewParent = this.CreateField(aPartNames[i], oField.GetType(), []);
oParentField.AddKid(oNewParent);
oParentField = oNewParent;
}
Expand Down Expand Up @@ -6898,38 +6982,6 @@ var CPresentation = CPresentation || function(){};
}
};

function private_createField(cName, cFieldType, nPageNum, oCoords, oPdfDoc) {
let oField;
switch (cFieldType) {
case AscPDF.FIELD_TYPES.button:
oField = new AscPDF.CPushButtonField(cName, nPageNum, oCoords, oPdfDoc);
break;
case AscPDF.FIELD_TYPES.checkbox:
oField = new AscPDF.CCheckBoxField(cName, nPageNum, oCoords, oPdfDoc);
break;
case AscPDF.FIELD_TYPES.combobox:
oField = new AscPDF.CComboBoxField(cName, nPageNum, oCoords, oPdfDoc);
break;
case AscPDF.FIELD_TYPES.listbox:
oField = new AscPDF.CListBoxField(cName, nPageNum, oCoords, oPdfDoc);
break;
case AscPDF.FIELD_TYPES.radiobutton:
oField = new AscPDF.CRadioButtonField(cName, nPageNum, oCoords, oPdfDoc);
break;
case AscPDF.FIELD_TYPES.signature:
oField = new AscPDF.CSignatureField(cName, nPageNum, oCoords, oPdfDoc);;
break;
case AscPDF.FIELD_TYPES.text:
oField = new AscPDF.CTextField(cName, nPageNum, oCoords, oPdfDoc);
break;
case AscPDF.FIELD_TYPES.unknown:
oField = new AscPDF.CBaseField(cName, nPageNum, oCoords, oPdfDoc);
break;
}

return oField;
}

function CreateAnnotByProps(oProps, oPdfDoc) {
let aRect = oProps.rect;
let nPageNum = oProps.page;
Expand Down
Loading

0 comments on commit b7289d2

Please sign in to comment.