update 20251210

This commit is contained in:
Stefan Hutter
2025-12-10 17:42:12 +01:00
parent 10ed1e6087
commit 6ac2108d40
303 changed files with 2207203 additions and 1040 deletions
+64 -107
View File
@@ -87,6 +87,7 @@ namespace Versandstrasse
public void writelog(string inhalt)
{
// inhalt = DateTime.Now + " / " + inhalt;
//System.IO.File.AppendAllText(@"h:\edoka_work\vs.log", Environment.NewLine + inhalt);
}
@@ -97,6 +98,7 @@ namespace Versandstrasse
if (loadedDocument.Pages.Count % 2 == 0) { } else { Add_EmptyPage(ref loadedDocument); }
MemoryStream outputStream = new MemoryStream();
string dok = "";
try
{
loadedDocument.Save(outputStream);
@@ -117,15 +119,18 @@ namespace Versandstrasse
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
{
writelog("Start");
Lic();
this.resturi = resturi;
this.apikey = apikey;
clsdok dok = new clsdok("", "", "", "");
writelog("Start");
if (sbsourcedok != "")
{
@@ -206,24 +211,31 @@ namespace Versandstrasse
foreach (vsdokument vdok in vscript.dokuments)
{
var stream = new MemoryStream(Convert.FromBase64String(vdok.sourcedokument));
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);
}
if (couvertpos)
{
writelog("DrawCouvert Start");
drawcouvert(ref loadedDocument);
writelog("DrawCouvert Ende");
}
if (vdok.TextReplaces.Count > 0)
{
foreach (TextReplace tr in vdok.TextReplaces)
{
if (tr.TextToReplace.Trim() != "")
{
Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText, tr.Fontname, tr.Fontsize, startuppath);
writelog("Replace Start " + tr.TextToReplace);
Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText, tr.Fontname, tr.Fontsize, startuppath,tr);
writelog("Replace Ende");
}
}
//loadedDocument.Save(@"x:\r1.pdf");
@@ -233,27 +245,11 @@ namespace Versandstrasse
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();
@@ -266,6 +262,7 @@ namespace Versandstrasse
}
}
writelog("Merge Start");
if (vscript.dokuments.Count > 1)
{
int i = 0;
@@ -283,40 +280,16 @@ namespace Versandstrasse
PdfDocumentBase.Merge(finalDoc, streams);
MemoryStream outputStream = new MemoryStream();
string filename = tempdir + Guid.NewGuid().ToString() + ".pdf";
//FileStream filestream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//finalDoc.Save(filestream,;
finalDoc.Save(outputStream);
//filestream.Close();
PdfLoadedDocument loadeddoc = new PdfLoadedDocument(outputStream);
//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();
@@ -333,7 +306,7 @@ namespace Versandstrasse
vscript.FinalDoc = vscript.dokuments[0].pdfdokument.ToString();
}
writelog("Merge Ende");
return vscript.FinalDoc;
}
catch (Exception ex) { writelog("2" + ex.Message);return ""; }
@@ -341,7 +314,8 @@ namespace Versandstrasse
private void split_doc(ref versandscript vscript, string document)
{
//SaveBase64ToFile(document, @"x:\f0.pdf");
writelog("Split");
; //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));
@@ -392,6 +366,7 @@ namespace Versandstrasse
//if (vsd.LogoHeigth == 0) { vsd.LogoHeigth = height; }
//if (vsd.LogoWidth == 0) { vsd.LogoWidth = width; }
//vsd.sourcedokument = dokument;
writelog("Copies");
if (vscript.dokuments[0].sourcedokument == "") { vscript.dokuments[0].sourcedokument = dokument; }
int i = 1;
List<String> copyparts = vscript.PartCopies.Split(';').ToList();
@@ -419,14 +394,14 @@ namespace Versandstrasse
}
return null;
}
private PdfUsedFont get_fontname(ref PdfLoadedDocument document)
private PdfUsedFont get_fontname(ref PdfLoadedDocument document, string fontname)
{
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.Name.Contains(fontname)) { return font; }
//if (font.Type == PdfFontType.TrueType)
//{
@@ -442,68 +417,43 @@ namespace Versandstrasse
}
return null;
}
private void Replace_Text(ref PdfLoadedDocument document, string texttoreplace, string newtext, string fontname, string fontsize, string startuppath)
private void Replace_Text(ref PdfLoadedDocument document, string texttoreplace, string newtext, string fontname, string fontsize, string startuppath, TextReplace tr)
{
//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;
//Dimenssions
int x = -5;
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); }
//Font und Size
string fname = "Futura";
string fsize = "8";
if (fontname != "")
{
fname = fontname;
fsize = fontsize;
}
PdfUsedFont usedfont = get_fontname(ref document);
if (tr.Fontname != "") { fname = tr.Fontname; }
if (tr.Fontsize != "") { fsize = tr.Fontsize; }
// PdfUsedFont usedfont = get_fontname(ref document, fname);
PdfFont font = new PdfTrueTypeFont(new Font(fname, Convert.ToInt32(fsize)));
//PdfFont fnt = new PdfStandardFont(PdfFontFamily.TimesRoman, 8);
Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
document.FindText(texttoreplace, out matchedTextbounds);
PdfSolidBrush brush = new PdfSolidBrush(Color.White);
try
{
if (tr.BGColor != "")
{
brush = new PdfSolidBrush(Color.FromName(tr.BGColor));
}
}
catch { }
List<RectangleF> machedrect = new List<RectangleF>();
int pageindex = 0;
@@ -515,11 +465,13 @@ namespace Versandstrasse
{
RectangleF rect = rectangle;
rect.X = rect.X - 5;
rect.Y = rect.Y - 5;
rect.Height = rect.Height + 5;
rect.X = rect.X + x;
rect.Y = rect.Y + y;
rect.Width = rect.Width + w;
rect.Height = rect.Height + h;
loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rectangle);
loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rect);
loadedPage.Graphics.DrawRectangle(brush, rect);
loadedPage.Graphics.DrawString(newtext, font, PdfBrushes.Black, rectangle.X - 2, rectangle.Y - 3);
//if (newtext != "")
//{
@@ -816,5 +768,10 @@ namespace Versandstrasse
public string Fontname { get; set; } = "";
public string Fontsize { get; set; } = "";
public string BGColor { get; set; } = "";
public string Boxpos_X { get; set; } = "";
public string Boxpos_Y { get; set; } = "";
public string Boxpos_W { get; set; } = "";
public string Boxpos_H { get; set; } = "";
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.