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.

163 lines
5.9 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ZZPDFTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string lickey = "MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ==";
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(lickey);
PdfLoadedDocument document = new PdfLoadedDocument(@"H:\tssettings\edoka\drv.pdf");
add_Logo(ref document, false,false);
replace_Text(ref document, "Bausteine", "");
//replace_Text(ref document, "Herr", Environment.NewLine+Environment.NewLine+ "Herr");
//replace_Text(ref document, "Christoph Nõgeli", "Christoph Nõgeli");
if (document.Pages.Count % 2 == 0)
{
}
else
{
add_emptypage(ref document);
}
document.Save(@"x:\output.pdf");
//PdfBitmap image = new PdfBitmap(@"E:\Software-Projekte\OnDoc\Images\tkb_logo_ohne_claim_4c_c.jpg");
//PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage;
//PdfGraphics graphics = loadedPage.Graphics;
//graphics.DrawImage(image, 80, 30);
}
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])
{
// Draws the white filled rectangle over the searched text.
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, bool color, bool onlyfirstpage)
{
string imagefile ="";
if (color) { imagefile = @"E:\Software-Projekte\OnDoc\Images\tkb_logo_ohne_claim_4c_c.jpg"; }
if (!color) { imagefile = @"E:\Software-Projekte\OnDoc\Images\tkb_logo_ohne_claim_sw.jpg"; }
PdfBitmap image = new PdfBitmap(imagefile);
int pages = document.Pages.Count;
if (onlyfirstpage) { pages = 1; }
int i = 0;
while (i < pages)
{
PdfLoadedPage loadedPage = document.Pages[i] as PdfLoadedPage;
PdfGraphics graphics = loadedPage.Graphics;
graphics.DrawImage(image, 85, 30);
i++;
}
}
private void add_emptypage(ref PdfLoadedDocument document)
{
document.Pages.Add();
}
private void button2_Click(object sender, EventArgs e)
{
string lickey = "MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ==";
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(lickey);
PdfLoadedDocument document = new PdfLoadedDocument(@"H:\tssettings\edoka\drv.pdf");
//add_Logo(ref document, false, false);
SplitDoc(ref document, "Freundliche Grüsse", "");
}
private void SplitDoc(ref PdfLoadedDocument document, string texttoreplace, string newtext)
{
PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage;
PdfFont font = new PdfTrueTypeFont(new Font("Futura Book", 9));
TextSearchOptions to = new TextSearchOptions();
Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
document.FindText(texttoreplace, out matchedTextbounds);
PdfDocument newdoc = new PdfDocument();
PdfDocument newdoc2 = new PdfDocument();
PdfDocument newdoc3 = new PdfDocument();
foreach (int index in matchedTextbounds.Keys)
{
if (matchedTextbounds[index].Count > 0)
{
newdoc.ImportPage(document, index);
newdoc2.ImportPageRange(document, index + 1, document.Pages.Count - (index + 1));
newdoc3 = newdoc2;
}
}
newdoc.Save(@"x:\output1.pdf");
newdoc2.Save(@"x:\outupt2.pdf");
newdoc3.Save(@"x:\outupt3.pdf");
PdfLoadedDocument doc = new PdfLoadedDocument (@"x:\output1.pdf");
add_Logo(ref doc, true, true);
if (doc.Pages.Count % 2 == 0)
{
}
else
{
add_emptypage(ref doc);
}
doc.Save(@"x:\output1.pdf");
doc = new PdfLoadedDocument(@"x:\outupt3.pdf");
replace_Text(ref doc, "Exemplar für den Kunden", " Exemplar für die Bank");
doc.Save(@"x:\outupt3.pdf");
document.Close(true);
newdoc.Close(true);
}
}
}