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.

560 lines
24 KiB

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Shape = Syncfusion.DocIO.DLS.Shape;
using System.IO;
using BarcodeLib;
using System.Xml.Linq;
using Microsoft.Office.Core;
namespace ZZ_BarcpdeFromFont
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string barcode_font;
public string barcode_fontsize;
public string zusatz_font;
public string zusatz_size;
public string width;
public string height;
public string top;
public string left;
public string imagewith;
public string imageheight;
public string inhalt;
public string zusatz;
public string rotation;
private void button1_Click(object sender, EventArgs e)
{
string inp = Bar25I(this.textBox1.Text);
//Font f = new Font("Bar 25i c", 46);
BarcodeLib.clsBarI25 barbarcode = new BarcodeLib.clsBarI25();
BarcodeLib.Barcode Barcode = new BarcodeLib.Barcode();
barcode_font = textBox7.Text;
barcode_fontsize = textBox12.Text;
zusatz_font = textBox8.Text;
zusatz_size = textBox13.Text;
width = textBox4.Text;
height = textBox5.Text;
top = textBox2.Text;
left = textBox3.Text;
imagewith = textBox10.Text;
imageheight = textBox11.Text;
inhalt = textBox1.Text;
zusatz = textBox9.Text;
rotation = textBox6.Text;
// pictureBox1.Image = barbarcode.GetBarI25(Color.Black, Color.White, inp, "Bar 25i c HR", 36, 350, 50, " U", "Futur Book", 8,0);
System.Drawing.Image barcodeimage;
barcodeimage = barbarcode.GetBarI25(Color.Black, Color.White, inhalt, barcode_font, Convert.ToInt32(barcode_fontsize), Convert.ToInt32(imagewith), Convert.ToInt32(imageheight), zusatz, zusatz_font, Convert.ToInt32(zusatz_size), Convert.ToInt32(rotation));
//barcodeimage = ScaleImage(barcodeimage, Convert.ToInt32(this.imageheight));
// barcodeimage = ScaleImage(barcodeimage, 200, 33);
//barcodeimage = Barcode.Get_LinerBarcode(Barcoded.Symbology.I2of5, inhalt,inhalt + " "+zusatz,"Below", zusatz_font, Convert.ToInt32(zusatz_size),Convert.ToInt32(rotation));
//barcodeimage = Barcode.Get_LinerBarcode(Barcoded.Symbology.I2of5, inhalt, inhalt + " " + zusatz, "Below", zusatz_font, Convert.ToInt32(zusatz_size), Convert.ToInt32(rotation));
barcodeimage = Barcode.Get_LinerBarcode(Barcoded.Symbology.I2of5C ,inhalt, inhalt + " " + zusatz, "Below", zusatz_font, Convert.ToInt32(zusatz_size), Convert.ToInt32(rotation));
// barcodeimage = AutoSizeImage(barcodeimage, Convert.ToInt32(width), Convert.ToInt32(height),false);
barcodeimage = ScaleImage(barcodeimage, 50);
pictureBox1.Image = barcodeimage;
pictureBox1.Image.Save(@"x:\gaga.png");
//pictureBox1.Image= DrawText(inp, f, Color.Black, 140,textBox1.Text+ " U");
//pictureBox1.Image = DrawText1(Color.Black, Color.White, "Bar 25i c HR", 36, inp, 350, 50, " U",0);
}
public string Bar25I(string BarTextIn)
{
string Bar25IRet = default;
string BarTextOut = "";
string TempString = "";
long CharValue = 0;
string barcodeout = "";
// Initialize input and output strings
BarTextOut = "";
BarTextIn = BarTextIn.Trim();
// Throw away non-numeric data
TempString = "";
for (int II = 1, loopTo = BarTextIn.Length; II <= loopTo; II++)
{
if (IsNumeric(BarTextIn.Substring(II - 1, 1)))
{
TempString = TempString + BarTextIn.Substring(II - 1, 1);
}
}
// If not an even number of digits, add a leading 0
if (TempString.Length % 2 == 1)
{
TempString = "0" + TempString;
}
// Break digit pairs up and convert to characters- build output string
for (int II = 1, loopTo1 = TempString.Length; II <= loopTo1; II += 2)
{
// Break string into pairs of digits and get value
CharValue = Convert.ToInt32(TempString.Substring(II - 1, 2));
// translate value to ASCII and save in BarTextOut
if (CharValue < 90)
{
BarTextOut = BarTextOut + (char)(CharValue + 33);
}
else
{
BarTextOut = BarTextOut + (char)(CharValue + 71);
}
}
// Build ouput string, trailing space for Windows rasterization bug
barcodeout = "{" + BarTextOut + "} ";
// Return the string
Bar25IRet = barcodeout;
return Bar25IRet;
}
public bool IsNumeric(string value)
{
return value.All(char.IsNumber);
}
//private Image DrawText(String text, Font font, Color textColor, Color backColor)
//{
// //first, create a dummy bitmap just to get a graphics object
// Image img = new Bitmap(1, 1);
// Graphics drawing = Graphics.FromImage(img);
// //measure the string to see how big the image needs to be
// SizeF textSize = drawing.MeasureString(text, font);
// //free up the dummy image and old graphics object
// img.Dispose();
// drawing.Dispose();
// //create a new image of the right size
// img = new Bitmap((int)textSize.Width, (int)textSize.Height);
// drawing = Graphics.FromImage(img);
// //paint the background
// drawing.Clear(backColor);
// //create a brush for the text
// Brush textBrush = new SolidBrush(textColor);
// drawing.DrawString(text, font, textBrush, 0, 0);
// drawing.Save();
// textBrush.Dispose();
// drawing.Dispose();
// return img;
//}
public Image DrawText(String text, System.Drawing.Font font, Color textColor, int maxWidth, String path)
{
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font, maxWidth);
//set the stringformat flags to rtl
StringFormat sf = new StringFormat();
//uncomment the next line for right to left languages
//sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
sf.Trimming = StringTrimming.Word;
//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();
int multi = 4;
//create a new image of the right size
img = new Bitmap((int)textSize.Width * multi, (int)textSize.Height);
drawing = Graphics.FromImage(img);
//Adjust for high quality
drawing.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
drawing.InterpolationMode = InterpolationMode.HighQualityBilinear;
drawing.PixelOffsetMode = PixelOffsetMode.HighQuality;
drawing.SmoothingMode = SmoothingMode.HighQuality;
drawing.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
//paint the background
drawing.Clear(Color.Transparent);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
System.Drawing.Font ftext = new System.Drawing.Font("Arial", 15, FontStyle.Bold);
drawing.DrawString(text, font, textBrush, new RectangleF(0, 0, textSize.Width * multi, textSize.Height), sf);
drawing.DrawString(path, ftext, textBrush, new RectangleF(50, 50, textSize.Width * multi, textSize.Height), sf);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
img.Save(path, System.Drawing.Imaging.ImageFormat.Png);
img.Dispose();
}
public Image DrawText1(Color foreColor, Color backColor, string fontName, int fontSize, string txt, int width, int height, string zusatz, int rotation)
{
Bitmap img = new Bitmap(width, height);
Graphics Gimg = Graphics.FromImage(img);
System.Drawing.Font imgFont = new System.Drawing.Font(fontName, fontSize);
PointF imgPoint = new PointF(5, 5);
SolidBrush bForeColor = new SolidBrush(foreColor);
SolidBrush bBackColor = new SolidBrush(backColor);
Gimg.FillRectangle(bBackColor, 0, 0, width, height);
Gimg.DrawString(txt, imgFont, bForeColor, imgPoint);
System.Drawing.Font fzFont = new System.Drawing.Font("Arial", 9);
SizeF textSize = Gimg.MeasureString(zusatz, fzFont, 50);
PointF imgPointZusatz = new PointF(width - textSize.Width - 15, 30);
Gimg.DrawString(zusatz, fzFont, bForeColor, imgPointZusatz);
return RotateImage(img, rotation, true, true, Color.White);
//img.Save(imagePath, ImageFormat.Jpeg);
}
public static Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsizeOk,
bool clipOk, Color backgroundColor)
{
// Test for zero rotation and return a clone of the input image
if (angleDegrees == 0f)
return (Bitmap)inputImage.Clone();
// Set up old and new image dimensions, assuming upsizing not wanted and clipping OK
int oldWidth = inputImage.Width;
int oldHeight = inputImage.Height;
int newWidth = oldWidth;
int newHeight = oldHeight;
float scaleFactor = 1f;
// If upsizing wanted or clipping not OK calculate the size of the resulting bitmap
if (upsizeOk || !clipOk)
{
double angleRadians = angleDegrees * Math.PI / 180d;
double cos = Math.Abs(Math.Cos(angleRadians));
double sin = Math.Abs(Math.Sin(angleRadians));
newWidth = (int)Math.Round(oldWidth * cos + oldHeight * sin);
newHeight = (int)Math.Round(oldWidth * sin + oldHeight * cos);
}
// If upsizing not wanted and clipping not OK need a scaling factor
if (!upsizeOk && !clipOk)
{
scaleFactor = Math.Min((float)oldWidth / newWidth, (float)oldHeight / newHeight);
newWidth = oldWidth;
newHeight = oldHeight;
}
// Create the new bitmap object. If background color is transparent it must be 32-bit,
// otherwise 24-bit is good enough.
Bitmap newBitmap = new Bitmap(newWidth, newHeight, backgroundColor == Color.Transparent ?
PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb);
newBitmap.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution);
// Create the Graphics object that does the work
using (Graphics graphicsObject = Graphics.FromImage(newBitmap))
{
graphicsObject.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsObject.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphicsObject.SmoothingMode = SmoothingMode.HighQuality;
// Fill in the specified background color if necessary
if (backgroundColor != Color.Transparent)
graphicsObject.Clear(backgroundColor);
// Set up the built-in transformation matrix to do the rotation and maybe scaling
graphicsObject.TranslateTransform(newWidth / 2f, newHeight / 2f);
if (scaleFactor != 1f)
graphicsObject.ScaleTransform(scaleFactor, scaleFactor);
graphicsObject.RotateTransform(angleDegrees);
graphicsObject.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f);
// Draw the result
graphicsObject.DrawImage(inputImage, 0, 0);
}
return newBitmap;
}
public static Image RotateImage1(Image img, float rotationAngle)
{
//create an empty Bitmap image
Bitmap bmp = new Bitmap(img.Width, img.Height);
//turn the Bitmap into a Graphics object
Graphics gfx = Graphics.FromImage(bmp);
//now we set the rotation point to the center of our image
gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
//now rotate the image
gfx.RotateTransform(rotationAngle);
gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
//set the InterpolationMode to HighQualityBicubic so to ensure a high
//quality image once it is transformed to the specified size
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
//now draw our new image onto the graphics object
gfx.DrawImage(img, new System.Drawing.Point(0, 0));
//dispose of our Graphics object
gfx.Dispose();
//return the image
return bmp;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public System.Drawing.Image AutoSizeImage(System.Drawing.Image oBitmap, int maxWidth, int maxHeight, bool bStretch = false)
{
// Größenverhältnis der max. Dimension
float maxRatio = (float)(maxWidth / (double)maxHeight);
// Bildgröße und aktuelles Größenverhältnis
int imgWidth = oBitmap.Width;
int imgHeight = oBitmap.Height;
float imgRatio = (float)(imgWidth / (double)imgHeight);
// Bild anpassen?
if (imgWidth > maxWidth | imgHeight > maxHeight | bStretch)
{
if (imgRatio <= maxRatio)
{
// Größenverhältnis des Bildes ist kleiner als die
// maximale Größe, in der das Bild angezeigt werden kann.
// In diesem Fall muss die Bildbreite angepasst werden.
imgWidth = (int)Math.Round(imgWidth / (imgHeight / (double)maxHeight));
imgHeight = maxHeight;
}
else
{
// Größenverhältnis des Bildes ist größer als die
// maximale Größe, in der das Bild angezeigt werden kann.
// In diesem Fall muss die Bildhöhe angepasst werden.
imgHeight = (int)Math.Round(imgHeight / (imgWidth / (double)maxWidth));
imgWidth = maxWidth;
}
// Bitmap-Objekt in der neuen Größe erstellen
var oImage = new Bitmap(imgWidth, imgHeight);
// Bild interpolieren, damit die Qualität erhalten bleibt
using (Graphics g = Graphics.FromImage(oImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(oBitmap, new System.Drawing.Rectangle(0, 0, imgWidth, imgHeight));
}
// neues Bitmap zurückgeben
return oImage;
}
else
{
// unverändertes Originalbild zurückgeben
return oBitmap;
}
}
public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth / image.Width;
var ratioY = (double)maxHeight / image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
using (var graphics = Graphics.FromImage(newImage))
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
return newImage;
}
public static Image ScaleImage(Image image, int height)
{
double ratio = (double)height / image.Height;
int newWidth = (int)(image.Width * ratio);
int newHeight = (int)(image.Height * ratio);
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(newImage))
{
g.DrawImage(image, 0, 0, newWidth, newHeight);
}
image.Dispose();
return newImage;
}
private void button3_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
word.Documents.Open(@"E:\Software-Projekte\OnDoc\Excel_Dateien\empty.docx");
FindAndReplaceImages(word.ActiveDocument, @"x:\gaga.png");
word.Visible = true;
}
private void FindAndReplaceImages(Document wordDoc, string imagePath)
{
var sec = wordDoc.Application.Selection.Sections[1];
foreach (Section wordSection in wordDoc.Sections)
{
var footer = sec.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
//var footerImage = footer.Shapes.AddShape(1, 0, 0, 594, 280);
var footerImage = footer.Shapes.AddShape(1, Convert.ToInt32(this.left), Convert.ToInt32(this.top), Convert.ToInt32(this.width), Convert.ToInt32(this.height));
footerImage.Fill.UserPicture(imagePath);
footerImage.WrapFormat.Type = WdWrapType.wdWrapThrough;
footerImage.WrapFormat.AllowOverlap = -1;
footerImage.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
footerImage.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
footerImage.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
footerImage.Top = (float)Convert.ToInt32(this.top);
}
}
public void Lic()
{
// 25. string lickey = "Ngo9BigBOggjHTQxAR8/V1NCaF5cXmZCf1FpRmJGdld5fUVHYVZUTXxaS00DNHVRdkdnWXdecXRdQ2heUU13XUI="
//string lickey = "Ngo9BigBOggjHTQxAR8/V1NCaF5cXmZCf1FpRmJGdld5fUVHYVZUTXxaS00DNHVRdkdnWXZeeXRQR2NZUEZ2WkE=";
string lickey = "MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ==";
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(lickey);
}
private void button2_Click(object sender, EventArgs e)
{
WordDocument document = new WordDocument();
document.Open(@"E:\Software-Projekte\OnDoc\Excel_Dateien\empty.docx");
IWParagraph paragraph = document.Sections[0].Paragraphs[0];
if (document.Sections.Count < 1)
{
document.AddSection();
}
IWParagraph footerparagraph;
if (document.Sections[0].HeadersFooters.Footer.Paragraphs.Count < 1)
{
footerparagraph = document.Sections[0].HeadersFooters.Footer.AddParagraph();
}
footerparagraph = document.Sections[0].HeadersFooters.Footer.Paragraphs[0];
System.Drawing.Image sfimage = Image.FromFile(@"x:\gaga.png");
//sfimage = AutoSizeImage(sfimage, Convert.ToInt32(this.width), Convert.ToInt32(this.height), false);
//sfimage = ScaleImage(sfimage, Convert.ToInt32(this.width), Convert.ToInt32(this.height));
Shape rectangle = footerparagraph.AppendShape(AutoShapeType.Rectangle, Convert.ToInt32(this.width), Convert.ToInt32(this.height));
paragraph = rectangle.TextBody.AddParagraph() as WParagraph;
rectangle.LineFormat.Line = false;
IWPicture pic = paragraph.AppendPicture(sfimage);
pic.Width = 160;
pic.Height = 28;
rectangle.VerticalPosition = (Convert.ToInt32(this.top)/150 * 2.83465f);
rectangle.HorizontalPosition = Convert.ToInt32(this.left);
//rectangle.LineFormat.DashStyle = LineDashing.Dot;
//rectangle.LineFormat.Color = Color.DarkGray;
//IWParagraph rectangleparagraph = rectangle.TextBody.AddParagraph();
//IWPicture picture = rectangleparagraph.AppendPicture(sfimage);
//footerparagraph.AppendShape
document.Save(@"x:\gaga.docx");
document.Close();
System.Diagnostics.Process.Start("winword.exe", "/w " + @"x:\gaga.docx");
return;
//WordDocument document = new WordDocument();
//document.Open(@"E:\Software-Projekte\OnDoc\Excel_Dateien\empty.docx");
////Add a new section to the document.
//IWSection section = document.AddSection();
////Add a new paragraph to the section.
//IWParagraph paragraph = section.AddParagraph() as WParagraph;
//Shape rectangle = paragraph.AppendShape(AutoShapeType.RoundedRectangle, 150, 100);
//rectangle.VerticalPosition = 72;
//rectangle.HorizontalPosition = 72;
//paragraph = section.AddParagraph() as WParagraph;
//paragraph = rectangle.TextBody.AddParagraph() as WParagraph;
//System.Drawing.Image sfimage = Image.FromFile(@"x:\gaga.png");
//paragraph.AppendPicture(sfimage);
//IWTextRange text = paragraph.AppendText("This text is in rounded rectangle shape");
//text.CharacterFormat.TextColor = Color.Green;
//text.CharacterFormat.Bold = true;
////Apply fill color for shape.
//rectangle.FillFormat.Fill = true;
//rectangle.FillFormat.Color = Color.LightGray;
////Set transparency (opacity) to the shape fill color.
//rectangle.FillFormat.Transparency = 75;
////Apply wrap formats.
//rectangle.WrapFormat.TextWrappingStyle = TextWrappingStyle.Square;
//rectangle.WrapFormat.TextWrappingType = TextWrappingType.Right;
////Set horizontal and vertical origin.
//rectangle.HorizontalOrigin = HorizontalOrigin.Margin;
//rectangle.VerticalOrigin = VerticalOrigin.Page;
////Set line format.
//rectangle.LineFormat.DashStyle = LineDashing.Dot;
//rectangle.LineFormat.Color = Color.DarkGray;
////Set the left internal margin for the shape.
//rectangle.TextFrame.InternalMargin.Left = 30;
////Set the right internal margin for the shape.
//rectangle.TextFrame.InternalMargin.Right = 24;
////Set the bottom internal margin for the shape.
//rectangle.TextFrame.InternalMargin.Bottom = 18;
////Set the top internal margin for the shape.
//rectangle.TextFrame.InternalMargin.Top = 6;
////Saves the Word document to MemoryStream
//document.Save(@"x:\gaga.docx", FormatType.Docx);
////Closes the Word document
//document.Close();
}
}
}