private void AddPDF(string pdfPath, float x, float y)
{
//新建PDF文档,添加一页
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(pdfPath);
if (doc.Pages.Count > 0)
{
foreach (PdfPageBase p in doc.Pages)
{
//Image image = Image.FromFile(imgUrl);
//加载缩放后的图片到PdfImage对象
//PdfImage pdfImage = PdfImage.FromImage(image);
//float x = PdfPageSize.A4.Width;
//在指定位置绘入图片
// p.Canvas.DrawImage(pdfImage, x, y, fwidth, fheight);
// PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rect1, new PdfRGBColor(Color.Red ), new PdfRGBColor(Color.Transparent), PdfLinearGradientMode.Horizontal);//填充图形颜色
//创建Rectangle2D对象,并指定图形在PDF页面中的大小、位置
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("宋体", 12f), true);
PdfFontBase font = new PdfFont(PdfFontFamily.Symbol, 12);
PdfPen pen = new PdfPen(Color.Red);
//审批级别--------------------------------------------------------------------------------------
Rectangle rectAudit1 = new Rectangle((int)x, (int)y + 15 * 0, 35, 15);
p.Canvas.DrawRectangle(pen, rectAudit1);//绘制矩形到页面
p.Canvas.DrawString("设计:", font1, PdfBrushes.Red, new PointF(x + 2, y + 15 * 0));
Rectangle rectAudit2 = new Rectangle((int)x, (int)y + 15 * 1, 35, 15);
p.Canvas.DrawRectangle(pen, rectAudit2);//绘制矩形到页面
p.Canvas.DrawString("审核:", font1, PdfBrushes.Red, new PointF(x + 2, y + 15 * 1));
Rectangle rectAudit3 = new Rectangle((int)x, (int)y + 15 * 2, 35, 15);
p.Canvas.DrawRectangle(pen, rectAudit3);//绘制矩形到页面
p.Canvas.DrawString("批准:", font1, PdfBrushes.Red, new PointF(x + 2, y + 15 * 2));
//审批人--------------------------------------------------------------------------------------
Rectangle rectName1 = new Rectangle((int)x + 35, (int)y + 15 * 0, 42, 15);
p.Canvas.DrawRectangle(pen, rectName1);//绘制矩形到页面
p.Canvas.DrawString("赵志丽", font1, PdfBrushes.Red, new PointF(x + +35 + 2, y + 15 * 0));
Rectangle rectName2 = new Rectangle((int)x + 35, (int)y + 15 * 1, 42, 15);
p.Canvas.DrawRectangle(pen, rectName2);//绘制矩形到页面
p.Canvas.DrawString("何文静", font1, PdfBrushes.Red, new PointF(x + +35 + 2, y + 15 * 1));
Rectangle rectName3 = new Rectangle((int)x + 35, (int)y + 15 * 2, 42, 15);
p.Canvas.DrawRectangle(pen, rectName3);//绘制矩形到页面
p.Canvas.DrawString("吕小波", font1, PdfBrushes.Red, new PointF(x + 35 + 2, y + 15 * 2));
//审批日期--------------------------------------------------------------------------------------
Rectangle rectTime1 = new Rectangle((int)x + 35 + 42, (int)y + 15 * 0, 100, 15);
p.Canvas.DrawRectangle(pen, rectTime1);//绘制矩形到页面
p.Canvas.DrawString("2022-03-14 07:63", font1, PdfBrushes.Red, new PointF(x + 35 + 42 + 2, y + 15 * 0));
Rectangle rectTime2 = new Rectangle((int)x + 35 + 42, (int)y + 15 * 1, 100, 15);
p.Canvas.DrawRectangle(pen, rectTime2);//绘制矩形到页面
p.Canvas.DrawString("2022-03-14 09:23", font1, PdfBrushes.Red, new PointF(x + 35 + 42 + +2, y + 15 * 1));
Rectangle rectTime3 = new Rectangle((int)x + 35 + 42, (int)y + 15 * 2, 100, 15);
p.Canvas.DrawRectangle(pen, rectTime3);//绘制矩形到页面
p.Canvas.DrawString("2022-03-14 10:41", font1, PdfBrushes.Red, new PointF(x + 35 + 42 + +2, y + 15 * 2));
}
}
//保存文档
string newpath = "g:\\a.pdf";
doc.SaveToFile(newpath);
}