做出来的PDF文件用Chrome或者Edge都可以正常打开,但是使用Adobe Reader DC打开的时候报错。
PDF文件在附件.
- Code: Select all
public ActionResult MakeSamplePDF3()
{
try
{
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins(10));
PdfFont font_title = new PdfFont(PdfFontFamily.Helvetica, 18f, PdfFontStyle.Bold);
PdfFont font_normal = new PdfFont(PdfFontFamily.Helvetica, 15f, PdfFontStyle.Bold);
PdfFont font_small = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
PdfFont font_smaller = new PdfFont(PdfFontFamily.Helvetica, 8f, PdfFontStyle.Bold);
PdfFont font_small_underline = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Underline);
PdfFont font_thin = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Regular);
PdfFont font_thinner = new PdfFont(PdfFontFamily.Helvetica, 8f, PdfFontStyle.Regular);
PdfFont font_thinner_ita = new PdfFont(PdfFontFamily.Helvetica, 8f, PdfFontStyle.Italic);
float height = 63;
float x = 0;
float y = 0;
PdfStringFormat centeralign = new PdfStringFormat(PdfTextAlignment.Center);
PdfStringFormat rightalign = new PdfStringFormat(PdfTextAlignment.Right);
page.Canvas.DrawString("PDF Maker", font_title, PdfBrushes.Black, page.Canvas.ClientSize.Width / 2, height + 10, centeralign);
page.Canvas.DrawString("TEST1", font_normal, PdfBrushes.Black, page.Canvas.ClientSize.Width / 2, height + 35, centeralign);
page.Canvas.DrawString("Date:", font_small, PdfBrushes.Black, x, height + 70);
page.Canvas.DrawString(DateTime.Now.ToString("yyyy/MM/dd"), font_small_underline, PdfBrushes.Black, 50, height + 70);
//创建一个PdfGrid对象
PdfGrid grid = new PdfGrid();
//设置单元格边距
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
//设置表格默认字体
grid.Style.Font = font_thin;
//添加4行4列
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();
PdfGridRow row4 = grid.Rows.Add();
PdfGridRow row5 = grid.Rows.Add();
PdfGridRow row6 = grid.Rows.Add();
PdfGridRow row7 = grid.Rows.Add();
PdfGridRow row8 = grid.Rows.Add();
grid.Columns.Add(4);
grid.Columns[0].Format.Alignment = PdfTextAlignment.Center;
grid.Columns[1].Format.Alignment = PdfTextAlignment.Center;
grid.Columns[2].Format.Alignment = PdfTextAlignment.Center;
grid.Columns[3].Format.Alignment = PdfTextAlignment.Center;
//设置列宽
grid.Columns[0].Width = page.Canvas.ClientSize.Width * 0.3f;
grid.Columns[1].Width = page.Canvas.ClientSize.Width * 0.2333f;
grid.Columns[2].Width = page.Canvas.ClientSize.Width * 0.2333f;
grid.Columns[3].Width = page.Canvas.ClientSize.Width * 0.2333f;
//写入数据
row1.Cells[0].Value = "Customer Name";
row1.Cells[0].ColumnSpan = 4;
row1.Cells[0].Style.Font = font_normal;
row1.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.AliceBlue;
row2.Cells[0].Value = "Factors";
row2.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row2.Cells[0].Style.BackgroundBrush = PdfBrushes.LightGray;
row2.Cells[1].Value = "2022";
row2.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row2.Cells[1].Style.BackgroundBrush = PdfBrushes.LightGray;
row2.Cells[2].Value = "2021";
row2.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row2.Cells[2].Style.BackgroundBrush = PdfBrushes.LightGray;
row2.Cells[3].Value = "2020";
row2.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row2.Cells[3].Style.BackgroundBrush = PdfBrushes.LightGray;
row3.Cells[0].Value = "Overall";
row3.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
row3.Cells[1].Value = "100";
row3.Cells[2].Value = "100";
row3.Cells[3].Value = "100";
row3.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row3.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row3.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row4.Cells[0].Value = "Delivery";
row4.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
row4.Cells[1].Value = "100";
row4.Cells[2].Value = "100";
row4.Cells[3].Value = "100";
row4.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row4.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row4.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row5.Cells[0].Value = "Price & Payment Term";
row5.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
row5.Cells[1].Value = "100";
row5.Cells[2].Value = "100";
row5.Cells[3].Value = "100";
row5.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row5.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row5.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row6.Cells[0].Value = "Responsiveness";
row6.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
row6.Cells[1].Value = "100";
row6.Cells[2].Value = "100";
row6.Cells[3].Value = "100";
row6.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row6.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row6.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row7.Cells[0].Value = "Final Score";
row7.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
row7.Cells[0].Style.Font = font_small;
row7.Cells[1].Value = "100";
row7.Cells[2].Value = "100";
row7.Cells[3].Value = "100";
row7.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row7.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row7.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row7.Cells[1].Style.Font = font_small;
row7.Cells[2].Style.Font = font_small;
row7.Cells[3].Style.Font = font_small;
row7.Cells[1].Style.BackgroundBrush = PdfBrushes.OrangeRed;
row7.Cells[2].Style.BackgroundBrush = PdfBrushes.LightBlue;
row7.Cells[3].Style.BackgroundBrush = PdfBrushes.LightCyan;
row8.Cells[0].Value = "Rank";
row8.Cells[0].Style.StringFormat.Alignment = PdfTextAlignment.Left;
row8.Cells[0].Style.Font = font_small;
row8.Cells[1].Value = "100";
row8.Cells[2].Value = "100";
row8.Cells[3].Value = "100";
row8.Cells[1].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row8.Cells[2].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row8.Cells[3].Style.StringFormat.Alignment = PdfTextAlignment.Center;
row8.Cells[1].Style.Font = font_small;
row8.Cells[2].Style.Font = font_small;
row8.Cells[3].Style.Font = font_small;
row8.Cells[1].Style.BackgroundBrush = PdfBrushes.LightCyan;
row8.Cells[2].Style.BackgroundBrush = PdfBrushes.LightCyan;
row8.Cells[3].Style.BackgroundBrush = PdfBrushes.LightCyan;
//设置边框颜色、粗细
PdfBorders borders = new PdfBorders();
borders.All = new PdfPen(Color.Black, 0.1f);
foreach (PdfGridRow pgr in grid.Rows)
{
foreach (PdfGridCell pgc in pgr.Cells)
{
pgc.Style.Borders = borders;
}
}
//在指定为绘入表格
grid.Draw(page, new PointF(0, height + 100));
page.Canvas.DrawString("[Grade = Your score / Max]", font_smaller, PdfBrushes.Black, page.Canvas.ClientSize.Width - 3, height + 106, rightalign);
page.Canvas.DrawString("*Certain category may be excluded from final score calculation if data is insufficient.Remaining KPI weights will be adjusted to account for the missing weights.", font_thinner_ita, PdfBrushes.Black, 0, height + 240);
//page.Canvas.DrawString("Remaining KPI weights will be adjusted to account for the missing weights.", font_thinner_ita, PdfBrushes.Black, 50, height + 245);
//创建一个PdfGrid对象
PdfGrid grid2 = new PdfGrid();
//设置单元格边距
grid2.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
//设置表格默认字体
grid2.Style.Font = font_thin;
//添加4行4列
PdfGridRow grid2row1 = grid2.Rows.Add();
PdfGridRow grid2row2 = grid2.Rows.Add();
PdfGridRow grid2row3 = grid2.Rows.Add();
grid2.Columns.Add(2);
grid2.Columns[0].Format.Alignment = PdfTextAlignment.Center;
grid2.Columns[1].Format.Alignment = PdfTextAlignment.Center;
//设置列宽
grid2.Columns[0].Width = page.Canvas.ClientSize.Width * 0.45f;
grid2.Columns[1].Width = page.Canvas.ClientSize.Width * 0.15f;
//写入数据
grid2row1.Cells[0].Value = "Preferred Supplier (Ranking A)";
grid2row1.Cells[1].Value = ">= 90%";
grid2row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightGreen;
grid2row1.Cells[1].Style.BackgroundBrush = PdfBrushes.LightGreen;
grid2row2.Cells[0].Value = "Acceptable Supplier (Ranking B & C)";
grid2row2.Cells[1].Value = "70% - 89%";
grid2row2.Cells[0].Style.BackgroundBrush = PdfBrushes.Yellow;
grid2row2.Cells[1].Style.BackgroundBrush = PdfBrushes.Yellow;
grid2row3.Cells[0].Value = "Under Performing Supplier (Ranking D & E)";
grid2row3.Cells[1].Value = "< 70%";
grid2row3.Cells[0].Style.BackgroundBrush = PdfBrushes.Red;
grid2row3.Cells[1].Style.BackgroundBrush = PdfBrushes.Red;
//在指定为绘入表格
grid2.Draw(page, new PointF(page.Canvas.ClientSize.Width * 0.2f, height + 270));
page.Canvas.DrawString("Areas for Improvement (Rank D or E)", font_normal, PdfBrushes.Black, 0, height + 340);
PdfTextBoxField textboxbig = new PdfTextBoxField(page, "Improvement");
textboxbig.Bounds = new RectangleF(0, height + 360, page.Canvas.ClientSize.Width, 100);
textboxbig.Multiline = true;
textboxbig.ForeColor = new PdfRGBColor(Color.Black);
textboxbig.Font = font_thin;
textboxbig.Text = @"Example:"
+ Environment.NewLine + "Overall Quality: Prevent RMA cases."
+ Environment.NewLine + "Delivery: Avoid shipment delays past confirmed dates."
+ Environment.NewLine + "Responsiveness: Shorten email response rate and order schedule confirmation time."
+ Environment.NewLine + "Price & Payment Term: Support in cost down requests and better credit term.";
pdf.Form.Fields.Add(textboxbig);
page.Canvas.DrawString("Action Plan (Rank D or E)", font_normal, PdfBrushes.Black, 0, height + 480);
PdfTextBoxField textboxbig2 = new PdfTextBoxField(page, "ActionPlan");
textboxbig2.Bounds = new RectangleF(0, height + 500, page.Canvas.ClientSize.Width, 100);
textboxbig2.Multiline = true;
textboxbig2.ForeColor = new PdfRGBColor(Color.Black);
textboxbig2.Font = font_thin;
pdf.Form.Fields.Add(textboxbig2);
page.Canvas.DrawString("*Supplier whose rank falls under D or E must complete the Areas for Improvement and Action Plan sections and return the scorecard copy, signed and dated, ", font_thinner_ita, PdfBrushes.Black, 0, height + 620);
page.Canvas.DrawString("within 2 weeks of scorecard date.", font_thinner_ita, PdfBrushes.Black, 0, height + 630);
page.Canvas.DrawString("Completed By:", font_normal, PdfBrushes.Black, 0, height + 650);
PdfPen pen = new PdfPen(PdfBrushes.Black, 0.5f);
pen.DashStyle = PdfDashStyle.Solid;
page.Canvas.DrawLine(pen, new PointF(115, height + 665), new PointF(280, height + 665));
page.Canvas.DrawLine(pen, new PointF(315, height + 665), new PointF(480, height + 665));
page.Canvas.DrawString("(Signature)", font_thinner, PdfBrushes.Black, 150, height + 670);
page.Canvas.DrawString("(Print Name)", font_thinner, PdfBrushes.Black, 350, height + 670);
page.Canvas.DrawString("Title:", font_normal, PdfBrushes.Black, 0, height + 690);
page.Canvas.DrawLine(pen, new PointF(50, height + 705), new PointF(280, height + 705));
page.Canvas.DrawString("Date:", font_normal, PdfBrushes.Black, 290, height + 690);
page.Canvas.DrawLine(pen, new PointF(335, height + 705), new PointF(480, height + 705));
page.Canvas.DrawString("12-12-0002 rev3", font_thinner, PdfBrushes.Black, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height - 10, rightalign);
var stream = new MemoryStream();
pdf.SaveToStream(stream, FileFormat.PDF);
byte[] fileBytes = stream.GetBuffer();
string fileName = "Test.pdf";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
}
catch (Exception ex)
{
return View("Error");
}
}