微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在itext7中居中对齐和设置粗体文本

如何解决如何在itext7中居中对齐和设置粗体文本

我是使用 itext 生成 pdf 的新手。我遇到了一个问题,我在我的参考文献中添加了 itext7

添加带有值的 pdf。我想居中对齐并在我的 pdf 中设置粗体文本字段。我看过各种例子。但是,.SetBold 和 chunk 在 Visual Studio显示错误

这是我为 pdf 调用函数

[HttpGet]
        [Route("GetCoilWiseReport")]
        public HttpResponseMessage GetCoilWiseReport(string jwt,string fixture="",string 
         type="",string ID= "",string delivery = "",string invoice = "",string wagon = "",string location = "",string ICHP= "",string remarks="",string date="")
        {
            if (!Common.VerificationToken.VerifyJWToken(jwt))
            {
                return null;
            }

            var doc_date = DateTime.Now.Date.ToString("dd/MM/yyyy").Replace("-","/");
            var Type1 = type;
            var id1 = ID;
            var delivery1 = delivery;
            var invoice1= invoice;
            var wagon1 = wagon;
            var location1 = location;
            var ICHP1 = ICHP;
            var remarks1 = remarks;
            var date1 = date;


            string pdfDocument = @"D:\Annexure_One IT.pdf";//Utilities.ISOP_DOCS_FOLDER + 
             "MasterDocs\\supplierDespatchATP.pdf";

            

            var dateLine = "Date:" + doc_date ;
            var coilDetails = "                                          COIL Details: " + ID;
            

            

            var firstRow = "Type : " + type +"                   ID:"+ID+ "                    Delivery No:"+delivery;
            var secondRow = "Invoice No.:" + invoice+"                 Wagon No:"+wagon+"            Location:"+location;
            var thirdRow = "ICHP : " + ICHP + "                            Remarks:" + remarks;
            


            PdfDocument pdf = new PdfDocument(new PdfWriter(pdfDocument));
        
            //Add new page
            PageSize ps = PageSize.A4;
            pdfpage page = pdf.AddNewPage(ps);
            PdfCanvas canvas = new PdfCanvas(page);
            iText.Layout.Document iDoc = new iText.Layout.Document(pdf,ps);
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.COURIER);
            PdfFont fontBold = PdfFontFactory.CreateFont(StandardFonts.COURIER_BOLD);
            IList<String> text = new List<String>();
            
            text.Add("");
            text.Add(dateLine);
            text.Add("");
            text.Add("");
            text.Add(coilDetails);
            text.Add("");
            text.Add("");
            text.Add(firstRow);
            text.Add("");
            text.Add("");
            text.Add(secondRow);
            text.Add("");
            text.Add("");
            text.Add(thirdRow);
            text.Add("");
            text.Add("");
            text.Add("");
            text.Add("");
            text.Add("");
            text.Add("signature");
            ;

            foreach (String s in text)
            {
                //Add text and move to the next line
                //canvas.Newlineshowtext(s);
                if (s == "signature")
                {
                    List<string> coilURL = new List<string>();
                    List<string> coilView = new List<string>();


                    string connectionstring = Utilities.sql_DB1;// our sql DB connection
                    sqlConnection conn1 = new sqlConnection(connectionstring);

                    DataTable dt = new DataTable();

                    string query = "Select CCD_IMAGE_URL,CCD_VIEW_DESC from T_CQL_COIL_DESC 
                   where CCD_COIL_ID = '" + ID + "'";

                    //sqlConnection conn = new sqlConnection(connectionstring);
                    sqlCommand cmd = new sqlCommand(query,conn1);
                    conn1.open();




                    // create data adapter
                    sqlDataAdapter da = new sqlDataAdapter(cmd);
                    // this will query your database and return the result to your datatable


                    da.Fill(dt);

                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; (i < dt.Rows.Count); i++)
                        {
                            coilURL.Add(imgurl + dt.Rows[i]["CCD_IMAGE_URL"].ToString());
                            coilView.Add(dt.Rows[i]["CCD_VIEW_DESC"].ToString());

                            var view = "View:"+coilView[i];
                            var sig = coilURL[i];

                            iText.Layout.Element.Paragraph p1 = new 
                             iText.Layout.Element.Paragraph().Add(view);
                            //p.SetMaxHeight(20);
                            iDoc.Add(p1);

                            iText.Layout.Element.Image sigImage = new 
                            iText.Layout.Element.Image(ImageDataFactory.Create(sig));
                            sigImage.SetAutoScale(true);
                            
                            iText.Layout.Element.Paragraph p = new 
                            iText.Layout.Element.Paragraph().Add(sigImage);
                            //p.SetMaxHeight(20);
                            iDoc.Add(p);

                        }
                    }

                    


                    conn1.Close();
                    da.dispose();
                }
               
                else
                {
                    iText.Layout.Element.Paragraph para = new 
                   iText.Layout.Element.Paragraph(s);
                    para.SetFont(font);
                    para.SetFontSize(9);
                    iDoc.Add(para);
                }
            }
            //iDoc.Close();

            //Close document

            iDoc.Close();
            // Add Paragraph to document



            pdf.Close();



            HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
            httpResponseMessage.Content = new StreamContent(new FileStream(pdfDocument,FileMode.Open,FileAccess.Read));
            //httpResponseMessage.Content.Headers.ContentType = new 
            MediaTypeHeaderValue("application / vnd.openxmlformats - 
            officedocument.wordprocessingml.document");
            httpResponseMessage.Content.Headers.ContentType = new 
            MediaTypeHeaderValue("application/pdf");
            httpResponseMessage.Content.Headers.Contentdisposition = new 
            ContentdispositionHeaderValue("attachment");
            httpResponseMessage.Content.Headers.Contentdisposition.FileName = "Coil_Detail" + 
             fixture + "_" + ID + "_" + ".pdf";
            httpResponseMessage.StatusCode = HttpStatusCode.OK;

            

            return httpResponseMessage;
        }

我正在尝试使 CoilDetails 加粗并居中对齐。在当前上下文中我该怎么做。请帮忙

解决方法

使用 p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER); 将其居中对齐,并在添加后尝试使用 .SetBold() 或 .SetFont(bold) 将其设置为粗体

,

我在 c# 中使用 linqpad.net 使用 itext7 编写了一些脚本 但我没有问题可以转换为 Visual Studio。 这是 C# 中的一行

p = new Paragraph(targetText).SetFontSize(12).SetTextAlignment(TextAlignment.JUSTIFIED).SetFontColor(new DeviceRgb(0,153,0)).SetBold();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。