iTextSharp و استفاده از قلمهای محدود فارسی
نویسنده: وحید نصیری
تاریخ: ۱۳۹۰/۰۶/۲۲ ۰۹:۲۴:۰۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System.Diagnostics;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace HeadersAndFooters
{
class Program
{
static void Main(string[] args)
{
using (var pdfDoc = new Document(PageSize.A4))
{
PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create));
pdfDoc.Open();
FontFactory.Register("c:\\windows\\fonts\\bzar.ttf");
Font bZar = FontFactory.GetFont("b zar", BaseFont.IDENTITY_H);
FontFactory.Register("c:\\windows\\fonts\\tahoma.ttf");
Font tahoma = FontFactory.GetFont("tahoma", BaseFont.IDENTITY_H);
FontSelector fontSelector = new FontSelector();
//قلم اصلی
if (bZar.Familyname != "unknown")
{
fontSelector.AddFont(bZar);
}
//قلم پیش فرض در صورت نبود تعاریف مناسب در قلم اصلی
if (tahoma.Familyname != "unknown")
{
fontSelector.AddFont(tahoma);
}
var table1 = new PdfPTable(1);
table1.WidthPercentage = 100;
table1.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
var pdfCell = new PdfPCell { RunDirection = PdfWriter.RUN_DIRECTION_RTL, Border = 0 };
pdfCell.Phrase = fontSelector.Process("نمایش مخلوطی از متن فارسی و English با هم توسط قلمی که کاراکترهای انگلیسی را پشتیبانی نمیکند");
table1.AddCell(pdfCell);
pdfDoc.Add(table1);
}
//open the final file with adobe reader for instance.
Process.Start("Test.pdf");
}
}
}
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTFAMILY, "BNazanin");
styles.LoadStyle("english", HtmlTags.FONTFAMILY, "tahoma"); var cleanTagsContent = Regex.Replace(content, @"<[^>]*>", String.Empty);
var regex = new Regex("[a-zA-Z0-9]*");
foreach (var word in cleanTagsContent.Split(' '))
if (regex.Match(word).Value == word && word.Length > 0)
{
content = content.Replace("<" + word, "#!#");
content = content.Replace(word + ">", "#^#");
content = content.Replace(word, string.Format("<span class='english'>{0}</span>", word));
content = content.Replace("#!#", "<" + word);
content = content.Replace("#^#", word + ">");
}content = content.Replace("<" + word, "#!#"); .DefaultFonts(fonts =>
{
fonts.Path(System.IO.Path.Combine(AppPath.ApplicationPath, "fonts\\irsans.ttf"),
System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf"));
fonts.Size(9);
fonts.Color(System.Drawing.Color.Black);
})