Set font for all text from Pdfptable with Itextsha

2020-05-06 13:06发布

var htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), styles);
document.Open();

BaseFont Vn_Helvetica = BaseFont.CreateFont(@"C:\Windows\Fonts\arial.ttf", 
"Identity-H", BaseFont.EMBEDDED);
Font fontNormal = new Font(Vn_Helvetica, 12, Font.NORMAL);


                foreach (var t in htmlarraylist)
                {
                    if (t is PdfPTable)
                    {                          
                       //how set fontNormal all text from Pdfptable ?
                    }

                    document.Add((IElement)t);

                }

                    document.Close();

Can someone help me please

2条回答
时光不老,我们不散
2楼-- · 2020-05-06 13:41

i have try set font family in pdf template. and show the following error also

'BaseFont' does not contain a definition for 'Cambria'

please suggest me...thanks

public void abcd()
        {
            try
            {
                string UniqueNumber = Request.QueryString["UniqueNumber"];
                string strFileName = UniqueNumber;
                string strFileExtension = ".pdf";
                string strContentType = FileManager.FileContentType_application_pdf;
                string strExportData = string.Empty;
                Document pdfDoc = new Document();
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                var output = new MemoryStream();
                BaseFont bfTimes = BaseFont.CreateFont(BaseFont.Cambria, BaseFont.CP1252, false);
                //Font ChronicleFont = new Font(bfTimes, 26f);
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                StringReader sr;
                sr = new StringReader(Convert.ToString(ExportData.UserDetails(UniqueNumber)));
                PdfWriter.GetInstance(pdfDoc, output);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                strFileName = strFileName.Replace(" - ", "-").Replace(" ", "-").Replace("--", "-");
                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}{1}", strFileName, strFileExtension));
                Response.ContentType = strContentType;
                Response.Charset = "";
                Response.BinaryWrite(output.ToArray());
                Response.Flush();
                Response.End();

            }
            catch (Exception ex)
            {
                //

            }
        }
查看更多
Fickle 薄情
3楼-- · 2020-05-06 13:42

Please try by setting font to the PdfPTable.DefaultCell property in you foreach loop

Example:
t.DefaultCell.Phrase = new Phrase() { Font = fontNormal };
查看更多
登录 后发表回答