update 20250501

This commit is contained in:
Stefan Hutter
2025-05-01 09:50:49 +02:00
parent eb5c908f0c
commit c7a4e24e3f
160 changed files with 1339 additions and 400 deletions

View File

@@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -17,6 +21,7 @@ using edoka_dms;
using Model;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Graphics.Fonts;
using Syncfusion.Pdf.Parsing;
using Syncfusion.XPS;
@@ -58,11 +63,11 @@ namespace Versandstrasse
string resturi = "";
string apikey = "";
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)
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)
{
Lic();
this.resturi=resturi;
this.apikey=apikey;
this.resturi = resturi;
this.apikey = apikey;
clsdok dok = new clsdok("", "", "", "");
if (sbsourcedok != "")
@@ -82,7 +87,8 @@ namespace Versandstrasse
Database.DB db = new Database.DB(connectionstring);
db.clear_parameter();
db.add_parameter("@dokumentid", dokumentid);
if (sb != null) {
if (sb != null)
{
db.add_parameter("@doktyp", sb.dokumenttypnr.ToString());
}
db.Get_Tabledata("ondoc_get_versandstrasse_parameter", true, false);
@@ -114,9 +120,9 @@ namespace Versandstrasse
{
}
if (vscript.Split_Doc !="")
if (vscript.Split_Doc != "")
{
split_doc(ref vscript,dok.dokument);
split_doc(ref vscript, dok.dokument);
}
if (vscript.PartCopies != "")
@@ -144,11 +150,11 @@ namespace Versandstrasse
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
if (vdok.Logo)
{
Add_Logo(ref loadedDocument, vdok.LogoColor, vdok.LogoLeft, vdok.LogoTop, vdok.LogoWidth, vdok.LogoHeigth, vdok.LogoOnPages,this.resturi,this.apikey,startuppath);
Add_Logo(ref loadedDocument, vdok.LogoColor, vdok.LogoLeft, vdok.LogoTop, vdok.LogoWidth, vdok.LogoHeigth, vdok.LogoOnPages, this.resturi, this.apikey, startuppath);
}
if (couvertpos)
{
if (couvertpos)
{
drawcouvert(ref loadedDocument);
}
if (vdok.TextReplaces.Count > 0)
@@ -157,18 +163,48 @@ namespace Versandstrasse
{
if (tr.TextToReplace.Trim() != "")
{
Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText, tr.Fontname, tr.Fontsize);
Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText, tr.Fontname, tr.Fontsize, startuppath);
}
}
//loadedDocument.Save(@"x:\r1.pdf");
}
if (loadedDocument.Pages.Count % 2 == 0) { } else { Add_EmptyPage(ref loadedDocument); }
MemoryStream outputStream = new MemoryStream();
if (loadedDocument.Pages.Count % 2 == 0) { } else { Add_EmptyPage(ref loadedDocument); }
MemoryStream outputStream = new MemoryStream();
try
{
//db.Get_Tabledata("Select * from Ondoc_PDF_CompressionOptions where aktiv=1", false, true);
//if (db.dsdaten.Tables[0].Rows.Count > 0)
//{
// PdfCompressionOptions options = new PdfCompressionOptions();
// options.CompressImages = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0][2]);
// options.ImageQuality = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0][3]);
// options.OptimizeFont = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0][4]);
// options.OptimizePageContents = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0][5]);
// options.RemoveMetadata = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0][6]);
// loadedDocument.CompressionOptions = options;
//}
loadedDocument.Save(outputStream);
//outputStream.Position = 0;
//outputStream.WriteTo(filestream);
//loadedDocument.Close(true);
//loadedDocument = new PdfLoadedDocument(filename);
//loadedDocument.ConvertToPDFA(PdfConformanceLevel.Pdf_A1B);
//loadedDocument.Save(outputStream);
//filestream.Close();
byte[] bytes;
bytes = outputStream.ToArray();
vdok.pdfdokument = Convert.ToBase64String(bytes);
}
catch
{
}
}
if (vscript.dokuments.Count > 1)
{
@@ -180,12 +216,47 @@ namespace Versandstrasse
var stream = new MemoryStream(Convert.FromBase64String(vdoc.pdfdokument));
streams[i] = stream;
i++;
}
PdfDocument finalDoc = new PdfDocument();
PdfDocumentBase.Merge(finalDoc, streams);
MemoryStream outputStream = new MemoryStream();
finalDoc.Save(outputStream);
string filename = tempdir + Guid.NewGuid().ToString() + ".pdf";
//FileStream filestream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//finalDoc.Save(filestream,;
finalDoc.Save(filename);
//filestream.Close();
PdfLoadedDocument loadeddoc = new PdfLoadedDocument(filename);
//db.Get_Tabledata("Select * from Ondoc_PDF_CompressionOptions where aktiv=1", false, true);
//if (db.dsdaten.Tables[0].Rows.Count > 0)
//{
// PdfCompressionOptions options = new PdfCompressionOptions();
// options.CompressImages = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0][2]);
// options.ImageQuality = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0][3]);
// options.OptimizeFont = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0][4]);
// options.OptimizePageContents = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0][5]);
// options.RemoveMetadata = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0][6]);
// loadeddoc.CompressionOptions = options;
//}
loadeddoc.ConvertToPDFA(PdfConformanceLevel.Pdf_A1B);
loadeddoc.Save(outputStream);
System.IO.File.Delete(filename);
//loadedDocument = new PdfLoadedDocument(filename);
//loadedDocument.ConvertToPDFA(PdfConformanceLevel.Pdf_A1B);
//loadedDocument.Save(outputStream);
//finalDoc.Save(outputStream);
byte[] bytes;
bytes = outputStream.ToArray();
dok.dokument = Convert.ToBase64String(bytes);
@@ -197,7 +268,7 @@ namespace Versandstrasse
}
else
{
vscript.FinalDoc = vscript.dokuments[0].pdfdokument.ToString();
vscript.FinalDoc = vscript.dokuments[0].pdfdokument.ToString();
}
@@ -222,28 +293,27 @@ namespace Versandstrasse
if (matchedTextbounds[index].Count > 0)
{
newdoc.ImportPage(loadedDocument, index);
newdoc2.ImportPageRange(loadedDocument, index + 1, loadedDocument.Pages.Count - (index+1));
newdoc2.ImportPageRange(loadedDocument, index + 1, loadedDocument.Pages.Count - (index + 1));
}
}
//newdoc.Save(@"x:\nf1.pdf");
//newdoc2.Save(@"x:\nf2.pdf");
MemoryStream outputStream = new MemoryStream();
newdoc.Save(outputStream);
byte[] bytes;
byte[] bytes1;
bytes = outputStream.ToArray();
vscript.dokuments[0].sourcedokument = Convert.ToBase64String(bytes);
outputStream.Close();
outputStream = null;
MemoryStream outputStream2 = new MemoryStream();
newdoc2.Save(outputStream2);
bytes1 = outputStream2.ToArray();
vscript.dokuments[1].sourcedokument = Convert.ToBase64String(bytes1);
outputStream2.Close();
outputStream2 = null;
@@ -259,7 +329,7 @@ namespace Versandstrasse
//if (vsd.LogoHeigth == 0) { vsd.LogoHeigth = height; }
//if (vsd.LogoWidth == 0) { vsd.LogoWidth = width; }
//vsd.sourcedokument = dokument;
if (vscript.dokuments[0].sourcedokument=="") { vscript.dokuments[0].sourcedokument=dokument; }
if (vscript.dokuments[0].sourcedokument == "") { vscript.dokuments[0].sourcedokument = dokument; }
int i = 1;
List<String> copyparts = vscript.PartCopies.Split(';').ToList();
foreach (string cp in copyparts)
@@ -267,23 +337,107 @@ namespace Versandstrasse
string[] part = cp.Split(':');
int part1 = Convert.ToInt32(part[0]);
int part2 = Convert.ToInt32(part[1]);
vscript.dokuments[part2-1].sourcedokument = vscript.dokuments[part1-1].sourcedokument;
vscript.dokuments[part2 - 1].sourcedokument = vscript.dokuments[part1 - 1].sourcedokument;
//vsdokument source = vscript.dokuments[part1 - 1];
//if (vscript.dokuments.Count > part2) { vscript.dokuments[i] = source; } else { vscript.dokuments.Add(source); }
}
}
private void Replace_Text(ref PdfLoadedDocument document, string texttoreplace, string newtext, string fontname, string fontsize)
private Syncfusion.Pdf.Graphics.Fonts.PdfUsedFont get_font(ref PdfLoadedDocument document)
{
PdfUsedFont[] usedFonts = document.UsedFonts;
foreach (PdfUsedFont font in usedFonts)
{
if (font.Name.Contains("Futura"))
{
return font;
}
}
return null;
}
private PdfUsedFont get_fontname(ref PdfLoadedDocument document)
{
PdfUsedFont[] usedFonts = document.UsedFonts;
// Iterate through the fonts and check if they are embedded
foreach (PdfUsedFont font in usedFonts)
{
if (font.Name.Contains("Futura")) { return font; }
//if (font.Type == PdfFontType.TrueType)
//{
// if (font.Name.Contains("Futura")) { return font; }
// break;
// Console.WriteLine($"Font '{font.Name}' is embedded.");
//}
//else
//{
// return font;
// Console.WriteLine($"Font '{font.Name}' is not embedded.");
//}
}
return null;
}
private void Replace_Text(ref PdfLoadedDocument document, string texttoreplace, string newtext, string fontname, string fontsize, string startuppath)
{
//string imagefile = "";
//string texttol = newtext.Trim().Replace(" ", "_");
//texttol = texttol + ".jpg";
//imagefile = startuppath + @"\images\"+texttol;
//PdfBitmap image;
//if (System.IO.File.Exists(imagefile))
//{
// System.Drawing.Image img = System.Drawing.Image.FromFile(imagefile);
// img = ResizeImage(img, new Size(86, 12));
// image = new PdfBitmap(img);
// PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage;
// Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
// document.FindText(texttoreplace, out matchedTextbounds);
// List<RectangleF> imachedrect = new List<RectangleF>();
// int ipageindex = 0;
// for (int i = 0; i < document.Pages.Count; i++)
// {
// document.FindText(texttoreplace, ipageindex, out imachedrect);
// loadedPage = (PdfLoadedPage)document.Pages[i];
// foreach (RectangleF rectangle in matchedTextbounds[i])
// {
// RectangleF rect = rectangle;
// rect.X = rect.X - 5;
// rect.Y = rect.Y - 5;
// rect.Height = rect.Height + 5;
// loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rectangle);
// loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rect);
// PdfGraphics graphics = loadedPage.Graphics;
// graphics.DrawImage(image, rect.X, rect.Y);
// }
// }
//}
//else
//{
PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage;
string fname = "Futura Book";
string fsize = "9";
string fname = "Futura";
string fsize = "8";
if (fontname != "")
{
fname = fontname;
fsize = fontsize;
}
PdfUsedFont usedfont = get_fontname(ref document);
PdfFont font = new PdfTrueTypeFont(new Font(fname, Convert.ToInt32(fsize)));
PdfFont fnt = new PdfStandardFont(PdfFontFamily.Helvetica, 8);
Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
document.FindText(texttoreplace, out matchedTextbounds);
@@ -303,42 +457,34 @@ namespace Versandstrasse
rect.Height = rect.Height + 5;
loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rectangle);
loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rect);
loadedPage.Graphics.DrawString(newtext, font, PdfBrushes.Black, rectangle.X - 2, rectangle.Y - 3);
loadedPage.Graphics.DrawString(newtext, fnt, PdfBrushes.Black, rectangle.X - 2, rectangle.Y - 3);
//if (newtext != "")
//{
// System.Drawing.Image img = DrawText(System.Drawing.Color.Black, System.Drawing.Color.White, "Futura Book", 14, newtext, (int)rect.Width, (int)rect.Height, "", "", 0, 0);
// PdfGraphics graphics = loadedPage.Graphics;
// PdfBitmap pdfimg = new PdfBitmap(img);
// graphics.DrawImage(pdfimg, rect.X, rect.Y);
//}
}
}
//for (int i = 0; i < matchedTextbounds.Count; i++)
//{
// for (int i1 = 0; i1 < document.Pages.Count; i1++)
// {
// loadedPage = (PdfLoadedPage)document.Pages[i1];
// foreach (RectangleF rectangle in matchedTextbounds[i])
// {
// RectangleF rect = rectangle;
// rect.X = rect.X - 5;
// rect.Y = rect.Y - 5;
// rect.Height = rect.Height + 5;
// loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rectangle);
// loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rect);
// loadedPage.Graphics.DrawString(newtext, font, PdfBrushes.Black, rectangle.X - 2, rectangle.Y - 3);
// }
// }
//}
}
private string get_logo(bool color, string resturi, string apikey)
{
int imageid = 0;
if (color) { imageid = 4; } else { imageid = 5; }
return get_image(imageid,-1,-1,resturi,apikey);
return get_image(imageid, -1, -1, resturi, apikey);
}
private string get_image(int imageid, int width, int heigh, string resturi, string apikey)
{
@@ -427,7 +573,7 @@ namespace Versandstrasse
{
PdfLoadedPage loadedPage = document.Pages[i] as PdfLoadedPage;
PdfGraphics graphics = loadedPage.Graphics;
graphics.DrawImage(image,left,top);
graphics.DrawImage(image, left, top);
}
}
@@ -452,12 +598,130 @@ namespace Versandstrasse
document.Pages.Add();
}
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)
{
//width = 320;
//height = 50;
string barcodetext = txt;
barcodetext = txt;
Bitmap img = new Bitmap(width, height);
Graphics Gimg = Graphics.FromImage(img);
Font imgFont = new Font(fontName, fontSize);
PointF imgPoint = new PointF(0, 0);
SolidBrush bForeColor = new SolidBrush(foreColor);
SolidBrush bBackColor = new SolidBrush(backColor);
Gimg.FillRectangle(bBackColor, 0, 0, width, height);
Gimg.DrawString(barcodetext, imgFont, bForeColor, imgPoint);
//Font fzFont = new Font(zusatzfontname, zusatzfontsize);
//SizeF textSize = Gimg.MeasureString(zusatz, fzFont, 30);
//PointF imgPointZusatz = new PointF(width - textSize.Width, 25);
//Gimg.DrawString(zusatz, fzFont, bForeColor, imgPointZusatz);
return RotateImage(img, rotation, true, true, Color.White);
//img.Save(imagePath, ImageFormat.Jpeg);
}
private static System.Drawing.Image ResizeImage(System.Drawing.Image imgToResize, Size size)
{
// Get the image current width
int sourceWidth = imgToResize.Width;
// Get the image current height
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
// Calculate width and height with new desired size
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
nPercent = Math.Min(nPercentW, nPercentH);
// New Width and Height
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw image with new width and height
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (System.Drawing.Image)b;
}
public static Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsizeOk,
bool clipOk, Color backgroundColor)
{
// Test for zero rotation and return a clone of the input image
if (angleDegrees == 0f)
return (Bitmap)inputImage.Clone();
// Set up old and new image dimensions, assuming upsizing not wanted and clipping OK
int oldWidth = inputImage.Width;
int oldHeight = inputImage.Height;
int newWidth = oldWidth;
int newHeight = oldHeight;
float scaleFactor = 1f;
// If upsizing wanted or clipping not OK calculate the size of the resulting bitmap
if (upsizeOk || !clipOk)
{
double angleRadians = angleDegrees * Math.PI / 180d;
double cos = Math.Abs(Math.Cos(angleRadians));
double sin = Math.Abs(Math.Sin(angleRadians));
newWidth = (int)Math.Round(oldWidth * cos + oldHeight * sin);
newHeight = (int)Math.Round(oldWidth * sin + oldHeight * cos);
}
// If upsizing not wanted and clipping not OK need a scaling factor
if (!upsizeOk && !clipOk)
{
scaleFactor = Math.Min((float)oldWidth / newWidth, (float)oldHeight / newHeight);
newWidth = oldWidth;
newHeight = oldHeight;
}
// Create the new bitmap object. If background color is transparent it must be 32-bit,
// otherwise 24-bit is good enough.
Bitmap newBitmap = new Bitmap(newWidth, newHeight, backgroundColor == Color.Transparent ?
PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb);
newBitmap.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution);
// Create the Graphics object that does the work
using (Graphics graphicsObject = Graphics.FromImage(newBitmap))
{
graphicsObject.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsObject.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphicsObject.SmoothingMode = SmoothingMode.HighQuality;
// Fill in the specified background color if necessary
if (backgroundColor != Color.Transparent)
graphicsObject.Clear(backgroundColor);
// Set up the built-in transformation matrix to do the rotation and maybe scaling
graphicsObject.TranslateTransform(newWidth / 2f, newHeight / 2f);
if (scaleFactor != 1f)
graphicsObject.ScaleTransform(scaleFactor, scaleFactor);
graphicsObject.RotateTransform(angleDegrees);
graphicsObject.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f);
// Draw the result
graphicsObject.DrawImage(inputImage, 0, 0);
}
return newBitmap;
}
}
public class versandscript
{
public string bezeichnung { get; set; } = "Versandstrassenparaemter";
public int dokumenttypnr { get; set; } = 0;
public string Split_Doc { get; set; } = "";
public string PartCopies { get; set; } = "";