Files
OnDoc/API_DocTest/Form3.cs
T
2026-06-10 18:14:56 +02:00

99 lines
3.0 KiB
C#

using Newtonsoft.Json.Linq;
using Syncfusion.Windows.Forms.PdfViewer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace API_DocTest
{
public partial class Form3 : Form
{
static readonly string ApiUrl = Properties.Settings.Default.DogGenURI;
static readonly string token = Properties.Settings.Default.Token;
public Form3()
{
InitializeComponent();
}
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 button5_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("Fehler beim Generieren." + ex.Message, "Generieren", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
}
}