我正在尝试制作一个C#应用程序,该应用程序允许我将DHL运送标签PDF文件拖放到.exe上,将其转换为图像,然后缩放,裁剪,合成和打印在如兄弟ql500。
经过很多“如果出错,然后stackoverflow()”,我就可以做我想做的事。
只有一个主要问题,我无法更改打印参数,始终必须手动执行,并且不会留下。
我必须设置的参数是纸张宽度(62mm)和打印长度(160mm)。
我将所有设置放到名为“ DHLLabel”的预设中,但是我无法将其设为默认值。
图片:
print dialog parameters dialog
我的问题是,这些参数(格式:DHLLabel)始终返回到默认值。
将宽度(Bandbreite)或长度(Länge)设置为我需要的值时,下次我想打印标签时,它将恢复为默认值。
我尝试通过 PrintDocument.DefaultPageSettings.PaperSize 在C#中设置它,但它什么也没做。
这里是我到目前为止所得到的:(需要cs-pdf-to-image库)
using PdfToImage;
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace DHLLabelConverter
{
class Program
{
public static void CopyRegionIntoImage(Bitmap srcBitmap, Rectangle srcRegion, Bitmap destBitmap, Rectangle destRegion)
{
using (Graphics grD = Graphics.FromImage(destBitmap))
{
grD.DrawImage(srcBitmap, destRegion, srcRegion, GraphicsUnit.Pixel);
}
}
static void Main(string[] args)
{
if(args.Length > 0 && args[0] != "" && File.Exists(args[0]) )
{
PDFConvert converter = new PDFConvert();
GhostScriptRevision version = converter.GetRevision();
bool Converted = false;
converter.RenderingThreads = -1;
converter.TextAlphaBit = -1;
converter.TextAlphaBit = -1;
converter.FitPage = true;
converter.OutputFormat = "png256";
converter.Width = 4000;
converter.Height = 6000;
converter.ResolutionX = 1000;
converter.ResolutionY = 1000;
converter.OutputToMultipleFile = false;
converter.FirstPageToConvert = 1;
converter.LastPageToConvert = 1;
System.IO.FileInfo input = new FileInfo(args[0]);
//convert pdf to image
Converted = converter.Convert(input.FullName, "temp.png");
String fil = Path.GetDirectoryName(args[0]);
//open image to create a modified version of it
var stream = File.OpenRead("temp.png");
Bitmap label_main = new Bitmap(stream);
Bitmap label_edit = new Bitmap(1750, 700);
//modify and compose the actual image
label_main.RotateFlip(RotateFlipType.Rotate90FlipNone);
CopyRegionIntoImage(label_main, new Rectangle(3000, 270, 2120, 1250), label_edit, new Rectangle(20, 0, 1100, 700));
CopyRegionIntoImage(label_main, new Rectangle(3050, 1530, 1200, 180), label_edit, new Rectangle(50, 585, 680, 105));
CopyRegionIntoImage(label_main, new Rectangle(3500, 2360, 1100, 1350), label_edit, new Rectangle(1100, 0, 650, 700));
//save the edited image.
label_edit.Save("temp2.png");
//open print dialog, pass edited image into it and set parameters
PrintDialog pdi = new PrintDialog();
PrintDocument pdo = new PrintDocument();
pdo.PrintPage += (sender, arg) =>
{
Image i = Image.FromFile(@"temp2.png");
arg.Graphics.DrawImage(i, 0,0,600,240);
};
pdo.DefaultPageSettings.PrinterSettings.PrinterName = "Brother QL-500";
pdo.DefaultPageSettings.Landscape = true;
pdi.Document = pdo;
if (pdi.ShowDialog() == DialogResult.OK)
{
pdo.Print();
}else{
MessageBox.Show("Print Cancelled");
}
}
}
}
}