【问题标题】:How to center align and set bold a text in itext7如何在itext7中居中对齐并设置粗体文本
【发布时间】:2021-09-13 04:42:03
【问题描述】:

我是使用 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 加粗并居中对齐。在当前情况下,我该如何做到这一点。请帮忙

【问题讨论】:

  • “我正在尝试使 CoilDetails 加粗并居中对齐。在当前上下文中我该如何做。” - 首先,您当前的上下文,您的架构not 被设计为具有特殊样式的任何文本行,因为它们都保存在一个字符串列表中。当然,如果只有这一行需要特殊处理,你可以记住它在一个变量中的列表位置,然后当你遍历列表时,检查你是否在那个位置。或者,您可以在该字符串前面加上要检查的标记。或者可以具有保存此类信息的并行结构。或者...

标签: c# reactjs pdf itext itext7


【解决方案1】:

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

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

【讨论】:

  • 与 Anuj 的答案几乎相同,但它显示了如何为相同的“p”堆叠 2​​+ 个命令;我不是想从 Anuj 获得荣誉。
【解决方案2】:

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

【讨论】:

  • 您好,您可以为当前上下文添加代码吗?我提到 .set bold 和 p.set 文本对齐在当前上下文中的 Visual Studio 中产生错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多