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.

140 lines
4.8 KiB

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using edoka_dms;
using Model;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
namespace Versandstrasse
{
public class clsVersandstrasse
{
public void Lic()
{
string lickey = "MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ==";
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(lickey);
}
public string Prepare_PDF(string dokumentid, string tempdir, string connectionstring)
{
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));
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);
int logo = 0;
int color = 0;
int left = 0;
int top = 0;
string pages = "";
logo = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logo"]);
color = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logocolor"]);
left = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logoleft"]);
top = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logocolor"]);
pages = db.dsdaten.Tables[0].Rows[0]["pages"].ToString();
if (logo == 1)
{
Add_Logo(ref loadedDocument, color, left, top, pages);
}
if (loadedDocument.Pages.Count % 2 == 0)
{
}
else
{
Add_EmptyPage(ref loadedDocument);
}
MemoryStream outputStream = new MemoryStream();
loadedDocument.Save(outputStream);
byte[] bytes;
bytes = outputStream.ToArray();
dok.dokument = Convert.ToBase64String(bytes);
return dok.dokument;
}
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[i];
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 void Add_Logo(ref PdfLoadedDocument document, int color, int left, int top, string pages)
{
string imagefile = "";
imagefile = @"E:\Software-Projekte\OnDoc\Images\tkb_logo_ohne_claim_4c_c.jpg";
if (color == 0) { imagefile = @"E:\Software-Projekte\OnDoc\Images\tkb_logo_ohne_claim_sw.jpg"; }
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();
}
}
}