You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
10 KiB

using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using edoka_dms;
using Model;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
namespace Versandstrasse
{
public class clsVersandstrasse
{
private List<PdfLoadedDocument> loadedDocuments = new List<PdfLoadedDocument>();
public void Lic()
{
string lickey = "MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ==";
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(lickey);
}
bool logo = false;
bool color = false;
int left = 0;
int top = 0;
int height = 0;
int width = 0;
string pages = "";
string xml = "";
public string Prepare_PDF(string dokumentid, string tempdir, string connectionstring, versandscript vs = null)
{
Lic();
OnDocOffice.OfficeToPDF officeToPDF = new OnDocOffice.OfficeToPDF();
clsdok dok = new clsdok("", "", "", "");
dok.dokument = officeToPDF.word_to_pdf(dokumentid, connectionstring, tempdir);
//var stream = new MemoryStream(Convert.FromBase64String(dok.dokument));
////Originalfile laden
//PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
Database.DB db = new Database.DB(connectionstring);
db.clear_parameter();
db.add_parameter("@dokumentid", dokumentid);
db.Get_Tabledata("ondoc_get_versandstrasse_parameter", true, false);
versandscript vscript = new versandscript();
logo = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0]["logo"]);
color = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0]["logocolor"]);
top = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logotop"]);
left = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logoleft"]);
height = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logoheight"]);
width = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logowidth"]);
pages = db.dsdaten.Tables[0].Rows[0]["pages"].ToString();
xml = db.dsdaten.Tables[0].Rows[0]["vscript"].ToString();
if (xml != "")
{
XmlSerializer serializer = new XmlSerializer(typeof(versandscript));
using (StringReader reader = new StringReader(xml))
{
vscript = (versandscript)(serializer.Deserialize(reader));
}
if (vscript.PartCopies != "")
{
create_copies(ref vscript, dok.dokument);
}
}
else
{
vsdokument d = new vsdokument();
d.LogoTop = top;
d.LogoLeft = left;
d.LogoHeigth = height;
d.LogoWidth = width;
d.LogoColor = color;
d.Logo = logo;
d.LogoOnPages = "1";
d.sourcedokument = dok.dokument;
vscript.dokuments.Add(d);
}
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);
}
loadedDocument.Save(@"x:\output1.pdf");
if (vdok.TextReplaces.Count > 0)
{
foreach (TextReplace tr in vdok.TextReplaces)
{
if (tr.TextToReplace.Trim() != "")
{
Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText);
}
}
}
if (loadedDocument.Pages.Count % 2 == 0) { } else { Add_EmptyPage(ref loadedDocument); }
MemoryStream outputStream = new MemoryStream();
loadedDocument.Save(outputStream);
byte[] bytes;
bytes = outputStream.ToArray();
vdok.pdfdokument = Convert.ToBase64String(bytes);
}
if (vscript.dokuments.Count > 1)
{
int i = 0;
Stream[] streams = new Stream[vscript.dokuments.Count];
foreach (vsdokument vdoc in vscript.dokuments)
{
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);
byte[] bytes;
bytes = outputStream.ToArray();
dok.dokument = Convert.ToBase64String(bytes);
vscript.FinalDoc = Convert.ToBase64String(bytes);
finalDoc.Close();
finalDoc.Dispose();
streams = null;
vscript.FinalDoc = vscript.FinalDoc;
}
else
{
vscript.FinalDoc = vscript.dokuments[0].pdfdokument.ToString();
}
return vscript.FinalDoc;
}
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;
int i = 1;
List<String> copyparts = vscript.PartCopies.Split(';').ToList();
foreach (string cp in copyparts)
{
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); }
}
}
private void Replace_Text(ref PdfLoadedDocument document, string texttoreplace, string newtext)
{
PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage;
PdfFont font = new PdfTrueTypeFont(new Font("Futura Book", 9));
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])
{
loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rectangle);
loadedPage.Graphics.DrawString(newtext, font, PdfBrushes.Black, rectangle.X - 2, rectangle.Y - 2);
}
}
}
private string get_logo(bool color)
{
if (color) { return @"E:\Software-Projekte\OnDoc\Images\tkb_logo_ohne_claim_sw.jpg"; } else { return @"E:\Software-Projekte\OnDoc\Images\tkb_logo_ohne_claim_4c_c.jpg"; }
}
private void Add_Logo(ref PdfLoadedDocument document, bool color, int left, int top, int width, int height, string pages)
{
string imagefile = "";
imagefile = get_logo(color);
PdfBitmap image = new PdfBitmap(imagefile);
if (pages == "0") { return; }
if (pages == "All")
{
for (int i = 0; i < document.Pages.Count; i++)
{
PdfLoadedPage loadedPage = document.Pages[i] as PdfLoadedPage;
PdfGraphics graphics = loadedPage.Graphics;
graphics.DrawImage(image, 85, 30);
}
}
else
{
int docpages = document.Pages.Count;
string[] page = pages.Split(';');
foreach (var p in page)
{
int pageno = Convert.ToInt32(p) - 1;
PdfLoadedPage loadedPage = document.Pages[pageno] as PdfLoadedPage;
PdfGraphics graphics = loadedPage.Graphics;
graphics.DrawImage(image, 85, 30);
}
}
}
private void Add_EmptyPage(ref PdfLoadedDocument document)
{
document.Pages.Add();
}
}
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; } = "";
public string FinalDoc { get; set; } = "";
public List<vsdokument> dokuments { get; set; } = new List<vsdokument>();
}
public class vsdokument
{
public bool Logo { get; set; } = true;
public bool LogoColor { get; set; } = false;
public int LogoTop { get; set; } = 0;
public int LogoLeft { get; set; } = 0;
public int LogoHeigth { get; set; } = 0;
public int LogoWidth { get; set; } = 0;
public string LogoOnPages { get; set; } = "1";
public string sourcedokument { get; set; } = "";
public string pdfdokument { get; set; } = "";
public List<TextReplace> TextReplaces { get; set; } = new List<TextReplace>();
}
public class TextReplace
{
public string TextToReplace { get; set; } = "";
public string NewText { get; set; } = "";
}
}