天天看点

.net Core 使用iTextSharp 创建PDF文件并且导出准备工作

准备工作

 NuGet 安装 iTextSharp 和 System.Text.Encoding.CodePages

代码,现成的,直接copy拿去用吧:

[HttpGet]
        [Route("byCustomerProductCode/{customerProductCode}")]
        public FileResult ExportSignARPAPdf(string customerProductCode)
        {
            //定义一个Document,并设置页面大小为A4,竖向
            Document doc = new Document(PageSize.A4, 72, 72, 60, 0);

                string str1 = System.Environment.CurrentDirectory;
                string filePath = (str1 + "/File/" + customerProductCode + ".pdf");//路径
                                                                               
                PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));
                doc.Open();

                #region 创建字体
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                BaseFont baseFT = BaseFont.CreateFont("C:\\Windows\\Fonts\\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//获取系统的字体

                Font font = new Font(baseFT, 10); //写入一个段落, Paragraph
                Font title = new Font(baseFT, 16);
                Font bold = new Font(baseFT, 10, 1); //字体加粗
                Font italics = new Font(baseFT, 10, 2); //字体斜体
                Font underline = new Font(baseFT, 10, 4); //下划线
                Font boldAndItalics = new Font(Font.FontFamily.TIMES_ROMAN, 11f, Font.BOLD | Font.ITALIC, BaseColor.BLACK);

                #endregion

                #region 文本数据
                string heardText = "Restricted Project Information Acknowledgement";

                string text = "You, the undersigned, are involved in the development of a highly confidential Apple project code-named [" + customerProductCode + "]. The following guidelines are intended to protect the secrecy of the project. Please acknowledge that you have read and agree to follow the guidelines by signing below.";

                List clauseList = new List(true, 20);
                clauseList.Add(new ListItem("Only refer to Apple by the code-name provided by your employer.\r\r", font));
                clauseList.Add(new ListItem("Only refer to the project by the code - name provided by your employer.\r\r", font));
                clauseList.Add(new ListItem("Do not disclose any confidential information related to the project or any confidential prototypes, drawings, images, or other materials to anyone other than those that have a need to know in connection with the project and who have been approved by Apple.To determine whether someone has been approved, please contact your project manager.\r\r", font));
                clauseList.Add(new ListItem("Comply with the Apple Security Requirements and any confidentiality agreements related to the project.Ask your project manager if you do not know what these requirements are.\r\r", font));
                clauseList.Add(new ListItem("Your obligation to maintain the confidentiality of the project continues even after your work on the project or your service to your employer has ended.\r\r", font));
                clauseList.Add(new ListItem("Unauthorized use or disclosure of confidential information or prototypes related to the project is a violation of the confidentiality agreement between Apple and your employer and may be a violation of the law.\r\r", font));

                string agreedText = "Acknowledged and agreed:";

                string signature = "Signature:  ___________________________________\r\r";
                string name = "     Name:  ___________________________________\r\r";
                string titleDown = "    Title:  ___________________________________\r\r";
                string companyUs = "  Company:  Shenzhen INCUBE Automation Co., Ltd.\r\r";
                string companyCh = "深圳市智立方自动化设备有限公司     ";
                string date = "\r     Date:  ___________________________________\r\r";

                #endregion


                #region 给文本添加字体
                Paragraph heardTexts = new Paragraph(heardText, title);
                Paragraph texts = new Paragraph(text, font);
                Paragraph agreed = new Paragraph(agreedText, boldAndItalics);
                Paragraph signatures = new Paragraph(signature, font);
                Paragraph names = new Paragraph(name, font);
                Paragraph titleDowns = new Paragraph(titleDown, font);
                Paragraph companyUss = new Paragraph(companyUs, font);
                Paragraph companyChs = new Paragraph(companyCh, underline);
                Paragraph dates = new Paragraph(date, font);

                #endregion


                #region 缩进
                heardTexts.IndentationLeft = 20;
                heardTexts.IndentationRight = 20;

                texts.IndentationLeft = 20;
                texts.IndentationRight = 20;

                clauseList.IndentationLeft = 40;
                clauseList.IndentationRight = 40;

                agreed.IndentationLeft = 20;
                agreed.IndentationRight = 20;

                signatures.IndentationLeft = 70;
                names.IndentationLeft = 70;
                titleDowns.IndentationLeft = 70;
                companyUss.IndentationLeft = 70;
                companyChs.IndentationLeft = 130;
                dates.IndentationLeft = 70;
                #endregion


                #region 文本添加到PDF
                doc.Add(heardTexts);
                doc.Add(new Paragraph("\r"));//换行
                doc.Add(texts);
                doc.Add(new Paragraph("\r"));//换行
                doc.Add(clauseList);
                doc.Add(agreed);
                doc.Add(new Paragraph("\r"));//换行
                doc.Add(signatures);
                doc.Add(names);
                doc.Add(titleDowns);
                doc.Add(companyUss);
                doc.Add(companyChs);
                doc.Add(dates);

                #endregion

                //关闭document
                doc.Close();
                return File(System.IO.File.ReadAllBytes(filePath), "application/octet-stream", customerProductCode + ".pdf");
            }