Update 20260426

This commit is contained in:
Stefan Hutter
2026-04-26 09:09:54 +02:00
parent 9629f247f9
commit d3d7b06872
79 changed files with 11391 additions and 146 deletions

View File

@@ -9,6 +9,7 @@ using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
@@ -88,10 +89,12 @@ namespace Versandstrasse
public void writelog(string inhalt)
{
// inhalt = DateTime.Now + " / " + inhalt;
// inhalt = DateTime.Now + " / " + inhalt;
//System.IO.File.AppendAllText(@"h:\edoka_work\vs.log", Environment.NewLine + inhalt);
}
public string FixPageBreaks(string base64Pdf)
public string FixPageBreaks(string base64Pdf, bool rotate)
{
byte[] inputBytes = Convert.FromBase64String(base64Pdf);
@@ -140,6 +143,7 @@ namespace Versandstrasse
lastBreakIndex = i;
lastOrientation = currentOrientation;
}
// if (isLandscape) { loadedDoc.Pages[i].Rotation== PdfPageRotateAngle.RotateAngle90;}
i++;
}
@@ -155,8 +159,11 @@ namespace Versandstrasse
loadedDoc.Pages.Add(lastSize);
}
loadedDoc.Save(outputStream);
loadedDoc.Close(true);
return Convert.ToBase64String(outputStream.ToArray());
}
@@ -164,7 +171,7 @@ namespace Versandstrasse
private bool IsLandscape(float width, float height, PdfPageRotateAngle rotation)
private bool IsLandscape(float width, float height, PdfPageRotateAngle rotation)
{
bool landscape = width > height;
@@ -181,7 +188,7 @@ namespace Versandstrasse
// =========================================================
// 📄 A4 erkennen (mit Toleranz)
// =========================================================
private bool IsA4(PdfLoadedPage page)
private bool IsA4(PdfLoadedPage page)
{
const float tolerance = 5f;
@@ -199,13 +206,155 @@ namespace Versandstrasse
Landscape
}
public string check_pdf_pages(string dokument)
public string RotatePage1(string base64pdf, bool rotate)
{
return FixPageBreaks(dokument);
byte[] inputBytes = Convert.FromBase64String(base64pdf);
MemoryStream inputStream = new MemoryStream(inputBytes);
MemoryStream outputStream = new MemoryStream();
PdfDocument newDoc = new PdfDocument();
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
PdfLoadedPage loadedPage = loadedDocument.Pages[i] as PdfLoadedPage;
PdfTemplate template = loadedPage.CreateTemplate();
PdfSection pdfSection = newDoc.Sections.Add();
pdfSection.PageSettings.Margins.All = 0;
pdfSection.PageSettings.Size = new SizeF(loadedPage.Graphics.ClientSize);
PdfPage newPage = pdfSection.Pages.Add();
PdfGraphicsState state = newPage.Graphics.Save();
if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle90)
{
newPage.Graphics.TranslateTransform(newPage.Graphics.ClientSize.Width, 0);
newPage.Graphics.RotateTransform(90);
}
else if (loadedPage.Rotation == PdfPageRotateAngle.RotateAngle270)
{
newPage.Graphics.TranslateTransform(0, newPage.Graphics.ClientSize.Height);
newPage.Graphics.RotateTransform(270);
}
newPage.Graphics.DrawPdfTemplate(template, new PointF(0, 0));
newPage.Graphics.Restore(state);
}
newDoc.Save(outputStream);
newDoc.Close(true);
loadedDocument.Close();
return Convert.ToBase64String(outputStream.ToArray());
// using (MemoryStream inputStream = new MemoryStream(inputBytes))
// using (MemoryStream outputStream = new MemoryStream())
// {
// PdfLoadedDocument loadedDoc = new PdfLoadedDocument(inputStream);
// PdfDocument newDoc = new PdfDocument();
// for (int i = 0; i < loadedDoc.Pages.Count; i++)
// {
// PdfLoadedPage loadedPage = loadedDoc.Pages[i] as PdfLoadedPage;
// SizeF size = loadedPage.Size;
// SizeF targetSize = size;
// bool isRotated = loadedPage.Rotation == PdfPageRotateAngle.RotateAngle90 ||
// loadedPage.Rotation == PdfPageRotateAngle.RotateAngle270;
// if (isRotated)
// {
// targetSize = new SizeF(size.Height, size.Width);
// }
// PdfSection section = newDoc.Sections.Add();
// // Seitengröße setzen
// section.PageSettings.Size = targetSize;
// // Neue Seite erzeugen
// PdfPage newPage = section.Pages.Add();
// PdfGraphics g = newPage.Graphics;
// PdfTemplate template = loadedPage.CreateTemplate();
// // 👉 Transformation anwenden
// switch (loadedPage.Rotation)
// {
// case PdfPageRotateAngle.RotateAngle90:
// g.TranslateTransform(size.Height, 0);
// g.RotateTransform(90);
// break;
// case PdfPageRotateAngle.RotateAngle270:
// g.TranslateTransform(0, size.Width);
// g.RotateTransform(270);
// break;
// case PdfPageRotateAngle.RotateAngle180:
// g.TranslateTransform(size.Width, size.Height);
// g.RotateTransform(180);
// break;
// }
// //g.DrawPdfTemplate(template, new PointF(-50,50));
// g.DrawPdfTemplate(template, new PointF(1, 1),
//new SizeF(targetSize.Width - 2, targetSize.Height - 2));
// }
// newDoc.Save(outputStream);
// newDoc.Close(true);
// loadedDoc.Close();
// return Convert.ToBase64String(outputStream.ToArray());
// }
}
public string RotatePage(string base64pdf, bool rotate)
{
// Load the existing PDF document
byte[] inputBytes = Convert.FromBase64String(base64pdf);
using (MemoryStream inputStream = new MemoryStream(inputBytes))
using (MemoryStream outputStream = new MemoryStream())
{
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);
// Iterate through all pages
foreach (PdfLoadedPage page in loadedDocument.Pages)
{
// Set rotation to portrait (0 degrees)
var size = page.Size;
var rotation = page.Rotation;
bool isLandscape = IsLandscape(size.Width, size.Height, rotation);
if (isLandscape)
{
page.Rotation = PdfPageRotateAngle.RotateAngle90;
}
}
loadedDocument.Save(outputStream);
loadedDocument.Close(true);
return Convert.ToBase64String(outputStream.ToArray());
}
}
public string check_pdf_pages(string dokument, bool rotatepage=false)
{
if (rotatepage)
{
return RotatePage1(RotatePage(FixPageBreaks(dokument, true), true), true);
}
else
{
return FixPageBreaks(dokument, true);
}
var stream = new MemoryStream(Convert.FromBase64String(dokument));
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
if (loadedDocument.Pages.Count % 2 == 0) { } else { Add_EmptyPage(ref loadedDocument); }
MemoryStream outputStream = new MemoryStream();
string dok = "";
@@ -227,11 +376,11 @@ namespace Versandstrasse
{
stream = null;
loadedDocument = null;
outputStream = null;
outputStream = null;
}
}
public string Prepare_PDF(string dokumentid, string tempdir, string connectionstring, string resturi, string apikey, string startuppath, versandscript vs = null, serienbrief sb = null, string sbsourcedok = "", bool couvertpos = false)
{
try
@@ -241,7 +390,7 @@ namespace Versandstrasse
this.resturi = resturi;
this.apikey = apikey;
clsdok dok = new clsdok("", "", "", "");
if (sbsourcedok != "")
{
@@ -341,11 +490,11 @@ namespace Versandstrasse
{
foreach (TextReplace tr in vdok.TextReplaces)
{
if (tr.TextToReplace.Trim() != "")
{
writelog("Replace Start " + tr.TextToReplace);
Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText, tr.Fontname, tr.Fontsize, startuppath,tr);
Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText, tr.Fontname, tr.Fontsize, startuppath, tr);
writelog("Replace Ende");
}
}
@@ -356,11 +505,11 @@ namespace Versandstrasse
MemoryStream outputStream = new MemoryStream();
try
{
loadedDocument.Save(outputStream);
byte[] bytes;
bytes = outputStream.ToArray();
@@ -391,16 +540,16 @@ namespace Versandstrasse
PdfDocumentBase.Merge(finalDoc, streams);
MemoryStream outputStream = new MemoryStream();
string filename = tempdir + Guid.NewGuid().ToString() + ".pdf";
finalDoc.Save(outputStream);
PdfLoadedDocument loadeddoc = new PdfLoadedDocument(outputStream);
loadeddoc.ConvertToPDFA(PdfConformanceLevel.Pdf_A1B);
loadeddoc.Save(outputStream);
byte[] bytes;
bytes = outputStream.ToArray();
@@ -420,13 +569,13 @@ namespace Versandstrasse
writelog("Merge Ende");
return vscript.FinalDoc;
}
catch (Exception ex) { writelog("2" + ex.Message);return ""; }
catch (Exception ex) { writelog("2" + ex.Message); return ""; }
}
private void split_doc(ref versandscript vscript, string document)
{
writelog("Split");
; //SaveBase64ToFile(document, @"x:\f0.pdf");
; //SaveBase64ToFile(document, @"x:\f0.pdf");
vscript.dokuments[0].sourcedokument = document;
vsdokument vsd = vscript.dokuments[0];
var stream = new MemoryStream(Convert.FromBase64String(vscript.dokuments[0].sourcedokument));
@@ -530,7 +679,7 @@ namespace Versandstrasse
}
private void Replace_Text(ref PdfLoadedDocument document, string texttoreplace, string newtext, string fontname, string fontsize, string startuppath, TextReplace tr)
{
PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage;
@@ -539,10 +688,10 @@ namespace Versandstrasse
int y = -5;
int w = 0;
int h = 5;
if (tr.Boxpos_X != "") { x=Convert.ToInt32(tr.Boxpos_X); }
if (tr.Boxpos_Y != "") { y=Convert.ToInt32(tr.Boxpos_Y); }
if (tr.Boxpos_W != "") { w=Convert.ToInt32(tr.Boxpos_W); }
if (tr.Boxpos_H != "") { h=Convert.ToInt32(tr.Boxpos_H); }
if (tr.Boxpos_X != "") { x = Convert.ToInt32(tr.Boxpos_X); }
if (tr.Boxpos_Y != "") { y = Convert.ToInt32(tr.Boxpos_Y); }
if (tr.Boxpos_W != "") { w = Convert.ToInt32(tr.Boxpos_W); }
if (tr.Boxpos_H != "") { h = Convert.ToInt32(tr.Boxpos_H); }
//Font und Size
string fname = "Futura";
string fsize = "8";
@@ -550,12 +699,12 @@ namespace Versandstrasse
if (tr.Fontname != "") { fname = tr.Fontname; }
if (tr.Fontsize != "") { fsize = tr.Fontsize; }
// PdfUsedFont usedfont = get_fontname(ref document, fname);
// PdfUsedFont usedfont = get_fontname(ref document, fname);
PdfFont font = new PdfTrueTypeFont(new Font(fname, Convert.ToInt32(fsize)));
Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
document.FindText(texttoreplace, out matchedTextbounds);
PdfSolidBrush brush = new PdfSolidBrush(Color.White);
try
{
@@ -594,9 +743,9 @@ namespace Versandstrasse
}
}
@@ -723,26 +872,26 @@ namespace Versandstrasse
private void Add_EmptyPage(ref PdfLoadedDocument document)
{
SizeF size = document.Pages[document.Pages.Count-1].Size;
bool landscape = false;
// Determine orientation based on size
if (size.Width > size.Height)
{
landscape = true;
}
else
{
landscape = false;
}
SizeF size = document.Pages[document.Pages.Count - 1].Size;
bool landscape = false;
// Determine orientation based on size
if (size.Width > size.Height)
{
landscape = true;
}
else
{
landscape = false;
}
PdfPage page = (PdfPage)document.Pages.Add();
if (landscape == true)
{
page.Section.PageSettings.Size = PdfPageSize.A4;
page.Section.PageSettings.Width = size.Width; // A4 landscape width
page.Section.PageSettings.Height = size.Height; // A4 landscape height
}
PdfPage page = (PdfPage)document.Pages.Add();
if (landscape == true)
{
page.Section.PageSettings.Size = PdfPageSize.A4;
page.Section.PageSettings.Width = size.Width; // A4 landscape width
page.Section.PageSettings.Height = size.Height; // A4 landscape height
}
}
public Image DrawText(Color foreColor, Color backColor, string fontName, int fontSize, string txt, int width, int height, string zusatz, string zusatzfontname, int zusatzfontsize, int rotation)

Binary file not shown.

Binary file not shown.