我认为您的要求是单击按钮以返回具有不同字体、自己的样式并需要定义徽标的 PDF(创建/打开 PDF 文件而不保存在服务器中)。[在此处输入图像描述][1]
如果你想定义标志使用标志块。
The best solution is using iTextSharp pdf tools.Its a free tool.
1)First initialize nuget packages.
itextsharp and itextsharp.xmlworker.
2)Define namespaces
using System.Text.RegularExpressions;
using iTextSharp.text;
using iTextSharp.text.pdf;
3)Start creating PDF file coding.
public ActionResult GetEmployeeListPDF()
{
MemoryStream workStream = new MemoryStream();
//file name to be created
string PDFFileName = string.Format("EmployesList.pdf");
Document doc = new Document();
//Create PDF Table with 4 columns
PdfPTable tableLayout = new PdfPTable(4);
//Create PDF Table
//file will created in this path
PdfWriter writerms = PdfWriter.GetInstance(doc, workStream);
writerms.CloseStream = false;
doc.Open();
//We have option to assign titles logos /images any thing
BaseFont f_cb = BaseFont.CreateFont("c:\\windows\\fonts\\calibrib.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writerms.DirectContent;
cb.BeginText();
cb.SetFontAndSize(f_cb, 16);
string text = "XYZ PVT. LTD";
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text, 280, 770, 0);
cb.EndText();
cb.SetLineWidth(0.5f); // Make a bit thicker than 1.0 default
cb.MoveTo(0, 755);
cb.LineTo(750, 755);
cb.SetRGBColorStroke(0, 0, 0);
cb.Stroke();
//Here if we want to define logo ,use this block
//var logoPath = Server.MapPath("~/logo.jpg");
//iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(logoPath);
//png.SetAbsolutePosition(150, 950);
//png.ScaleAbsolute(250, 70);
//png.PaddingTop = 0;
//doc.Add(png);
//Add Content to PDF
doc.Add(Add_Content_To_PDF(tableLayout));
// Closing the document
doc.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return File(workStream, "application/pdf", PDFFileName);
}
protected PdfPTable Add_Content_To_PDF(PdfPTable tableLayout)
{
float[] headers = { 30, 30, 30, 15 }; //Header Widths
tableLayout.SetWidths(headers); //Set the pdf headers
tableLayout.WidthPercentage = 100; //Set the PDF File witdh percentage
//Add Title to the PDF file at the top
tableLayout.AddCell(new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.HELVETICA, 10, 1, new iTextSharp.text.BaseColor(153, 51, 0)))) { Colspan = 6, Border = 0, PaddingBottom = 0, PaddingTop = 45, HorizontalAlignment = Element.ALIGN_CENTER });
////Add header
//tableLayout.SpacingBefore = 250f;
//If you want add any spaces ,margings or padding any styles before header you can add here.
//AddCellHeader function defining for common styles we written methos other wise you candefine heades directly here it self like
AddCellToHeader(tableLayout, "Name");
AddCellToHeader(tableLayout, "Phone No");
AddCellToHeader(tableLayout, "Gender");
AddCellToHeader(tableLayout, "Sal");
var date = DateTime.Now.ToString("dd-MM-yyyy");
////Add body
//List<EmployeeList> EmployeeList = GetEmployeeList();Fetch list of employees from database then pass that list in layout body.
//foreach (var emp in EmployeeList)
//{
// AddCellToBody(tableLayout, emp.Name);
// AddCellToBody(tableLayout, emp.Phone);
// AddCellToBody(tableLayout, emp.Gender);
// AddCellToBody(tableLayout, emp.Sal);
//}
//Just for understanding I AMBModular hardcoding
AddCellToBody(tableLayout,"ABC");
AddCellToBody(tableLayout, "9999999999");
AddCellToBody(tableLayout, "Male");
AddCellToBody(tableLayout, "20000");
AddCellToBody(tableLayout, "def");
AddCellToBody(tableLayout, "9999999999");
AddCellToBody(tableLayout, "Male");
AddCellToBody(tableLayout, "35000");
AddCellToBody(tableLayout, "xys");
AddCellToBody(tableLayout, "9999999999");
AddCellToBody(tableLayout, "Female");
AddCellToBody(tableLayout, "50000");
tableLayout.AddCell(new PdfPCell(new Phrase("Total Sal Amount", new Font(Font.FontFamily.HELVETICA, 7, 1, iTextSharp.text.BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 3, BackgroundColor = new iTextSharp.text.BaseColor(211, 211, 211),Colspan=3 });
tableLayout.AddCell(new PdfPCell(new Phrase("1,05,000", new Font(Font.FontFamily.HELVETICA, 7, 1, iTextSharp.text.BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 3, BackgroundColor = new iTextSharp.text.BaseColor(211, 211, 211) });
return tableLayout;
}
// Method to add single cell to the Header
private static void AddCellToHeader(PdfPTable tableLayout, string cellText)
{
tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.FontFamily.HELVETICA, 7, 1, iTextSharp.text.BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 3, BackgroundColor = new iTextSharp.text.BaseColor(211, 211, 211) });//if you want to define any colspan to our table.we need to write same methos with different name and define colspan or row span anything.
}
// Method to add single cell to the body
private static void AddCellToBody(PdfPTable tableLayout, string cellText)
{
tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.FontFamily.HELVETICA, 7, 1, iTextSharp.text.BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 3, BackgroundColor = iTextSharp.text.BaseColor.WHITE });//if you want to define any colspan to our table.we need to write same methos with different name and define colspan or row span anything.
}
[1]: https://i.stack.imgur.com/Io2lO.jpg