try
{
string tmpPath = SaveFilePathName("","json文件|*.json","將本模板另存為json文件");
if (tmpPath=="") { return; }
JObject jsonObject = new JObject();
jsonObject["txtRectangWidth"] = txtRectangWidth.Text;
jsonObject["txtRectangHeight"] = txtRectangHeight.Text;
jsonObject["txtRectangPrintWidth"] = txtRectangPrintWidth.Text;
jsonObject["txtRectangPrintHeight"] = txtRectangPrintHeight.Text;
jsonObject["txtLabelWidth"] = txtLabelWidth.Text;
jsonObject["txtLabelHeight"] = txtLabelHeight.Text;
jsonObject["txtLabelMoveUp"] = txtLabelMoveUp.Text;
jsonObject["txtLabelMoveLeft"] = txtLabelMoveLeft.Text;
jsonObject["cBoxPrintDirect"] = cBoxPrintDirect.Text;
jsonObject["cBoxPrinterName"] = cBoxPrinterName.Text;
if (chkPrintTestRectangle.Checked == true) { jsonObject["cBoxPrintDirect"] = "1"; } else { jsonObject["cBoxPrintDirect"] = "0"; }
if (chkShowPrintingPreview.Checked == true) { jsonObject["chkShowPrintingPreview"] = "1"; } else { jsonObject["chkShowPrintingPreview"] = "0"; }
if (chkLogoAble.Checked == true) { jsonObject["chkLogoAble"] = "1"; } else { jsonObject["chkLogoAble"] = "0"; }
if (chkTextAble.Checked == true) { jsonObject["chkTextAble"] = "1"; } else { jsonObject["chkTextAble"] = "0"; }
if (chkBarcodeAble.Checked == true) { jsonObject["chkBarcodeAble"] = "1"; } else { jsonObject["chkBarcodeAble"] = "0"; }
if (chkQrcodeAble.Checked == true) { jsonObject["chkQrcodeAble"] = "1"; } else { jsonObject["chkQrcodeAble"] = "0"; }
if (chkLineAble.Checked == true) { jsonObject["chkLineAble"] = "1"; } else { jsonObject["chkLineAble"] = "0"; }
if (chkRectangleAble.Checked == true) { jsonObject["chkRectangleAble"] = "1"; } else { jsonObject["chkRectangleAble"] = "0"; }
//寫入LogoAble
JObject jObjLogoAble = new JObject();
jObjLogoAble["txtLogoTop"] = txtLogoTop.Text;
jObjLogoAble["txtLogoLeft"] = txtLogoLeft.Text;
jObjLogoAble["txtLogoWidth"] = txtLogoWidth.Text;
jObjLogoAble["txtLogoHeight"] = txtLogoHeight.Text;
jObjLogoAble["txtLogoPath"] = txtLogoPath.Text;
jObjLogoAble["txtLogoUrl"] = txtLogoUrl.Text;
JArray arrayLogoAble = new JArray() { jObjLogoAble };
jsonObject["LogoAble"] = arrayLogoAble;
//寫入TextAble
JObject jObjTextAble = new JObject();
jObjTextAble["txtTextTop"] = txtTextTop.Text;
jObjTextAble["txtTextLeft"] = txtTextLeft.Text;
jObjTextAble["txtTextWidth"] = txtTextWidth.Text;
jObjTextAble["cBoxTextFontSize"] = cBoxTextFontSize.Text;
jObjTextAble["cBoxTextFontFamily"] = cBoxTextFontFamily.Text;
jObjTextAble["txtTextContent"] = txtTextContent.Text;
jObjTextAble["cBoxTextType"] = cBoxTextType.Text;
if (radioTextScale.Checked == true) { jObjTextAble["radioTextScale"] = "1"; } else { jObjTextAble["radioTextScale"] = "0"; }
if (radioTextDirectWrap.Checked == true) { jObjTextAble["radioTextDirectWrap"] = "1"; } else { jObjTextAble["radioTextDirectWrap"] = "0"; }
if (chkTextBold.Checked == true) { jObjTextAble["chkTextBold"] = "1"; } else { jObjTextAble["chkTextBold"] = "0"; }
if (chkTextDirect.Checked == true) { jObjTextAble["chkTextDirect"] = "1"; } else { jObjTextAble["chkTextDirect"] = "0"; }
JArray arrayTextAble = new JArray() { jObjTextAble };
jsonObject["TextAble"] = arrayTextAble;
//寫入BarcodeAble
JObject jObjBarcodeAble = new JObject();
jObjBarcodeAble["cBoxBarcodeType"] = cBoxBarcodeType.Text;
jObjBarcodeAble["txtBarcodeWidth"] = txtBarcodeWidth.Text;
jObjBarcodeAble["txtBarcodeHeight"] = txtBarcodeHeight.Text;
jObjBarcodeAble["txtBarcodeTop"] = txtBarcodeTop.Text;
jObjBarcodeAble["txtBarcodeLeft"] = txtBarcodeLeft.Text;
jObjBarcodeAble["txtBarCode"] = txtBarCode.Text;
JArray arrayBarcodeAble = new JArray() { jObjBarcodeAble };
jsonObject["BarcodeAble"] = arrayBarcodeAble;
//寫入QrcodeAble
JObject jObjQrcodeAble = new JObject();
jObjQrcodeAble["txtQrcodeWidth"] = txtQrcodeWidth.Text;
jObjQrcodeAble["txtQrcodeTop"] = txtQrcodeTop.Text;
jObjQrcodeAble["txtQrcodeLeft"] = txtQrcodeLeft.Text;
jObjQrcodeAble["txtQRCode"] = txtQRCode.Text;
jObjQrcodeAble["txtQrcodeAdjustNum"] = txtQrcodeAdjustNum.Text;
JArray arrayQrcodeAble = new JArray() { jObjQrcodeAble };
jsonObject["QrcodeAble"] = arrayQrcodeAble;
//寫入LineAble
JObject jObjLineAble = new JObject();
jObjLineAble["cBoxLineDirect"] = cBoxLineDirect.Text;
jObjLineAble["txtLineTop"] = txtLineTop.Text;
jObjLineAble["txtLineLeft"] = txtLineLeft.Text;
jObjLineAble["txtLineLength"] = txtLineLength.Text;
jObjLineAble["txtLineWidth"] = txtLineWidth.Text;
jObjLineAble["cBoxLineStyle"] = cBoxLineStyle.Text;
JArray arrayLineAble = new JArray() { jObjLineAble };
jsonObject["LineAble"] = arrayLineAble;
//寫入RectangleAble
JObject jObjRectangleAble = new JObject();
jObjRectangleAble["txtRectangleTop"] = txtRectangleTop.Text;
jObjRectangleAble["txtRectangleLeft"] = txtRectangleLeft.Text;
jObjRectangleAble["txtRectangleWidth"] = txtRectangleWidth.Text;
jObjRectangleAble["txtRectangleHeight"] = txtRectangleHeight.Text;
jObjRectangleAble["txtRectangleBorderWidth"] = txtRectangleBorderWidth.Text;
jObjRectangleAble["cBoxRectangleStyle"] = cBoxRectangleStyle.Text;
JArray arrayRectangleAble = new JArray() { jObjRectangleAble };
jsonObject["RectangleAble"] = arrayRectangleAble;
//保存json文件
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(tmpPath, output);
}
catch(Exception ex)
{
MessageBox.Show("保存失敗,錯誤原因:\r\n" + ex.Message, "保存失敗", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
/// <summary>
/// 選擇保存文件的名稱以及路徑 取消返回 空"";
/// </summary>
/// <param name="fileName"></param>
/// <param name="filter"></param>
/// <param name="title"></param>
/// <returns></returns>
public static string SaveFilePathName(string fileName = null, string filter = null, string title = null)
{
string path = "";
SaveFileDialog fbd = new SaveFileDialog();
if (!string.IsNullOrEmpty(fileName))
{
fbd.FileName = fileName;
}
if (!string.IsNullOrEmpty(filter))
{
fbd.Filter = filter;// "Excel|*.xls;*.xlsx;";
}
if (!string.IsNullOrEmpty(title))
{
fbd.Title = title;// "保存為";
}
if (fbd.ShowDialog() == DialogResult.OK)
{
path = fbd.FileName;
}
return path;
}