面单 打印(流—>PdfTable—>保存图片—>打印)
作者:互联网
public ApiResponse Print(DbExpressParam param, DbExpressResult data, List<LpkPrd> purData)
{
MemoryStream m = new MemoryStream();
iTextSharp.text.Rectangle ps = new iTextSharp.text.Rectangle((float)(7.6 / 2.54) * 72, (float)(13 / 2.54) * 72);//面单长,宽
Document document = new Document(ps, 0, 0, 0, 0);//面单间距
PdfWriter writer = PdfWriter.GetInstance(document, m);
writer.SetEncryption(PdfWriter.STRENGTH128BITS, null, null, PdfWriter.AllowPrinting);//防复制
try
{
// 中文字体,解决中文不能显示问题
string youyuan = System.AppDomain.CurrentDomain.BaseDirectory + "font\\SIMYOU.TTF";
BaseFont SY = BaseFont.CreateFont(youyuan, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//超粗字体
BaseFont bf = BaseFont.CreateFont(@"c:\windows\fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//黑体
//BaseFont bf_en = BaseFont.CreateFont(@"c:\windows\fonts\SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//宋体
iTextSharp.text.Font bf_font_6 = new iTextSharp.text.Font(SY, 6f, iTextSharp.text.Font.BOLD);
iTextSharp.text.Font bf_font_8b = new iTextSharp.text.Font(SY, 8f, iTextSharp.text.Font.BOLD);
iTextSharp.text.Font bf_font_10b = new iTextSharp.text.Font(SY, 10f, iTextSharp.text.Font.BOLD);
iTextSharp.text.Font bf_font_15b = new iTextSharp.text.Font(SY, 15f, iTextSharp.text.Font.BOLD);
iTextSharp.text.Font bf_font_8 = new iTextSharp.text.Font(bf, 8f, iTextSharp.text.Font.NORMAL);
iTextSharp.text.Font bf_font_13b = new iTextSharp.text.Font(SY, 13f, iTextSharp.text.Font.BOLD);
document.Open();
//cellpadding11.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
//cellpadding11.HorizontalAlignment = Element.ALIGN_CENTER;//左右居中
#region 面单内容
PdfPTable table = new PdfPTable(76);
table.WidthPercentage = 100f;
//第一行 (76*10)
PdfPTable tb1 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding1 = new PdfPrinter.MyPdfPCell(tb1, 7.6, 1);
table.AddCell(cellpadding1);
//logo空白
PdfPTable tb11 = new PdfPTable(31);
PdfPrinter.MyPdfPCell cellpadding11 = new PdfPrinter.MyPdfPCell(tb11, 3.1, 1);
cellpadding11.Phrase = new Phrase("", bf_font_10b);
tb1.AddCell(cellpadding11);
//代收+到付 27*10
PdfPTable tb12 = new PdfPTable(27);
PdfPrinter.MyPdfPCell cellpadding12 = new PdfPrinter.MyPdfPCell(tb12, 2.7, 1);
tb1.AddCell(cellpadding12);
//代收27*5
PdfPTable tb121 = new PdfPTable(27);
PdfPrinter.MyPdfPCell cellpadding121 = new PdfPrinter.MyPdfPCell(tb121, 2.7, 0.5);
tb12.AddCell(cellpadding121);
//代收
PdfPrinter.MyPdfPCell cellpadding1211 = new PdfPrinter.MyPdfPCell(2.7, 0.5);
cellpadding1211.SetBottomBorder(true);
cellpadding1211.SetLeftBorder(true);
cellpadding1211.SetRightBorder(true);
cellpadding1211.Phrase = new Phrase("代收:", bf_font_6);
cellpadding1211.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
tb121.AddCell(cellpadding1211);
//金额框
PdfPTable tb122 = new PdfPTable(27);
PdfPrinter.MyPdfPCell cellpadding122 = new PdfPrinter.MyPdfPCell(tb122, 2.7, 0.5);
cellpadding122.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
cellpadding122.SetLeftBorder(true);
cellpadding122.SetRightBorder(true);
tb12.AddCell(cellpadding122);
//到付框
PdfPrinter.MyPdfPCell cellpadding1212 = new PdfPrinter.MyPdfPCell(2.7, 0.5);
cellpadding1212.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
cellpadding1212.Phrase = new Phrase("到付:", bf_font_6);
tb122.AddCell(cellpadding1212);
//运输方式 18*10
PdfPTable tb13 = new PdfPTable(18);
PdfPrinter.MyPdfPCell cellpadding13 = new PdfPrinter.MyPdfPCell(tb13, 1.8, 1);
cellpadding13.Phrase = new Phrase("大件快" + Environment.NewLine + "递360", bf_font_10b);
cellpadding13.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
cellpadding13.HorizontalAlignment = Element.ALIGN_CENTER;//左右居中
tb1.AddCell(cellpadding13);
//第二行 大头笔信息 arrivedOrgSimpleName 76*16
PdfPTable tb2 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding2 = new PdfPrinter.MyPdfPCell(tb2, 7.6, 1.6);
cellpadding2.Phrase = new Phrase(data.arrivedOrgSimpleName, bf_font_15b);//始发地
cellpadding2.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
cellpadding2.HorizontalAlignment = Element.ALIGN_CENTER;//左右居中
cellpadding2.SetTopBorder(true);
cellpadding2.SetBottomBorder(true);
table.AddCell(cellpadding2);
//第三行到达区域 收货人区 +货物物件(76*8)
PdfPTable tb3 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding3 = new PdfPrinter.MyPdfPCell(tb3, 7.6, 0.8);
cellpadding3.SetBottomBorder(true);
table.AddCell(cellpadding3);
PdfPrinter.MyPdfPCell cellpadding31 = new PdfPrinter.MyPdfPCell(6.6, 0.8);
cellpadding31.Phrase = new Phrase($"【{param.receiver.county}】", bf_font_13b);
cellpadding31.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
tb3.AddCell(cellpadding31);
PdfPrinter.MyPdfPCell cellpadding32 = new PdfPrinter.MyPdfPCell(1, 0.8);
cellpadding32.Phrase = new Phrase("1/1", bf_font_10b);
cellpadding32.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
tb3.AddCell(cellpadding32);
//第四行收货人信息 76*18
PdfPTable tb4 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding4 = new PdfPrinter.MyPdfPCell(tb4, 7.6, 2.4);
cellpadding4.SetBottomBorder(true);
table.AddCell(cellpadding4);
PdfPTable tb41 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding41 = new PdfPrinter.MyPdfPCell(tb41, 7.6, 2.4);
tb4.AddCell(cellpadding41);
//收字
PdfPTable tb411 = new PdfPTable(10);
PdfPrinter.MyPdfPCell cellpadding411 = new PdfPrinter.MyPdfPCell(tb411, 1, 2.4);
tb41.AddCell(cellpadding411);
PdfPrinter.MyPdfPCell cellpadding4111 = new PdfPrinter.MyPdfPCell(1, 2.4);
cellpadding4111.Phrase = new Phrase("收", bf_font_10b);
cellpadding4111.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
cellpadding4111.HorizontalAlignment = Element.ALIGN_CENTER;//左右居中
tb411.AddCell(cellpadding4111);
//具体信息
PdfPTable tb412 = new PdfPTable(66);
PdfPrinter.MyPdfPCell cellpadding412 = new PdfPrinter.MyPdfPCell(tb412, 6.6, 2.4);
tb41.AddCell(cellpadding412);
PdfPrinter.MyPdfPCell cellpadding41211 = new PdfPrinter.MyPdfPCell(6.6, 0.5);
cellpadding41211.Phrase = new Phrase($"{param.receiver.name} ", bf_font_8b);
tb412.AddCell(cellpadding41211);
PdfPrinter.MyPdfPCell cellpadding41212 = new PdfPrinter.MyPdfPCell(6.6, 0.5);
cellpadding41212.Phrase = new Phrase($"{param.receiver.mobile} ", bf_font_8b);
tb412.AddCell(cellpadding41212);
PdfPrinter.MyPdfPCell cellpadding41213 = new PdfPrinter.MyPdfPCell(6.6, 0.5);
cellpadding41213.Phrase = new Phrase($"{param.receiver.province} " + $"{param.receiver.city} " + $"{param.receiver.county}", bf_font_8b);
tb412.AddCell(cellpadding41213);
PdfPrinter.MyPdfPCell cellpadding41214 = new PdfPrinter.MyPdfPCell(6.6, 0.9);
cellpadding41214.Phrase = new Phrase($"{param.receiver.town}", bf_font_8b);
tb412.AddCell(cellpadding41214);
//第五行寄件人信息 76*6
PdfPTable tb5 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding5 = new PdfPrinter.MyPdfPCell(tb5, 7.6, 0.6);
cellpadding5.SetBottomBorder(true);
table.AddCell(cellpadding5);
PdfPrinter.MyPdfPCell cellpadding51 = new PdfPrinter.MyPdfPCell(1, 0.6);
cellpadding51.Phrase = new Phrase("寄", bf_font_8b);
cellpadding51.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
cellpadding51.HorizontalAlignment = Element.ALIGN_CENTER;//左右居中
tb5.AddCell(cellpadding51);
PdfPrinter.MyPdfPCell cellpadding52 = new PdfPrinter.MyPdfPCell(4.6, 0.6);
cellpadding52.Phrase = new Phrase($"{param.sender.name} {param.sender.mobile}", bf_font_8b);
cellpadding52.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
tb5.AddCell(cellpadding52);
PdfPrinter.MyPdfPCell cellpadding53 = new PdfPrinter.MyPdfPCell(2, 0.6);
cellpadding53.Phrase = new Phrase("实名认证", bf_font_8b);
cellpadding53.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
cellpadding53.HorizontalAlignment = Element.ALIGN_CENTER;//左右居中
tb5.AddCell(cellpadding53);
//第七行条码76*24
PdfContentByte cb = writer.DirectContent;
iTextSharp.text.pdf.Barcode128 code128 = new iTextSharp.text.pdf.Barcode128();
code128.CodeType = Barcode128.CODE_A;
code128.Code = data.mailNo;//包裹号
iTextSharp.text.Image imgBarcode = code128.CreateImageWithBarcode(cb, null, null);
imgBarcode.ScaleAbsolute((float)(imgBarcode.Width * 1.5), (float)(imgBarcode.Height * 1.2));
PdfPTable tb7 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding7 = new PdfPrinter.MyPdfPCell(tb7, 7.6, 2.4);
cellpadding7.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
cellpadding7.HorizontalAlignment = Element.ALIGN_CENTER;//左右居中
table.AddCell(cellpadding7);
PdfPrinter.MyPdfPCell cellpadding71 = new PdfPrinter.MyPdfPCell(imgBarcode, 7.6, 1.8);
cellpadding71.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
cellpadding71.HorizontalAlignment = Element.ALIGN_CENTER;//左右居中
tb7.AddCell(cellpadding71);
PdfPrinter.MyPdfPCell cellpadding72 = new PdfPrinter.MyPdfPCell(7.6, 0.6);
cellpadding72.Phrase = new Phrase("已验收", bf_font_10b);
cellpadding72.VerticalAlignment = Element.ALIGN_BOTTOM;
cellpadding72.HorizontalAlignment = Element.ALIGN_RIGHT;
tb7.AddCell(cellpadding72);
//第六行到达区域 收货人区 +货物物件76*6
PdfPTable tb6 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding6 = new PdfPrinter.MyPdfPCell(tb6, 7.6, 0.6);
cellpadding6.SetTopBorder(true);
cellpadding6.SetBottomBorder(true);
table.AddCell(cellpadding6);
PdfPrinter.MyPdfPCell cellpadding61 = new PdfPrinter.MyPdfPCell(4, 0.6);
cellpadding61.Phrase = new Phrase(" 签回单返单:", bf_font_10b);
cellpadding61.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
tb6.AddCell(cellpadding61);
var baojia = "";
if (param.addService != null)
{
if (Convert.ToInt32(param.addService.insuranceValue) > 0)
{
baojia = param.addService.insuranceValue.ToString();
}
}
PdfPrinter.MyPdfPCell cellpadding62 = new PdfPrinter.MyPdfPCell(3.6, 0.6);
cellpadding62.Phrase = new Phrase("保价金额:" + baojia, bf_font_10b);
cellpadding62.VerticalAlignment = Element.ALIGN_MIDDLE;//上下居中
tb6.AddCell(cellpadding62);
//自定义区域 + 时间76*42
PdfPTable tb8 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding8 = new PdfPrinter.MyPdfPCell(tb8, 7.6, 4.2);
cellpadding8.SetTopBorder(true);
cellpadding8.SetBottomBorder(true);
table.AddCell(cellpadding8);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < purData.Count; i++)
{
sb.Append(" " + purData[i].PRD_NAME + "X" + purData[i].LPK_QTY + Environment.NewLine);
}
PdfPTable tb81 = new PdfPTable(76);
PdfPrinter.MyPdfPCell cellpadding81 = new PdfPrinter.MyPdfPCell(tb81, 7.6, 3.6);
cellpadding81.Phrase = new Phrase(" 订单号:" + purData.FirstOrDefault().PUR_CODE + Environment.NewLine + " 商品信息:" + Environment.NewLine + sb.ToString(), bf_font_8);
tb8.AddCell(cellpadding81);
PdfPrinter.MyPdfPCell cellpadding82 = new PdfPrinter.MyPdfPCell(7.6, 0.6);
cellpadding82.Phrase = new Phrase(DateTime.Now.ToString(), bf_font_8);
cellpadding82.VerticalAlignment = Element.ALIGN_BOTTOM;
cellpadding82.HorizontalAlignment = Element.ALIGN_RIGHT;
tb8.AddCell(cellpadding82);
#endregion
document.Add(table);
document.NewPage();
}
catch (Exception ex)
{
Res.Abs.Store.BLL.LogHelper.Error(" 面单打印", ex);
return new ApiResponse(false, "系统错误:" + ex.Message);
}
document.ResetPageCount();
document.Close();
Prints.PrintPdfDb(m, " 面单" + DateTime.Now.ToString("_yyyyMMddHHmmss") + ".pdf", PrinterType.DBExpress);
return new ApiResponse(true, null);
}
//打印方式 存放在系统。也可以自定义文件夹
public static void PrintPdfDb(MemoryStream ms, string path, PrinterType ptid, bool rotate = false)
{
string PdfSavePath = System.AppDomain.CurrentDomain.BaseDirectory + "Pdf\\";
path = PdfSavePath + path;
try
{
if (!Directory.Exists(PdfSavePath))//若文件夹不存在则新建文件夹
{
Directory.CreateDirectory(PdfSavePath); //新建文件夹
}
FileStream fs = new FileStream(path, FileMode.Create);
fs.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
fs.Flush();
fs.Close();
}
catch (Exception ex)
{
LogHelper.Error("pdf保存出错", ex);
return;
}
PrintDialog printDialog1 = new System.Windows.Forms.PrintDialog();
if (printDialog1.ShowDialog() == DialogResult.OK)//弹出选择印表机的窗体
{
PrinterHelper.PrintPdfDb(path, printDialog1.PrinterSettings.PrinterName, rotate);
}
}
//打印
public static void PrintPdfDb(string pdfInputPath, string printerName, bool rotate = false)
{
try
{
PDFFile pdfFile = PDFFile.Open(pdfInputPath);
int definition = 20; //清晰度
for (int i = 1; i <= pdfFile.PageCount; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
if (rotate == true)//纵向打印需要旋转
{
pageImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PrinterSettings.PrinterName = printerName;
pd.PrintPage += delegate (object sender, PrintPageEventArgs e)
{
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
e.Graphics.DrawImage(pageImage, new Rectangle(0, 0, pd.DefaultPageSettings.PaperSize.Width, pd.DefaultPageSettings.PaperSize.Height));
};
pd.Print();
//pageImage.Save(pdfInputPath.Replace(":", "").Replace("\\", "") + "." + ImageFormat.ToString(), imageFormat);
pageImage.Dispose();
}
pdfFile.Dispose();
}
catch (Exception ex)
{
LogHelper.Error("打印出错", ex);
}
}
标签:PdfPTable,打印,PdfTable,面单,AddCell,MyPdfPCell,Phrase,new,PdfPrinter 来源: https://www.cnblogs.com/KingPan/p/13494693.html