Files
OnDoc/API_DocTest/Form1.cs
Stefan Hutter 30d047e33d update 20260220
2026-02-20 18:04:31 +01:00

389 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using Syncfusion.Drawing;
using Syncfusion.Windows.Forms.Tools;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Windows.Forms.PdfViewer;
using System.IO;
using Syncfusion.Windows.Forms.Edit.Interfaces;
using Syncfusion.Windows.Forms.Edit.Enums;
using Syncfusion.Windows.Forms.Edit.Implementation.Config;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Windows.Forms.Design;
namespace API_DocTest
{
public partial class Form1 : Form
{
static string connectionstring = Properties.Settings.Default.ConnectionString;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string lickey = "MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ==";
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(lickey);
LoadJSON();
Form1_Resize(sender, e);
Refresh_Filelist();
}
private void Refresh_Filelist()
{
listBox1.Items.Clear();
DirectoryInfo d = new DirectoryInfo(Properties.Settings.Default.tempdir); //Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.json"); //Getting Text files
string str = "";
foreach (FileInfo file in Files)
{
listBox1.Items.Add(file.Name);
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
var key = GetKey();
if (string.IsNullOrEmpty(key)) return;
string json = LoadJsonFromDb(key);
editControl1.Text = json ?? "{}";
editControl1.MoveToBeginning();
pdfViewerControl1.Unload(); // PDF zurücksetzen
}
private string LoadJsonFromDb(string key)
{
string json = null;
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT JavaScriptObject FROM ProvDokuments WHERE ProvDokumentid = @key", con))
{
cmd.Parameters.AddWithValue("@key", key);
con.Open();
json = cmd.ExecuteScalar()?.ToString();
}
if (json == null)
{
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT JsonObjekt FROM _OnDoc_API_TestScripts WHERE id = @key", con))
{
cmd.Parameters.AddWithValue("@key", key);
con.Open();
json = cmd.ExecuteScalar()?.ToString();
}
}
return json;
//using (var con = new SqlConnection(connectionstring))
//using (var cmd = new SqlCommand(
// "SELECT JavaScriptObject FROM ProvDokuments WHERE ProvDokumentid = @key", con))
//{
// cmd.Parameters.AddWithValue("@key", key);
// con.Open();
// return cmd.ExecuteScalar() as string;
//}
}
private void SaveJsonToDb(string key, string json)
{
using (var con = new SqlConnection(connectionstring))
using (var cmd = new SqlCommand(@"
IF EXISTS (SELECT 1 FROM _OnDoc_API_TestScripts WHERE id = @key)
UPDATE _OnDoc_API_TestScripts SET JsonObjekt =@json
WHERE id = @key
ELSE
INSERT INTO _OnDoc_API_TestScripts (id,JsonObjekt)
VALUES (@key, @json)
", con))
{
cmd.Parameters.AddWithValue("@key", key);
cmd.Parameters.AddWithValue("@json", json);
con.Open();
cmd.ExecuteNonQuery();
}
}
private string GetKey()
{
return txtdbkey.Text;
}
private string GetKey1()
{
return txtfilekey.Text;
}
static readonly string ApiUrl = Properties.Settings.Default.DogGenURI;
static readonly string token = Properties.Settings.Default.Token;
public static byte[] Generate(string json)
{
try
{
using (var client = new HttpClient())
{
// 🔐 Bearer Token
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);
var content = new StringContent(
json,
Encoding.UTF8,
"application/json");
var response = client.PostAsync(ApiUrl, content).Result;
var responseText = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode)
{
// optional: Logging
return null;
}
var jsonResult = JObject.Parse(responseText);
var base64Pdf = jsonResult["file"]?.ToString();
if (string.IsNullOrEmpty(base64Pdf))
return null;
return Convert.FromBase64String(base64Pdf);
}
}
catch (Exception ex)
{
// Logging
return null;
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
var key = GetKey();
if (string.IsNullOrEmpty(key)) return;
SaveJsonToDb(key,editControl1.Text);
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
try
{
byte[] byteArray = Generate(editControl1.Text);
string temp_inBase64 = Convert.ToBase64String(byteArray);
var stream = new MemoryStream(Convert.FromBase64String(temp_inBase64));
if (stream.Length > 0)
{
this.pdfViewerControl1.Load(stream);
this.pdfViewerControl1.Visible = true;
pdfViewerControl1.ZoomMode = ZoomMode.FitWidth;
}
else
{
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void Form1_Resize(object sender, EventArgs e)
{
try
{
splitContainer1.SplitterDistance = this.Width / 2;
}
catch { }
}
private void LoadJSON()
{
using (SqlConnection conn = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT id FROM _OnDoc_API_TestScripts ORDER BY id", conn))
{
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
comboBox1.Items.Clear();
while (reader.Read())
{
comboBox1.Items.Add(reader.GetString(0));
}
}
}
}
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.txtKey.Text= toolStripComboBox1.Text.ToString();
toolStripButton1_Click(sender, e);
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
string key = GetKey();
string json = null;
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT JavaScriptObject FROM ProvDokuments WHERE ProvDokumentid = @key", con))
{
cmd.Parameters.AddWithValue("@key", key);
con.Open();
json = cmd.ExecuteScalar()?.ToString();
}
if (json == null)
{
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT JsonObjekt FROM _OnDoc_API_TestScripts WHERE id = @key", con))
{
cmd.Parameters.AddWithValue("@key", key);
con.Open();
json = cmd.ExecuteScalar()?.ToString();
}
}
System.IO.File.WriteAllText(Properties.Settings.Default.tempdir + DateTime.Now.ToString("yyyymmddhhMMss") + ".json",json);
}
private void button1_Click(object sender, EventArgs e)
{
var key = GetKey();
if (string.IsNullOrEmpty(key)) return;
string json = LoadJsonFromDb(key);
editControl1.Text = json ?? "{}";
editControl1.MoveToBeginning();
pdfViewerControl1.Unload(); // PDF zurücksetzen
}
private void button2_Click(object sender, EventArgs e)
{
var key = GetKey();
if (string.IsNullOrEmpty(key)) return;
SaveJsonToDb(key, editControl1.Text);
}
private void button3_Click(object sender, EventArgs e)
{
string key = GetKey1();
string json = null;
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT JavaScriptObject FROM ProvDokuments WHERE ProvDokumentid = @key", con))
{
cmd.Parameters.AddWithValue("@key", key);
con.Open();
json = cmd.ExecuteScalar()?.ToString();
}
if (json == null)
{
using (SqlConnection con = new SqlConnection(connectionstring))
using (SqlCommand cmd = new SqlCommand(
"SELECT JsonObjekt FROM _OnDoc_API_TestScripts WHERE id = @key", con))
{
cmd.Parameters.AddWithValue("@key", key);
con.Open();
json = cmd.ExecuteScalar()?.ToString();
}
}
System.IO.File.WriteAllText(Properties.Settings.Default.tempdir + txtfilename.Text + ".json", json);
Refresh_Filelist();
}
private void button4_Click(object sender, EventArgs e)
{
try
{
byte[] byteArray = Generate(editControl1.Text);
string temp_inBase64 = Convert.ToBase64String(byteArray);
var stream = new MemoryStream(Convert.FromBase64String(temp_inBase64));
if (stream.Length > 0)
{
this.pdfViewerControl1.Load(stream);
this.pdfViewerControl1.Visible = true;
pdfViewerControl1.ZoomMode = ZoomMode.FitWidth;
}
else
{
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void button5_Click(object sender, EventArgs e)
{
string filename=Properties.Settings.Default.tempdir+listBox1.SelectedItem.ToString();
try
{
byte[] byteArray = Generate(System.IO.File.ReadAllText(filename));
string temp_inBase64 = Convert.ToBase64String(byteArray);
var stream = new MemoryStream(Convert.FromBase64String(temp_inBase64));
if (stream.Length > 0)
{
this.pdfViewerControl1.Load(stream);
this.pdfViewerControl1.Visible = true;
pdfViewerControl1.ZoomMode = ZoomMode.FitWidth;
}
else
{
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.txtdbkey.Text = comboBox1.SelectedItem.ToString();
button1_Click(sender, e);
}
}
}