.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");
            }

首先,需要安装 iTextSharp 库。可以通过 NuGet 包管理器安装,或者从 iTextSharp 的官方网站下载并手动安装。 下面是使用 iTextSharp 将 Word 文件转换为 PDF 文件的基本步骤: 1. 使用 Microsoft.Office.Interop.Word 库打开 Word 文件,并将其转换为可读取的格式。示例代码如下: ```csharp using Microsoft.Office.Interop.Word; using System.IO; Application wordApp = new Application(); Document wordDoc = wordApp.Documents.Open("path/to/word/file"); wordDoc.SaveAs2(Path.ChangeExtension(wordDoc.FullName, ".html"), WdSaveFormat.wdFormatHTML); wordDoc.Close(); wordApp.Quit(); ``` 2. 使用 iTextSharp 库读取 HTML 文件,并将其转换为 PDF 文件。示例代码如下: ```csharp using iTextSharp.text; using iTextSharp.text.pdf; string htmlFile = "path/to/html/file"; string pdfFile = "path/to/pdf/file"; using (var ms = new MemoryStream()) { using (var document = new Document(PageSize.A4, 72f, 72f, 72f, 72f)) { PdfWriter writer = PdfWriter.GetInstance(document, ms); document.Open(); using (var sr = new StreamReader(htmlFile)) { var parsedHtmlElements = HTMLWorker.ParseToList(sr.ReadToEnd(), null); foreach (var htmlElement in parsedHtmlElements) { document.Add(htmlElement as IElement); } } document.Close(); } File.WriteAllBytes(pdfFile, ms.ToArray()); } ``` 注意,上述示例代码中使用的是 iTextSharp 版本5.x。如果使用的是 iTextSharp 版本4.x,需要改为使用 XMLWorkerHelper.ParseXHtml 方法将 HTML 文件转换为 PDF 文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值