一、使用NuGet安装ITextSharp
在NuGet下搜索:iTextSharp.LGPLv2.Core 并安装
二、创建pdf文档
//设置页面大小
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(254.7f, 90.56f);//采用点计算:一mm约等于2.83 点
//设置边界
Document document = new Document(pageSize, 5, 5, 5, 0);
三、设置文档基本字体
string fontpath = "C:\\Windows\\Fonts\\simsun.ttc,0";
BaseFont customfont = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//设置字体颜色样式
var baseFont = new iTextSharp.text.Font(customfont)
{
Color = new BaseColor(System.Drawing.Color.Black), //设置字体颜色
Size = 12, //字体大小
};
四、定义需要使用的字体样式
var headerStyle = new iTextSharp.text.Font(customfont)
{
Color = new BaseColor(System.Drawing.Color.Black),
Size = 14,
};
五、插入表格
PdfPTable table = new PdfPTable(4); //生成只有一列的行数据
table.TotalWidth = 254f;
table.LockedWidth = true;
table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; //无边框
table.DefaultCell.MinimumHeight = 30f; //设置表格最小高度
float[] headWidths_1 = new float[] { 56f, 80f, 40f, 68f }; //设置表格列宽度
table.SetWidths(headWidths_1);
document.Add(table);
六、表格总插入单元格,行列合并
var Row_1_Cell_4 = new PdfPCell(new Paragraph("", customerStyle));//表格内容
Row_1_Cell_4.MinimumHeight = 15f;//设置最小高度
Row_1_Cell_4.Rowspan = 5;//设置行合并
Row_1_Cell_4.Colspan = 2;//设置列合并
Row_1_Cell_4.HorizontalAlignment = Element.ALIGN_CENTER;//设置文本居中
table.AddCell(Row_1_Cell_4);//将单元格内容添加到表格中
七、插入二维码
QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();//创建二维码对象
QRCodeData qrCodeData = qrGenerator.CreateQrCode(barCodeDto.Id,QRCodeGenerator.ECCLevel.Q);//创建二维码
QRCode qrcode = new QRCode(qrCodeData);//创建二维码对象
Bitmap qrCodeImage = qrcode.GetGraphic(2, Color.Black, Color.White, null, 2, 6, false);//将二维码转换成bitmap
System.Drawing.Image image1 = qrCodeImage;//bitmap转image
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(image1, ImageFormat.Jpeg);//将图片转换成 iTextSharp图片
image.SetAbsolutePosition(186f, 14f);//设置图片的绝对位置(左下角 0,0 )
document.Add(image);//将二维码图片插入pdf中
八、查询条形码
System.DrawingCore.Bitmap qrCodeImage2 = BarCodeHelper.Generate(barCodeDto.bom.DrawngNo.Split("_")[0], 100, 15);//生成条形码图片
MemoryStream ms2 = new MemoryStream();//创建接收流对象
qrCodeImage2.Save(ms2, System.DrawingCore.Imaging.ImageFormat.Jpeg);//保存图片到流
iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(ms2.ToArray());//将流转换成image对象
image2.SetAbsolutePosition(1f, 10f);//设置图片的绝对位置
document.Add(image2);//插入条码
九、补充
条形码帮助类,注意NuGet安装ZXing.Net.Bindings.ZKWeb.System.Drawing生成条形码
public class BarCodeHelper
{
public static Bitmap Generate(string text, int width, int height)
{
var w = new ZXing.OneD.Code128Writer();
BitMatrix b = w.encode(text, BarcodeFormat.CODE_128, width, height);
var zzb = new ZXing.ZKWeb.BarcodeWriter();
zzb.Options = new EncodingOptions()
{
Margin = 3,
PureBarcode = false
};
Bitmap b2 = zzb.Write(b);
return b2;
}
}
十、完整示例代码
public MemoryStream buildPdf(List<RpWorkOrderBarCodeDto> result)
{
List<SysCustomer> customers = service.Where<SysCustomer>(d => d.IsDel.Equals("0")).ToList();
//设置页面大小
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(254.7f, 90.56f);
//设置边界
Document document = new Document(pageSize, 5, 5, 5, 0);
//生成pdf
//Document document = new Document();
//var fileStream = File.Create("测试.pdf");
System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
PdfWriter pw = PdfWriter.GetInstance(document, bookStream);
document.Open();
//指定字体文件,IDENTITY_H:支持中文
string fontpath = "C:\\Windows\\Fonts\\simsun.ttc,0";
//BaseFont customfont = BaseFont.CreateFont("宋体", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
BaseFont customfont = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// iTextSharp.text.Font customfont = FontFactory.GetFont("SimSun", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
//BaseFont customfont = BaseFont.get(BaseFont.s, BaseFont.CP1252, false);
//设置字体颜色样式
var baseFont = new iTextSharp.text.Font(customfont)
{
Color = new BaseColor(System.Drawing.Color.Black), //设置字体颜色
Size = 12, //字体大小
};
//定义字体样式
var headerStyle = new iTextSharp.text.Font(customfont)
{
Color = new BaseColor(System.Drawing.Color.Black),
Size = 14,
};
var customerStyle = new iTextSharp.text.Font(customfont)
{
Color = new BaseColor(System.Drawing.Color.Black),
Size = 12,
};
var customerStyle8 = new iTextSharp.text.Font(customfont)
{
Color = new BaseColor(System.Drawing.Color.Black),
Size = 8,
};
var customerStyle10 = new iTextSharp.text.Font(customfont)
{
Color = new BaseColor(System.Drawing.Color.Black),
Size = 10,
};
int i = 1;
foreach (RpWorkOrderBarCodeDto barCodeDto in result)
{
PdfPTable table = new PdfPTable(4); //生成只有一列的行数据
table.TotalWidth = 254f;
table.LockedWidth = true;
table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; //无边框
table.DefaultCell.MinimumHeight = 30f; //高度
float[] headWidths_1 = new float[] { 56f, 80f, 40f, 68f }; //宽度
table.SetWidths(headWidths_1);
#region 总条码部分
if (string.IsNullOrEmpty(barCodeDto.BomId))
{
#region 序号
var Row_1_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.salOrder != null ? barCodeDto.salOrder.SchedulingNo+"" : "", customerStyle));//序号
Row_1_Cell_1.MinimumHeight = 15f;
Row_1_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_1);
#endregion
#region 箱号
PdfPCell Row_1_Cell_2;
string Box = barCodeDto.box != null ? barCodeDto.box.No + "" : "";
if (!string.IsNullOrEmpty(Box))
{
if (Box.Length <= 7)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box, customerStyle));
}
else if (Box.Length > 7 && Box.Length <= 14)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box, customerStyle10));
}
else if (Box.Length > 14 && Box.Length <= 20)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box, customerStyle8));
}
else
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box.Substring(0, 20), customerStyle8));
}
}
else
{
Row_1_Cell_2 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_1_Cell_2.MinimumHeight = 15f;
Row_1_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_2);
#endregion
#region 包号
//PdfPCell Row_1_Cell_2;
//if (!string.IsNullOrEmpty(barCodeDto.packageName))
//{
// if (barCodeDto.packageName.Length <= 7)
// {
// Row_1_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.packageName, customerStyle));
// }
// else if (barCodeDto.packageName.Length > 7 && barCodeDto.packageName.Length <= 14)
// {
// Row_1_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.packageName, customerStyle10));
// }
// else if (barCodeDto.packageName.Length > 14 && barCodeDto.packageName.Length <= 20)
// {
// Row_1_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.packageName, customerStyle8));
// }
// else
// {
// Row_1_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.packageName.Substring(0, 20), customerStyle8));
// }
//}
//else
//{
// Row_1_Cell_2 = new PdfPCell(new Paragraph("", customerStyle8));
//}
var Row_1_Cell_3 = new PdfPCell(new Paragraph((string.IsNullOrEmpty(barCodeDto.packageName) ? "" : barCodeDto.packageName.Length <= 2 ? barCodeDto.packageName : ""), customerStyle));//包号
Row_1_Cell_3.MinimumHeight = 15f;
Row_1_Cell_3.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_3);
#endregion
#region 条码列
var Row_1_Cell_4 = new PdfPCell(new Paragraph("", customerStyle));//条码列
Row_1_Cell_4.MinimumHeight = 15f;
Row_1_Cell_4.Rowspan = 5;
Row_1_Cell_4.Colspan = 2;
Row_1_Cell_4.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_4);
#endregion
#region 合同号
PdfPCell Row_2_Cell_1;
if (!string.IsNullOrEmpty(barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : ""))
{
if (barCodeDto.salOrder.BillNo.Length <= 7)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : "", customerStyle));
}
else if (barCodeDto.salOrder.BillNo.Length > 7 && barCodeDto.salOrder.BillNo.Length <= 14)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : "", customerStyle10));
}
else if (barCodeDto.salOrder.BillNo.Length > 14 && barCodeDto.salOrder.BillNo.Length <= 20)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : "", customerStyle8));
}
else
{
Row_2_Cell_1 = new PdfPCell(new Paragraph((barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : "").Substring(0, 20), customerStyle8));
}
}
else
{
Row_2_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_2_Cell_1.MinimumHeight = 15f;
Row_2_Cell_1.Colspan = 3;
Row_2_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_2_Cell_1);
#endregion
#region 模块号
PdfPCell Row_3_Cell_1;
if (!string.IsNullOrEmpty(barCodeDto.productType.Name))
{
if (barCodeDto.productType.Name.Length <= 7)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productType.Name, customerStyle));
}
else if (barCodeDto.productType.Name.Length > 7 && barCodeDto.productType.Name.Length <= 14)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productType.Name, customerStyle10));
}
else if (barCodeDto.productType.Name.Length > 14 && barCodeDto.productType.Name.Length <= 20)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productType.Name, customerStyle8));
}
else
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productType.Name.Substring(0, 20), customerStyle8));
}
}
else
{
Row_3_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_3_Cell_1.MinimumHeight = 15f;
Row_3_Cell_1.Colspan = 3;
Row_3_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_3_Cell_1);
#endregion
#region 排产日期
var Row_4_Cell_1 = new PdfPCell(new Paragraph("排产日期", customerStyle));
Row_4_Cell_1.MinimumHeight = 15f;
Row_4_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_4_Cell_1);
var Row_4_Cell_2 = new PdfPCell(new Paragraph( barCodeDto.salOrder != null ? barCodeDto.salOrder.SchedulingDt + "" : "", customerStyle));
Row_4_Cell_2.MinimumHeight = 15f;
Row_4_Cell_2.Colspan = 2;
Row_4_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_4_Cell_2);
#endregion
#region 发货日期
var Row_5_Cell_1 = new PdfPCell(new Paragraph("发货日期", customerStyle));
Row_5_Cell_1.MinimumHeight = 15f;
Row_5_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_5_Cell_1);
var Row_5_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.salOrder != null ? barCodeDto.salOrder.DirectDt + "" : "", customerStyle));
Row_5_Cell_2.MinimumHeight = 15f;
Row_5_Cell_2.Colspan = 2;
Row_5_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_5_Cell_2);
#endregion
}
#endregion
else
{
#region 电缆数据部分
if (barCodeDto.craft != null && barCodeDto.craft.Unit.Contains("M"))
{
bool yzx = false;
if (barCodeDto.productType != null)
{
if ("预制线类".Equals(barCodeDto.productType.Descpt))
{
yzx = true;
}
}
#region 预制线类
if (yzx)
{
#region 序号+包号
string SchedulingNoPackageName = (barCodeDto.salOrder != null ? barCodeDto.salOrder.SchedulingNo + "" : "") + (string.IsNullOrEmpty(barCodeDto.packageName) ? "" : barCodeDto.packageName.Length <= 2 ? "-" + barCodeDto.packageName : "");
PdfPCell Row_1_Cell_1;
if (!string.IsNullOrEmpty(SchedulingNoPackageName))
{
if (SchedulingNoPackageName.Length <= 7)
{
Row_1_Cell_1 = new PdfPCell(new Paragraph(SchedulingNoPackageName, customerStyle));
}
else if (SchedulingNoPackageName.Length > 7 && SchedulingNoPackageName.Length <= 14)
{
Row_1_Cell_1 = new PdfPCell(new Paragraph(SchedulingNoPackageName, customerStyle10));
}
else if (SchedulingNoPackageName.Length > 14 && SchedulingNoPackageName.Length <= 20)
{
Row_1_Cell_1 = new PdfPCell(new Paragraph(SchedulingNoPackageName, customerStyle8));
}
else
{
Row_1_Cell_1 = new PdfPCell(new Paragraph(SchedulingNoPackageName.Substring(0, 20), customerStyle8));
}
}
else
{
Row_1_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_1_Cell_1.MinimumHeight = 15f;
Row_1_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_1);
#endregion
#region 箱号
PdfPCell Row_1_Cell_2;
string Box = (barCodeDto.box != null ? barCodeDto.box.No + "" : "") + (barCodeDto.productType!=null? "-"+barCodeDto.productType.Name:"");
if (!string.IsNullOrEmpty(Box))
{
if (Box.Length <= 6)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box, customerStyle));
}
else if (Box.Length > 6 && Box.Length <= 10)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box, customerStyle10));
}
else if (Box.Length > 10 && Box.Length <= 14)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box, customerStyle8));
}
else
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box.Substring(0, 14), customerStyle8));
}
}
else
{
Row_1_Cell_2 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_1_Cell_2.MinimumHeight = 15f;
Row_1_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_2);
#endregion
#region 长度
var Row_1_Cell_3 = new PdfPCell(new Paragraph((Math.Ceiling(decimal.Parse(barCodeDto.Length) * 100) / 100) + "M", customerStyle10));
Row_1_Cell_3.MinimumHeight = 15f;
Row_1_Cell_3.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_3);
#endregion
#region 条码列
var Row_1_Cell_4 = new PdfPCell(new Paragraph("", customerStyle));//条码列
Row_1_Cell_4.MinimumHeight = 15f;
Row_1_Cell_4.Rowspan = 5;
Row_1_Cell_4.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_4);
#endregion
#region 部件号
PdfPCell Row_2_Cell_1;
if (!string.IsNullOrEmpty(barCodeDto.bom.DrawngNo))
{
if (barCodeDto.bom.DrawngNo.Length <= 7)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo, customerStyle));
}
else if (barCodeDto.bom.DrawngNo.Length > 7 && barCodeDto.bom.DrawngNo.Length <= 14)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo, customerStyle10));
}
else if (barCodeDto.bom.DrawngNo.Length > 14 && barCodeDto.bom.DrawngNo.Length <= 20)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo, customerStyle8));
}
else
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo.Substring(0, 20), customerStyle8));
}
}
else
{
Row_2_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_2_Cell_1.MinimumHeight = 15f;
Row_2_Cell_1.Colspan = 2;
Row_2_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_2_Cell_1);
#endregion
#region 数量
var Row_2_Cell_2 = new PdfPCell(new Paragraph((Math.Ceiling(decimal.Parse(barCodeDto.Count) * 100) / 100) + "EA", customerStyle10));
Row_2_Cell_2.MinimumHeight = 15f;
Row_2_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_2_Cell_2);
#endregion
#region 描述
PdfPCell Row_3_Cell_1;
if (!string.IsNullOrEmpty(barCodeDto.productDetail.Name))
{
if (barCodeDto.productDetail.Name.Length <= 7)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name, customerStyle));
}
else if (barCodeDto.productDetail.Name.Length > 7 && barCodeDto.productDetail.Name.Length <= 14)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name, customerStyle10));
}
else if (barCodeDto.productDetail.Name.Length > 14 && barCodeDto.productDetail.Name.Length <= 20)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name, customerStyle8));
}
else
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name.Substring(0, 20), customerStyle8));
}
}
else
{
Row_3_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_3_Cell_1.MinimumHeight = 15f;
Row_3_Cell_1.Colspan = 2;
Row_3_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_3_Cell_1);
#endregion
#region 剥皮
PdfPCell Row_3_Cell_2;
if (!string.IsNullOrEmpty(barCodeDto.frontAndLastCount))
{
if (barCodeDto.frontAndLastCount.Length <= 6)
{
Row_3_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.frontAndLastCount, customerStyle));
}
else if (barCodeDto.frontAndLastCount.Length > 6 && barCodeDto.frontAndLastCount.Length <= 14)
{
Row_3_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.frontAndLastCount, customerStyle10));
}
else if (barCodeDto.frontAndLastCount.Length > 14 && barCodeDto.frontAndLastCount.Length <= 20)
{
Row_3_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.frontAndLastCount, customerStyle8));
}
else
{
Row_3_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.frontAndLastCount.Substring(0, 20), customerStyle8));
}
}
else
{
Row_3_Cell_2 = new PdfPCell(new Paragraph("", customerStyle8));
}
//var Row_3_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.frontAndLastCount, customerStyle));
Row_3_Cell_2.MinimumHeight = 15f;
Row_3_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_3_Cell_2);
#endregion
#region 模块号-ggl
string ggl = "";
if (barCodeDto.productType.IsShowProductTypeName.Contains("工艺编码"))
{
if (barCodeDto.craft != null)
{
ggl += barCodeDto.craft.No;
}
}
if (barCodeDto.productType.IsShowProductTypeName.Contains("规格"))
{
if (barCodeDto.craft != null)
{
ggl += barCodeDto.craft.Specification;
}
}
if (barCodeDto.productType.IsShowProductTypeName.Contains("模块号"))
{
if (barCodeDto.craft != null)
{
ggl += barCodeDto.productType.Name;
}
}
if (string.IsNullOrEmpty(ggl))
{
if (barCodeDto.craft != null)
{
ggl += barCodeDto.craft.Specification;
}
}
PdfPCell Row_4_Cell_1;
if (!string.IsNullOrEmpty(ggl))
{
if (ggl.Length <= 6)
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(ggl, customerStyle));
}
else if (ggl.Length > 6 && ggl.Length <= 14)
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(ggl, customerStyle10));
}
else if (ggl.Length > 14 && ggl.Length <= 20)
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(ggl, customerStyle8));
}
else
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(ggl.Substring(0, 20), customerStyle8));
}
}
else
{
Row_4_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_4_Cell_1.MinimumHeight = 15f;
Row_4_Cell_1.Colspan = 3;
Row_4_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_4_Cell_1);
#endregion
#region 条码
var Row_5_Cell_1 = new PdfPCell(new Paragraph("", customerStyle));
Row_5_Cell_1.MinimumHeight = 15f;
Row_5_Cell_1.Colspan = 3;
Row_5_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_5_Cell_1);
#region 插入条形码
System.DrawingCore.Bitmap qrCodeImage2 = BarCodeHelper.Generate(barCodeDto.bom.DrawngNo.Split("_")[0], 100, 15);
MemoryStream ms2 = new MemoryStream();
if (qrCodeImage2.Width > 190)
{
System.DrawingCore.Image imageBarCode = DrawingHelper.resizeImage(qrCodeImage2, new Size(190, 16));
imageBarCode.Save(ms2, System.DrawingCore.Imaging.ImageFormat.Jpeg);
}
else
{
qrCodeImage2.Save(ms2, System.DrawingCore.Imaging.ImageFormat.Jpeg);
}
iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(ms2.ToArray());
image2.SetAbsolutePosition(1f, 10f);//设置图片的绝对位置
document.Add(image2);
#endregion
#endregion
}
#endregion
#region 随行和其他类
else
{
#region 序号
string SchedulingNoPackageName = (barCodeDto.salOrder != null ? barCodeDto.salOrder.SchedulingNo + "" : "");
PdfPCell Row_1_Cell_1;
if (!string.IsNullOrEmpty(SchedulingNoPackageName))
{
if (SchedulingNoPackageName.Length <= 7)
{
Row_1_Cell_1 = new PdfPCell(new Paragraph(SchedulingNoPackageName, customerStyle));
}
else if (SchedulingNoPackageName.Length > 7 && SchedulingNoPackageName.Length <= 14)
{
Row_1_Cell_1 = new PdfPCell(new Paragraph(SchedulingNoPackageName, customerStyle10));
}
else if (SchedulingNoPackageName.Length > 14 && SchedulingNoPackageName.Length <= 20)
{
Row_1_Cell_1 = new PdfPCell(new Paragraph(SchedulingNoPackageName, customerStyle8));
}
else
{
Row_1_Cell_1 = new PdfPCell(new Paragraph(SchedulingNoPackageName.Substring(0, 20), customerStyle8));
}
}
else
{
Row_1_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_1_Cell_1.MinimumHeight = 15f;
Row_1_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_1);
#endregion
#region 箱号
PdfPCell Row_1_Cell_2;
string Box = barCodeDto.box != null ? barCodeDto.box.No + "" : "";
if (!string.IsNullOrEmpty(Box))
{
if (Box.Length <= 7)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box, customerStyle));
}
else if (Box.Length > 7 && Box.Length <= 14)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box, customerStyle10));
}
else if (Box.Length > 14 && Box.Length <= 20)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box, customerStyle8));
}
else
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(Box.Substring(0, 20), customerStyle8));
}
}
else
{
Row_1_Cell_2 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_1_Cell_2.MinimumHeight = 15f;
Row_1_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_2);
#endregion
#region 长度
var Row_1_Cell_3 = new PdfPCell(new Paragraph((Math.Ceiling(decimal.Parse(barCodeDto.Length) * 100) / 100) + "M", customerStyle10));
Row_1_Cell_3.MinimumHeight = 15f;
Row_1_Cell_3.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_3);
#endregion
#region 条码列
var Row_1_Cell_4 = new PdfPCell(new Paragraph("", customerStyle));//条码列
Row_1_Cell_4.MinimumHeight = 15f;
Row_1_Cell_4.Rowspan = 5;
Row_1_Cell_4.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_4);
#endregion
#region 合同号
PdfPCell Row_2_Cell_1;
if (barCodeDto.salOrder!=null && !string.IsNullOrEmpty(barCodeDto.salOrder.BillNo))
{
if (barCodeDto.salOrder.BillNo.Length <= 7)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.salOrder.BillNo, customerStyle));
}
else if (barCodeDto.salOrder.BillNo.Length > 7 && barCodeDto.salOrder.BillNo.Length <= 14)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.salOrder.BillNo, customerStyle10));
}
else if (barCodeDto.salOrder.BillNo.Length > 14 && barCodeDto.salOrder.BillNo.Length <= 20)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.salOrder.BillNo, customerStyle8));
}
else
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.salOrder.BillNo.Substring(0, 20), customerStyle8));
}
}
else
{
Row_2_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_2_Cell_1.MinimumHeight = 15f;
Row_2_Cell_1.Colspan = 2;
Row_2_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_2_Cell_1);
#endregion
#region 数量
var Row_2_Cell_2 = new PdfPCell(new Paragraph((Math.Ceiling(decimal.Parse(barCodeDto.Count) * 100) / 100) + "EA", customerStyle10));
Row_2_Cell_2.MinimumHeight = 15f;
Row_2_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_2_Cell_2);
#endregion
#region 部件号
PdfPCell Row_3_Cell_1;
if (!string.IsNullOrEmpty(barCodeDto.bom.DrawngNo))
{
if (barCodeDto.bom.DrawngNo.Length <= 7)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo, customerStyle));
}
else if (barCodeDto.bom.DrawngNo.Length > 7 && barCodeDto.bom.DrawngNo.Length <= 14)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo, customerStyle10));
}
else if (barCodeDto.bom.DrawngNo.Length > 14 && barCodeDto.bom.DrawngNo.Length <= 20)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo, customerStyle8));
}
else
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo.Substring(0, 20), customerStyle8));
}
}
else
{
Row_3_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_3_Cell_1.MinimumHeight = 15f;
Row_3_Cell_1.Colspan = 2;
Row_3_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_3_Cell_1);
#endregion
#region 剥皮
PdfPCell Row_3_Cell_2;
if (!string.IsNullOrEmpty(barCodeDto.frontAndLastCount))
{
if (barCodeDto.frontAndLastCount.Length <= 6)
{
Row_3_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.frontAndLastCount, customerStyle));
}
else if (barCodeDto.frontAndLastCount.Length > 6 && barCodeDto.frontAndLastCount.Length <= 14)
{
Row_3_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.frontAndLastCount, customerStyle10));
}
else if (barCodeDto.frontAndLastCount.Length > 14 && barCodeDto.frontAndLastCount.Length <= 20)
{
Row_3_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.frontAndLastCount, customerStyle8));
}
else
{
Row_3_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.frontAndLastCount.Substring(0, 20), customerStyle8));
}
}
else
{
Row_3_Cell_2 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_3_Cell_2.MinimumHeight = 15f;
Row_3_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_3_Cell_2);
#endregion
#region 描述
PdfPCell Row_4_Cell_1;
if (!string.IsNullOrEmpty(barCodeDto.productDetail.Name))
{
if (barCodeDto.productDetail.Name.Length <= 7)
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name, customerStyle));
}
else if (barCodeDto.productDetail.Name.Length > 7 && barCodeDto.productDetail.Name.Length <= 14)
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name, customerStyle10));
}
else if (barCodeDto.productDetail.Name.Length > 14 && barCodeDto.productDetail.Name.Length <= 20)
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name, customerStyle8));
}
else
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name.Substring(0, 20), customerStyle8));
}
}
else
{
Row_4_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_4_Cell_1.MinimumHeight = 15f;
Row_4_Cell_1.Colspan = 2;
Row_4_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_4_Cell_1);
#endregion
#region 模块号-ggl
string ggl = "";
if (barCodeDto.craft != null)
{
ggl = barCodeDto.craft.No;
}
//if (barCodeDto.productType.IsShowProductTypeName.Contains("工艺编码"))
//{
// if (barCodeDto.craft != null)
// {
// ggl += barCodeDto.craft.No;
// }
//}
//if (barCodeDto.productType.IsShowProductTypeName.Contains("规格"))
//{
// if (barCodeDto.craft != null)
// {
// ggl += barCodeDto.craft.Specification;
// }
//}
//if (barCodeDto.productType.IsShowProductTypeName.Contains("模块号"))
//{
// if (barCodeDto.craft != null)
// {
// ggl += barCodeDto.productType.Name;
// }
//}
//if (string.IsNullOrEmpty(ggl))
//{
// if (barCodeDto.craft != null)
// {
// ggl += barCodeDto.craft.No;
// }
//}
PdfPCell Row_4_Cell_2;
if (!string.IsNullOrEmpty(ggl))
{
if (ggl.Length <= 6)
{
Row_4_Cell_2 = new PdfPCell(new Paragraph(ggl, customerStyle));
}
else if (ggl.Length > 6 && ggl.Length <= 14)
{
Row_4_Cell_2 = new PdfPCell(new Paragraph(ggl, customerStyle10));
}
else if (ggl.Length > 14 && ggl.Length <= 20)
{
Row_4_Cell_2 = new PdfPCell(new Paragraph(ggl, customerStyle8));
}
else
{
Row_4_Cell_2 = new PdfPCell(new Paragraph(ggl.Substring(0, 20), customerStyle8));
}
}
else
{
Row_4_Cell_2 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_4_Cell_2.MinimumHeight = 15f;
Row_4_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_4_Cell_2);
#endregion
#region 条码
var Row_5_Cell_1 = new PdfPCell(new Paragraph("", customerStyle));
Row_5_Cell_1.MinimumHeight = 15f;
Row_5_Cell_1.Colspan = 3;
Row_5_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_5_Cell_1);
#region 插入条形码
System.DrawingCore.Bitmap qrCodeImage2 = BarCodeHelper.Generate(barCodeDto.bom.DrawngNo.Split("_")[0], 100, 15);
MemoryStream ms2 = new MemoryStream();
if (qrCodeImage2.Width > 190)
{
System.DrawingCore.Image imageBarCode = DrawingHelper.resizeImage(qrCodeImage2, new Size(190, 16));
imageBarCode.Save(ms2, System.DrawingCore.Imaging.ImageFormat.Jpeg);
}
else
{
qrCodeImage2.Save(ms2, System.DrawingCore.Imaging.ImageFormat.Jpeg);
}
iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(ms2.ToArray());
image2.SetAbsolutePosition(1f, 10f);//设置图片的绝对位置
document.Add(image2);
#endregion
#endregion
}
#endregion
}
#endregion
#region EA数据部分
else
{
#region 序号
var Row_1_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.salOrder != null ? barCodeDto.salOrder.SchedulingNo + "" : "", customerStyle));//序号
Row_1_Cell_1.MinimumHeight = 15f;
Row_1_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_1);
#endregion
#region 合同号
PdfPCell Row_1_Cell_2;
if (!string.IsNullOrEmpty(barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : ""))
{
if (barCodeDto.salOrder.BillNo.Length <= 7)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : "", customerStyle));
}
else if (barCodeDto.salOrder.BillNo.Length > 7 && barCodeDto.salOrder.BillNo.Length <= 14)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : "", customerStyle10));
}
else if (barCodeDto.salOrder.BillNo.Length > 14 && barCodeDto.salOrder.BillNo.Length <= 20)
{
Row_1_Cell_2 = new PdfPCell(new Paragraph(barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : "", customerStyle8));
}
else
{
Row_1_Cell_2 = new PdfPCell(new Paragraph((barCodeDto.salOrder != null ? barCodeDto.salOrder.BillNo + "" : "").Substring(0, 20), customerStyle8));
}
}
else
{
Row_1_Cell_2 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_1_Cell_2.MinimumHeight = 15f;
Row_1_Cell_2.Colspan = 2;
Row_1_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_2);
#endregion
#region 条码列
var Row_1_Cell_3 = new PdfPCell(new Paragraph("", customerStyle));//条码列
Row_1_Cell_3.MinimumHeight = 15f;
Row_1_Cell_3.Rowspan = 5;
Row_1_Cell_3.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_1_Cell_3);
#endregion
#region 部件号
PdfPCell Row_2_Cell_1;
if (!string.IsNullOrEmpty(barCodeDto.bom.DrawngNo))
{
if (barCodeDto.bom.DrawngNo.Length <= 7)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo, customerStyle));
}
else if (barCodeDto.bom.DrawngNo.Length > 7 && barCodeDto.bom.DrawngNo.Length <= 14)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo, customerStyle10));
}
else if (barCodeDto.bom.DrawngNo.Length > 14 && barCodeDto.bom.DrawngNo.Length <= 20)
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo, customerStyle8));
}
else
{
Row_2_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.bom.DrawngNo.Substring(0, 20), customerStyle8));
}
}
else
{
Row_2_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_2_Cell_1.MinimumHeight = 15f;
Row_2_Cell_1.Colspan = 2;
Row_2_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_2_Cell_1);
#endregion
#region 数量
var Row_2_Cell_2 = new PdfPCell(new Paragraph("数量:" + (Math.Ceiling(decimal.Parse(barCodeDto.Count) * 100) / 100) + "", headerStyle));
Row_2_Cell_2.MinimumHeight = 15f;
Row_2_Cell_2.Rowspan = 2;
Row_2_Cell_2.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_2_Cell_2);
#endregion
#region 描述
PdfPCell Row_3_Cell_1;
if (!string.IsNullOrEmpty(barCodeDto.productDetail.Name))
{
if (barCodeDto.productDetail.Name.Length <= 7)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name, customerStyle));
}
else if (barCodeDto.productDetail.Name.Length > 7 && barCodeDto.productDetail.Name.Length <= 14)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name, customerStyle10));
}
else if (barCodeDto.productDetail.Name.Length > 14 && barCodeDto.productDetail.Name.Length <= 20)
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name, customerStyle8));
}
else
{
Row_3_Cell_1 = new PdfPCell(new Paragraph(barCodeDto.productDetail.Name.Substring(0, 20), customerStyle8));
}
}
else
{
Row_3_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_3_Cell_1.MinimumHeight = 15f;
Row_3_Cell_1.Colspan = 2;
Row_3_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_3_Cell_1);
#endregion
#region 客户+箱号
PdfPCell Row_4_Cell_1;
string Box = barCodeDto.box != null ? barCodeDto.box.No + "" : "";
string customer = customers.FirstOrDefault(d => d.Id.Equals(barCodeDto.salOrder.CustomerId)).Abbreviation;
//string customer = barCodeDto.customer != null ? barCodeDto.customer.Abbreviation + "" : "";
string customerAndBox = customer +"-"+ Box;
if (!string.IsNullOrEmpty(customerAndBox))
{
if (customerAndBox.Length <= 7)
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(customerAndBox, customerStyle));
}
else if (customerAndBox.Length > 7 && customerAndBox.Length <= 14)
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(customerAndBox, customerStyle10));
}
else if (customerAndBox.Length > 14 && customerAndBox.Length <= 20)
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(customerAndBox, customerStyle8));
}
else
{
Row_4_Cell_1 = new PdfPCell(new Paragraph(customerAndBox.Substring(0, 20), customerStyle8));
}
}
else
{
Row_4_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_4_Cell_1.MinimumHeight = 15f;
Row_4_Cell_1.Colspan = 3;
Row_4_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_4_Cell_1);
#endregion
#region 模块号-ggl
string ggl = "";
if (barCodeDto.productType.IsShowProductTypeName.Contains("工艺编码"))
{
if (barCodeDto.craft != null)
{
ggl += barCodeDto.craft.No;
}
}
if (barCodeDto.productType.IsShowProductTypeName.Contains("规格"))
{
if (barCodeDto.craft != null)
{
ggl += barCodeDto.craft.Specification;
}
}
if (barCodeDto.productType.IsShowProductTypeName.Contains("模块号"))
{
if (barCodeDto.craft != null)
{
ggl += barCodeDto.productType.Name;
}
}
if (string.IsNullOrEmpty(ggl))
{
if (barCodeDto.craft != null)
{
ggl += barCodeDto.craft.Specification;
}
}
PdfPCell Row_5_Cell_1;
if (!string.IsNullOrEmpty(ggl))
{
if (ggl.Length <= 7)
{
Row_5_Cell_1 = new PdfPCell(new Paragraph(ggl, customerStyle));
}
else if (ggl.Length > 7 && ggl.Length <= 14)
{
Row_5_Cell_1 = new PdfPCell(new Paragraph(ggl, customerStyle10));
}
else if (ggl.Length > 14 && ggl.Length <= 20)
{
Row_5_Cell_1 = new PdfPCell(new Paragraph(ggl, customerStyle8));
}
else
{
Row_5_Cell_1 = new PdfPCell(new Paragraph(ggl.Substring(0, 20), customerStyle8));
}
}
else
{
Row_5_Cell_1 = new PdfPCell(new Paragraph("", customerStyle8));
}
Row_5_Cell_1.MinimumHeight = 15f;
Row_5_Cell_1.Colspan = 3;
Row_5_Cell_1.HorizontalAlignment = Element.ALIGN_CENTER;//居中
table.AddCell(Row_5_Cell_1);
#endregion
}
#endregion
}
#region 插入二维码
QRCodeGenerator qrGenerator = new QRCoder.QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(barCodeDto.Id, QRCodeGenerator.ECCLevel.Q);
QRCode qrcode = new QRCode(qrCodeData);
// qrcode.GetGraphic 方法可参考最下发“补充说明”
Bitmap qrCodeImage = qrcode.GetGraphic(2, Color.Black, Color.White, null, 2, 6, false);
System.Drawing.Image image1 = qrCodeImage;
//FileStream ms = new FileStream();
//qrCodeImage.Save(ms, ImageFormat.Jpeg);
//System.Drawing.Image image1 = DrawingHelper.resizeImage(qrCodeImage, new Size(40, 40));
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(image1, ImageFormat.Jpeg);
image.SetAbsolutePosition(186f, 14f);//设置图片的绝对位置
document.Add(image);
#endregion
document.Add(table);
document.NewPage();
}
document.Close();
//fileStream.Dispose();
//将工作薄写入文件流
// document.Write(bookStream);
//输出之前调用Seek(偏移量,游标位置) 把0位置指定为开始位置
bookStream.Seek(0, System.IO.SeekOrigin.Begin);
//Stream对象,文件类型,文件名称
return bookStream;
}
十一、pdf效果