update 20250406

This commit is contained in:
Stefan Hutter
2025-04-06 07:39:53 +02:00
parent 632b97c292
commit 42c86d957c
155 changed files with 1725 additions and 607 deletions

View File

@@ -24,6 +24,21 @@ namespace Versandstrasse
{
public class clsVersandstrasse
{
public bool SaveBase64ToFile(string Base64String, string filename)
{
try
{
System.IO.File.WriteAllBytes(filename, Convert.FromBase64String(Base64String));
return true;
}
catch
{
return false;
}
}
private List<PdfLoadedDocument> loadedDocuments = new List<PdfLoadedDocument>();
public void Lic()
{
@@ -94,6 +109,15 @@ namespace Versandstrasse
{
vscript = (versandscript)(serializer.Deserialize(reader));
}
foreach (vsdokument vsd in vscript.dokuments)
{
}
if (vscript.Split_Doc !="")
{
split_doc(ref vscript,dok.dokument);
}
if (vscript.PartCopies != "")
{
create_copies(ref vscript, dok.dokument);
@@ -108,7 +132,7 @@ namespace Versandstrasse
d.LogoWidth = width;
d.LogoColor = color;
d.Logo = logo;
d.LogoOnPages = "1";
d.LogoOnPages = pages;
d.sourcedokument = dok.dokument;
vscript.dokuments.Add(d);
}
@@ -128,9 +152,10 @@ namespace Versandstrasse
{
if (tr.TextToReplace.Trim() != "")
{
Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText);
Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText, tr.Fontname, tr.Fontsize);
}
}
//loadedDocument.Save(@"x:\r1.pdf");
}
if (loadedDocument.Pages.Count % 2 == 0) { } else { Add_EmptyPage(ref loadedDocument); }
MemoryStream outputStream = new MemoryStream();
@@ -171,20 +196,64 @@ namespace Versandstrasse
}
return vscript.FinalDoc;
}
private void split_doc(ref versandscript vscript, string document)
{
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));
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
TextSearchOptions to = new TextSearchOptions();
Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
loadedDocument.FindText(vscript.Split_Doc, out matchedTextbounds);
PdfDocument newdoc = new PdfDocument();
PdfDocument newdoc2 = new PdfDocument();
foreach (int index in matchedTextbounds.Keys)
{
if (matchedTextbounds[index].Count > 0)
{
newdoc.ImportPage(loadedDocument, index);
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;
//SaveBase64ToFile(vscript.dokuments[0].sourcedokument, @"x:\f1.pdf");
//SaveBase64ToFile(vscript.dokuments[1].sourcedokument, @"x:\f2.pdf");
}
private void create_copies(ref versandscript vscript, string dokument)
{
vsdokument vsd = vscript.dokuments[0];
if (vsd.LogoTop == 0) { vsd.LogoTop = top; }
if (vsd.LogoLeft == 0) { vsd.LogoLeft = left; }
if (vsd.LogoHeigth == 0) { vsd.LogoHeigth = height; }
if (vsd.LogoWidth == 0) { vsd.LogoWidth = width; }
vsd.sourcedokument = dokument;
//vsdokument vsd = vscript.dokuments[0];
//if (vsd.LogoTop == 0) { vsd.LogoTop = top; }
//if (vsd.LogoLeft == 0) { vsd.LogoLeft = left; }
//if (vsd.LogoHeigth == 0) { vsd.LogoHeigth = height; }
//if (vsd.LogoWidth == 0) { vsd.LogoWidth = width; }
//vsd.sourcedokument = dokument;
int i = 1;
List<String> copyparts = vscript.PartCopies.Split(';').ToList();
@@ -193,28 +262,39 @@ namespace Versandstrasse
string[] part = cp.Split(':');
int part1 = Convert.ToInt32(part[0]);
int part2 = Convert.ToInt32(part[1]);
vsdokument source = vscript.dokuments[part1 - 1];
if (vscript.dokuments.Count >= part2) { vscript.dokuments[i] = source; } else { vscript.dokuments.Add(source); }
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)
private void Replace_Text(ref PdfLoadedDocument document, string texttoreplace, string newtext, string fontname, string fontsize)
{
PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage;
PdfFont font = new PdfTrueTypeFont(new Font("Futura Book", 9));
string fname = "Futura Book";
string fsize = "9";
if (fontname != "")
{
fname = fontname;
fsize = fontsize;
}
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);
for (int i = 0; i < matchedTextbounds.Count; i++)
{
loadedPage = (PdfLoadedPage)document.Pages[0];
foreach (RectangleF rectangle in matchedTextbounds[i])
for (int i1 = 0; i1 < document.Pages.Count; i1++)
{
loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rectangle);
loadedPage.Graphics.DrawString(newtext, font, PdfBrushes.Black, rectangle.X - 2, rectangle.Y - 2);
loadedPage = (PdfLoadedPage)document.Pages[i1];
foreach (RectangleF rectangle in matchedTextbounds[i])
{
loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rectangle);
loadedPage.Graphics.DrawString(newtext, font, PdfBrushes.Black, rectangle.X - 2, rectangle.Y - 2);
}
}
}
}
@@ -337,5 +417,8 @@ namespace Versandstrasse
{
public string TextToReplace { get; set; } = "";
public string NewText { get; set; } = "";
public string Fontname { get; set; } = "";
public string Fontsize { get; set; } = "";
}
}