pdf教案模板替换

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using NPOI.HSSF;
using System.IO;
using System.Text;
using NPOI.XWPF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;
using Spire.Pdf;
using Spire.Pdf.General.Find;
using Spire.Pdf.Graphics;
using System.Drawing;
using Spire.License.V1_0;
public partial class admin_pwd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}

protected void Button1_Click(object sender, EventArgs e)
{

if (topic.Text.Trim() == "")
{
lblMsg.Text = "请输入课题";
return;
}

if (depart.Text.Trim() == "")
{
lblMsg.Text = "请输入系别";
return;
}

if (profession.Text.Trim() == "")
{
lblMsg.Text = "请输入专业";
return;
}

if (stuclass.Text.Trim() == "")
{
lblMsg.Text = "请输入班级";
return;
}

if (name.Text.Trim() == "")
{
lblMsg.Text = "请输入姓名";
return;
}

if (stuno.Text.Trim() == "")
{
lblMsg.Text = "请输入学号";
return;
}


if (teacher.Text.Trim() == "")
{
lblMsg.Text = "请输入指导老师";
return;
}
if (year.Text.Trim() == "")
{
lblMsg.Text = "请输入年";
return;
}
if (month.Text.Trim() == "")
{
lblMsg.Text = "请输入月";
return;
}
if (day.Text.Trim() == "")
{
lblMsg.Text = "请输入日";
return;
}
if (topic.Text.Trim() == "")
{
lblMsg.Text = "请输入课题";
return;
}


if (topictype.Text.Trim() == "")
{
lblMsg.Text = "请输入课型";
return;
}
if (topictime.Text.Trim() == "")
{
lblMsg.Text = "请输入课时";
return;
}
if (stuclass.Text.Trim() == "")
{
lblMsg.Text = "请输入班级";
return;
}
if (teacpoint.Text.Trim() == "")
{
lblMsg.Text = "请输入教学重点";
return;
}
if (techmethod.Text.Trim() == "")
{
lblMsg.Text = "请输入教学方法";
return;
}
if (techvideo.Text.Trim() == "")
{
lblMsg.Text = "请输入教学媒体";
return;
}
if (repeatrequestion.Text.Trim() == "")
{
lblMsg.Text = "请输入复习提问";
return;
}


if (newclassin.Text.Trim() == "")
{
lblMsg.Text = "请输入新科引入";
return;
}
if (blackboard.Text.Trim() == "")
{
lblMsg.Text = "请输入板书课题";
return;
}
if (explainnclass.Text.Trim() == "")
{
lblMsg.Text = "请输入讲解新课";
return;
}
if (explain2.Text.Trim() == "")
{
lblMsg.Text = "请输入讲解新课2";
return;
}
if (explain3.Text.Trim() == "")
{
lblMsg.Text = "请输入讲解新课3";
return;
}


if (solidify.Text.Trim() == "")
{
lblMsg.Text = "请输入巩固小结";
return;
}
if (assignwork.Text.Trim() == "")
{
lblMsg.Text = "请输入布置作业";
return;
}
if (designe.Text.Trim() == "")
{
lblMsg.Text = "请输入板书设计";
return;
}
if (afterclass.Text.Trim() == "")
{
lblMsg.Text = "请输入课后记";
return;
}

string pdfTemple = Server.MapPath("~/FileTemp/") + "教案格式模板.pdf";
//加载PDF测试文档
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(pdfTemple, FileFormat.PDF);

#region 指定需要查找的页面区域范围
//指定需要查找的页面区域范围
RectangleF pagerec = new RectangleF(0, 0, 500, 700);
PdfTextFindCollection findCollectiontopic = pdf.Pages[0].FindText("topic", true);
PdfTextFindCollection findCollectiondepart = pdf.Pages[0].FindText("depart", true);
PdfTextFindCollection findCollectionprofession = pdf.Pages[0].FindText("profession", true);
PdfTextFindCollection findCollectionclass = pdf.Pages[0].FindText("class", true);
PdfTextFindCollection findCollectionname = pdf.Pages[0].FindText("name", true);
PdfTextFindCollection findCollectionstuno = pdf.Pages[0].FindText("stuno", true);

PdfTextFindCollection findCollectionTeacher = pdf.Pages[0].FindText("teacher", true);
PdfTextFindCollection findCollectionyear = pdf.Pages[0].FindText("year", true);
PdfTextFindCollection findCollectionmonth = pdf.Pages[0].FindText("month", true);
PdfTextFindCollection findCollectionday = pdf.Pages[0].FindText("day", true);


PdfTextFindCollection findCollectiontopic2 = pdf.Pages[1].FindText("topic", true);
PdfTextFindCollection findCollectionclass2 = pdf.Pages[1].FindText("class", true);

PdfTextFindCollection findCollectiontopictype = pdf.Pages[1].FindText("topictype", true);
PdfTextFindCollection findCollectionTopictime = pdf.Pages[1].FindText("topictime", true);
PdfTextFindCollection findCollectionTeacpoint = pdf.Pages[1].FindText("teacpoint", true);
PdfTextFindCollection findCollectionTeacdifficut = pdf.Pages[1].FindText("teacdifficut", true);
PdfTextFindCollection findCollectionTeacmethod = pdf.Pages[1].FindText("teacmethod", true);
PdfTextFindCollection findCollectionTeacvideo = pdf.Pages[1].FindText("teacvideo", true);


//reqeatquestion
//PdfTextFindCollection findCollectionRepeatreqstion = pdf.Pages[1].FindText("repeatrequestion", true);
PdfTextFindCollection findCollectionRepeatreqstion = pdf.Pages[1].FindText("repeatquestion", true);

PdfTextFindCollection findCollectionNewclassin = pdf.Pages[1].FindText("newclassin", true);
PdfTextFindCollection findCollectionBlackboard = pdf.Pages[1].FindText("blackboard", true);


PdfTextFindCollection findCollectionExplainnclass = pdf.Pages[1].FindText("explainnclass", true);


PdfTextFindCollection findCollectionExplain2 = pdf.Pages[2].FindText("explain2", true);
PdfTextFindCollection findCollectionExplain3 = pdf.Pages[2].FindText("explain3", true);
PdfTextFindCollection findCollectionSolidify = pdf.Pages[2].FindText("solidify", true);

PdfTextFindCollection findCollectionAssignwork = pdf.Pages[2].FindText("assignwork", true);
PdfTextFindCollection findCollectionDesigne = pdf.Pages[2].FindText("designe", true);
PdfTextFindCollection findCollectionAfterclass = pdf.Pages[2].FindText("afterclass", true);
#endregion


#region 替换查找结果
//替换查找结果
PdfBrush brush = new PdfSolidBrush(Color.Black);
PdfTrueTypeFont pdfTrueTypeFont = new PdfTrueTypeFont(new Font("宋体", 10, FontStyle.Regular), true);

RectangleF textrec;
foreach (PdfTextFind findtopic in findCollectiontopic.Finds)
{
textrec = findtopic.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[0].Canvas.DrawString(topic.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

// foreach (PdfTextFind find1 in findCollectiontopic2.Finds)
// {
// textrec = find1.Bounds;
// pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
// pdf.Pages[1].Canvas.DrawString("实验", pdfTrueTypeFont,
//new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

// }

//系别
foreach (PdfTextFind find2 in findCollectiondepart.Finds)
{
textrec = find2.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本

//中文乱码
pdf.Pages[0].Canvas.DrawString(depart.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//专业
foreach (PdfTextFind find3 in findCollectionprofession.Finds)
{
textrec = find3.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本

//中文乱码
pdf.Pages[0].Canvas.DrawString(profession.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//班级
foreach (PdfTextFind find4 in findCollectionclass.Finds)
{
textrec = find4.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[0].Canvas.DrawString(stuclass.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//班级2
foreach (PdfTextFind find4 in findCollectionclass2.Finds)
{
textrec = find4.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(stuclass.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

//姓名
foreach (PdfTextFind find5 in findCollectionname.Finds)
{
textrec = find5.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[0].Canvas.DrawString(name.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//学号
foreach (PdfTextFind find5 in findCollectionstuno.Finds)
{
textrec = find5.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[0].Canvas.DrawString(stuno.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//指导老师
foreach (PdfTextFind find5 in findCollectionTeacher.Finds)
{
textrec = find5.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[0].Canvas.DrawString(teacher.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

//年
foreach (PdfTextFind find5 in findCollectionyear.Finds)
{
textrec = find5.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[0].Canvas.DrawString(year.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//月
foreach (PdfTextFind find5 in findCollectionmonth.Finds)
{
textrec = find5.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[0].Canvas.DrawString(month.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//日
foreach (PdfTextFind find5 in findCollectionday.Finds)
{
textrec = find5.Bounds;
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[0].Canvas.DrawString(day.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//课型
foreach (PdfTextFind find5 in findCollectiontopictype.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(topictype.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

//课时
foreach (PdfTextFind find5 in findCollectionTopictime.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(topictime.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

//教学重点
foreach (PdfTextFind find5 in findCollectionTeacpoint.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(teacpoint.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//教学难点
foreach (PdfTextFind find5 in findCollectionTeacdifficut.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(techdifficut.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//教学方法
foreach (PdfTextFind find5 in findCollectionTeacmethod.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(techmethod.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//教学媒体
foreach (PdfTextFind find5 in findCollectionTeacvideo.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(techvideo.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

//复习提问
foreach (PdfTextFind find5 in findCollectionRepeatreqstion.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(repeatrequestion.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

//新课引入
foreach (PdfTextFind find5 in findCollectionNewclassin.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(newclassin.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//板书课题
foreach (PdfTextFind find5 in findCollectionBlackboard.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(blackboard.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

//讲解新课
foreach (PdfTextFind find5 in findCollectionExplainnclass.Finds)
{
textrec = find5.Bounds;
pdf.Pages[1].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[1].Canvas.DrawString(explainnclass.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}


foreach (PdfTextFind find5 in findCollectionExplain2.Finds)
{
textrec = find5.Bounds;
pdf.Pages[2].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[2].Canvas.DrawString(explain2.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//
foreach (PdfTextFind find5 in findCollectionExplain3.Finds)
{
textrec = find5.Bounds;
pdf.Pages[2].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[2].Canvas.DrawString(explain3.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//巩固小节
foreach (PdfTextFind find5 in findCollectionSolidify.Finds)
{
textrec = find5.Bounds;
pdf.Pages[2].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[2].Canvas.DrawString(solidify.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

//布置作业
foreach (PdfTextFind find5 in findCollectionAssignwork.Finds)
{
textrec = find5.Bounds;
pdf.Pages[2].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[2].Canvas.DrawString(assignwork.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//板书设计
foreach (PdfTextFind find5 in findCollectionDesigne.Finds)
{
textrec = find5.Bounds;
pdf.Pages[2].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[2].Canvas.DrawString(designe.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}
//课后记
foreach (PdfTextFind find5 in findCollectionAfterclass.Finds)
{
textrec = find5.Bounds;
pdf.Pages[2].Canvas.DrawRectangle(PdfBrushes.White, textrec);//涂抹指定文本
pdf.Pages[2].Canvas.DrawString(afterclass.Text.Trim(), pdfTrueTypeFont,
new PdfSolidBrush(Color.Black), textrec.X, textrec.Y);

}

#endregion

string NewTemple = Server.MapPath("~/FileTemp/") + "教案格式模板" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
pdf.SaveToFile(NewTemple, FileFormat.PDF);

//成功之后把路径写入数据库并跳转到列表页面

string sql = "insert into [File](File_Title,File_Path,File_Type,File_Date)values('教案数据--" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "','" + NewTemple + "','文件','" + DateTime.Now + "')";
DB.ExecuteSql(sql);
Response.Redirect("admin_file_view_list.aspx");

}


}

原文地址:https://www.cnblogs.com/wugh8726254/p/14231535.html