我们使用VS下面NuGet进行Spire.Doc安装,这个安装之后我们会出现水印,是因为我们这个没有付费导致的。
我们这里使用破解的Spire.Doc就能解决这个问题打印出来就没有这个问题了
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
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;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> printList = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
#region 创建待打印word
//创建一个文档对象
Document doc = new Document();
//添加一个section
Section section = doc.AddSection();
//纸张横向
Section sec = doc.Sections[0];
sec.PageSetup.Orientation = PageOrientation.Landscape;
//添加标题
Paragraph inStoreTitle = section.AddParagraph();
inStoreTitle.AppendText("入库单");
//设置标题格式
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.Name = "titleStyle";
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.TextColor = Color.Black;
style1.CharacterFormat.FontName = "方正小标宋简体";
style1.CharacterFormat.FontSize = 24f;
doc.Styles.Add(style1);
inStoreTitle.ApplyStyle("titleStyle");
//标题居中
inStoreTitle.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
//添加单号
Paragraph pStrat = section.AddParagraph();
pStrat.AppendText("单号:rkd" + DateTime.Now.ToString("yyyyMMddHHmmss"));
//设置单号格式
ParagraphStyle style12 = new ParagraphStyle(doc);
style12.Name = "oddNumStyle";
style12.CharacterFormat.Bold = false;
style12.CharacterFormat.TextColor = Color.Black;
style12.CharacterFormat.FontName = "宋体";
style12.CharacterFormat.FontSize = 12f;
doc.Styles.Add(style12);
pStrat.ApplyStyle("oddNumStyle");
//添加表格
//Section section = doc.AddSection(); //添加section
Table table = section.AddTable(true);
//指定表格的行数和列数(11行,11列)
table.ResetCells(11, 11);
//标题行数组
string[] tabTitle = { "入库编号", "货物编号", "货物名称", "仓库名称", "供货商", "规格", "计量单位", "单价", "入库数量", "入库总额", "经手人" };
//添加表格标题行
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < 11; j++)
{
TextRange range = table[i, j].AddParagraph().AppendText(tabTitle[j]);
range.CharacterFormat.FontName = "黑体";
range.CharacterFormat.FontSize = 12;
range.CharacterFormat.TextColor = Color.Black;
range.CharacterFormat.Bold = false;
table[i, j].Paragraphs[0].Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; //水平居中
table[i, j].CellFormat.VerticalAlignment = VerticalAlignment.Middle; //垂直居中
}
}
printList.Add("rk201910222107#fxbl70001#华硕飞行堡垒7 i7 8G 256G#A仓库#联想专卖店#全新#台#6999#2#13998#小明");
//遍历打印列表并插入表格数据
for (int i = 0; i < printList.Count; i++)
{
string[] arr = printList[i].Split('#');
for (int j = 0; j < 11; j++)
{
TextRange range = table[i + 1, j].AddParagraph().AppendText(arr[j]);
range.CharacterFormat.FontName = "宋体";
range.CharacterFormat.FontSize = 10;
range.CharacterFormat.TextColor = Color.Black;
range.CharacterFormat.Bold = false;
table[i + 1, j].CellFormat.VerticalAlignment = VerticalAlignment.Middle; //垂直居中
}
}
//添加签字栏
Paragraph pEnd = section.AddParagraph();
pEnd.AppendText("制单人: 审核人: 日期: 年 月 日");
//设置签字栏格式
ParagraphStyle style13 = new ParagraphStyle(doc);
style13.Name = "endLineStyle";
style13.CharacterFormat.Bold = false;
style13.CharacterFormat.TextColor = Color.Black;
style13.CharacterFormat.FontName = "宋体";
style13.CharacterFormat.FontSize = 12f;
doc.Styles.Add(style13);
pEnd.ApplyStyle("endLineStyle");
//保存文档
doc.SaveToFile("./inStoreDoc.docx", FileFormat.Docx2013);
#endregion
System.Threading.Thread.Sleep(1000);
#region 开始打印
//初始化PrintDialog实例
PrintDialog dialog = new PrintDialog();
//设置打印对话框属性
dialog.AllowPrintToFile = true;
dialog.AllowCurrentPage = true;
dialog.AllowSomePages = true;
//设置文档打印对话框
doc.PrintDialog = dialog;
//显示打印对话框并点击确定执行打印
System.Drawing.Printing.PrintDocument printDoc = doc.PrintDocument;
if (dialog.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
DeleDOC();
#endregion
}
/// <summary>
/// 删除insStoreDOC.docx文件
/// </summary>
public void DeleDOC()
{
if (System.IO.File.Exists("./inStoreDoc.docx"))
{
System.IO.File.Delete("./inStoreDoc.docx");
}
}
}
}
下载资源Spire.Doc