iTextSharp won't use font unless it is installed - c#

I have my PDF file being generated correctly with Chinese characters if I have the font installed in my system font directory. When I actually deploy this to the Azure website, I won't be able to install the font.
I added the font to the project and it is getting deployed, the application finds the path, but iTextSharp does not use it when the PDF is generated.
The current code which works -
FontFactory.Register("c:/windows/fonts/ARIALUNI.TTF");
This does not work but the path is good -
string arialuniTffFont = System.IO.Path.Combine(Server.MapPath("~/bin/fonts/arialuni.ttf"));
FontFactory.Register(arialuniTffFont);
UPDATED:
private void CreatePDF(IList<string> HTMLData, string fileName, bool rotate)
{
//Create PDF document
Document doc = new Document(PageSize.A4, 36, 36, 36, 36);
if (rotate)
{
doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
}
HTMLWorker parser = new HTMLWorker(doc);
string fontpath = Server.MapPath("/Fonts/arialuni.ttf");
FontFactory.Register(fontpath);
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.TABLE, HtmlTags.SIZE, "6pt");
styles.LoadTagStyle(HtmlTags.H3, HtmlTags.SIZE, "10pt");
styles.LoadTagStyle(HtmlTags.H5, HtmlTags.SIZE, "6pt");
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
parser.SetStyleSheet(styles);
PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create));
doc.Open();
//Try/Catch removed
foreach (var s in HTMLData) {
StringReader reader = new StringReader(s);
parser.Parse(reader);
doc.NewPage();
}
doc.Close();
}
The entire routine that does not produce Chinese characters.

The file that I was trying to use was the one that was directly downloaded from the internet. I did not realize that it needed to be extracted. Once I figured this out and got the correct file, it worked correctly without any code changes.

Related

html tags not converting when html to pdf convert using itextSharp

I am trying to create a pdf using iTextSharp and it did it. But it is also printing html tags in the pdf instead of making it as a design around text
Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
Paragraph Text = new Paragraph("<b>Hiii</b>");
pdfDoc.Add(Text);
pdfWriter.CloseStream = false;
pdfDoc.Close();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
Output of this is Hiii with bold tags the way it is looking in the code , but I want Hiii
You expect iText to parse HTML tags in the text of its layout elements (Paragraph, ...) but iText does not do so. To get "Hiii" instead of "<b>Hiii</b>" in iText you essentially have two options:
Explicitly use a bold font for bold text and a normal font for regular text.
var phrase = new Phrase();
phrase.Add(new Chunk("REASON(S) FOR CANCELLATION:", boldFont));
phrase.Add(new Chunk(" See Statutoryreason(s) designated by Code No(s) 1 on the reverse side hereof", normalFont));
This is explained in detail in the answers to the question #Ratheesh pointed to in a comment. In particular you can get a bold font either by explicitly naming a bold font, e.g.
var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
Or you can ask iText to find a bold font of a font family; if iText cannot find it, it will use create a poor-man's-bold version of the regular font by drawing a line along the outlines of the font glyphs:
Font verdanaBold = FontFactory.GetFont("Verdana", 7f, Font.BOLD);
Use the XMLWorker to parse XHTML and generate accordingly styled layout elements from it.
This also has been explained in many answers on stack overflow, see for example the CreateSimpleHtmlParagraph method in this answer:
private static Phrase CreateSimpleHtmlParagraph(String text)
{
var p = new Phrase();
var mh = new MyElementHandler();
using (TextReader sr = new StringReader("<html><body><p>" + text + "</p></body></html>"))
{
XMLWorkerHelper.GetInstance().ParseXHtml(mh, sr);
}
foreach (var element in mh.elements)
{
foreach (var chunk in element.Chunks)
{
p.Add(chunk);
}
}
return p;
}
(The helper class MyElementHandler is shown in the referenced answer.)

c# iTextSharp converting WMF to PDF embedding fonts

I am fairly new to C#, but am using it to convert some older files in a WMF/PMF format to PDF. I have the script working, but some of the fonts in the original document are not coming through. For example, some of these old documents are payroll check runs and the checks use a special MICR font (where the account/routing numbers are displayed). This MICR line comes through the conversion as what appears to be a random base font.
I am simply using iTextSharp to convert the WMF to an image, then adding the image as a page in the PDF.
I have researched embedding fonts, but my problem is that I might not know what the original font name was. These files could be any number of things, and they could be quite old. Is there a way to include a font library that would expand iTextSharp's basic fonts so they are more likely to be recognized during the conversion?
This is the function performing the conversion. All of the WMF files (one for each check) are put in a directory and the function loads them all into a single PDF:
static bool BuildPDF(string pDecodeDir)
{
Document pdfDoc = new Document();
DirectoryInfo decodeDir = new DirectoryInfo(pDecodeDir);
int vCount = 0;
foreach (var file in decodeDir.GetFiles("*.WMF"))
{
try
{
iTextSharp.text.Image img1 = ImgWMF.GetInstance(file.FullName);
if (vCount == 0)
{
// in order to inherit the document size properly, we need to load the first image before creating the PDF object
pdfDoc = new Document(img1);
PdfWriter.GetInstance(pdfDoc, new FileStream(targetPath, FileMode.Create));
pdfDoc.Open();
}
else
{
pdfDoc.NewPage();
}
Console.WriteLine("Adding page {0}: {1}", vCount.ToString(), file.Name);
img1.SetAbsolutePosition(0, 0);
pdfDoc.Add(img1);
vCount++;
}
catch (System.Exception docerr)
{
Console.WriteLine("Doc Error: {0}", docerr.Message);
return false;
}
}
Console.WriteLine("{0} created!", targetPath);
pdfDoc.Close();
return true;
}

Main content overlaps with right margin while trying to convert HTML to PDF with iText (c#)

I'm trying to convert html to pdf with iText library, but while the pdf creation works well, the margins aren't.
I try to manually set the margins, but it seems the program not only ignores me, but worse, the main content overlaps the right side as you can see in the image
Here is a part of my code (cabecera is the string where html is located)
Main page
string file = string.Format("{0}LEGO_{1}_{2}_{3}_{4}_{5}.pdf",Constants.WebConfig.urlCopiaPdf, fecha.Day, fecha.Month, fecha.Year, fecha.Hour, fecha.Minute);
string fileWeb = string.Format("{0}LEGO_{1}_{2}_{3}_{4}_{5}.pdf", Constants.WebConfig.urlCopiaPdfWeb, fecha.Day, fecha.Month, fecha.Year, fecha.Hour, fecha.Minute);
string fileHTML = string.Format("{0}LEGO_{1}_{2}_{3}_{4}_{5}.html",Constants.WebConfig.urlCopiaPdf, fecha.Day, fecha.Month, fecha.Year, fecha.Hour, fecha.Minute);
//string file = string.Format("c:\\docs\\LEGO{0}_{1}_{2}_{3}_{4}.pdf", fecha.Day, fecha.Month, fecha.Year, fecha.Hour, fecha.Minute);
StringReader sr = new StringReader(cabecera);
Document pdfDoc = new Document(PageSize.A4, 40f, 40f, 60f, 20f);
pdfDoc.SetMarginMirroring(false);
HeaderAndFooter PageEventHandler = new HeaderAndFooter();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(file, FileMode.Create));
writer.PageEvent = PageEventHandler;
writer.SetMargins(40f, 40f, 60f, 20f);
writer.SetMarginMirroring(false);
pdfDoc.Open();
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Yes, I duplicated the setmargins, and setmarginmirroring, I was just trying if anything would work...I even tried creating a PdfPageEventHelper and write the following code as a test
public class HeaderAndFooter : PdfPageEventHelper
{
public HeaderAndFooter()
: base()
{
}
public override void OnEndPage(PdfWriter writer, Document document)
{
PdfContentByte cb = writer.DirectContent;
ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, new Phrase(String.Format("dsadsadsadsad", writer.PageNumber)), (document.Right - 40), (document.Bottom - document.Top) / 2, 0);
}
}
But nothing works...Can anyone help me?
Thanks
At the end my problem was the html I was using to create the PDF wasn't well formed (it didn't have an html, head and body tags,the template wasn't mine but...I should checked it before ask anything ), so after adding what it needs works perfectly well.

Export arabic data into pdf asp.net showing error

When I export ARABIC data into pdf.Microsoft adobereader showing error.Adobe reader could not open file because it is either not a supported file.My code is following asp.net c#.Guide me
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Document doc = new Document(PageSize.LETTER);
doc.Open();
//Sample HTML
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(#"<p>This is a test: <strong>مسندم</strong></p>");
//Path to our font
string arialuniTff = Server.MapPath("~/tradbdo.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(arialuniTff);
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Traditional Arabic Bold");
//Set the default encoding to support Unicode characters
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
//Parse our HTML using the stylesheet created above
List<IElement> list = HTMLWorker.ParseToList(new StringReader(stringBuilder.ToString()), ST);
//Loop through each element, don't bother wrapping in P tags
foreach (var element in list)
{
doc.Add(element);
}
doc.Close();
Response.Write(doc);
Response.End();
}
I found the following article which shows how to correctly export and display Arabic content via the iTextSharp library: http://geekswithblogs.net/JaydPage/archive/2011/11/02/using-itextsharp-to-correctly-display-hebrew--arabic-text-right.aspx.
Here is the code sample that you can try:
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;
public void WriteDocument()
{
//Declare a itextSharp document
Document document = new Document(PageSize.A4);
//Create our file stream and bind the writer to the document and the stream
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(#"C:\Test.Pdf", FileMode.Create));
//Open the document for writing
document.Open();
//Add a new page
document.NewPage();
//Reference a Unicode font to be sure that the symbols are present.
BaseFont bfArialUniCode = BaseFont.CreateFont(#"C:\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//Create a font from the base font
Font font = new Font(bfArialUniCode, 12);
//Use a table so that we can set the text direction
PdfPTable table = new PdfPTable(1);
//Ensure that wrapping is on, otherwise Right to Left text will not display
table.DefaultCell.NoWrap = false;
//Create a regex expression to detect hebrew or arabic code points
const string regex_match_arabic_hebrew = #"[\u0600-\u06FF,\u0590-\u05FF]+";
if (Regex.IsMatch("مسندم", regex_match_arabic_hebrew, RegexOptions.IgnoreCase))
{
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
}
//Create a cell and add text to it
PdfPCell text = new PdfPCell(new Phrase("مسندم", font));
//Ensure that wrapping is on, otherwise Right to Left text will not display
text.NoWrap = false;
//Add the cell to the table
table.AddCell(text);
//Add the table to the document
document.Add(table);
//Close the document
document.Close();
//Launch the document if you have a file association set for PDF's
Process AcrobatReader = new Process();
AcrobatReader.StartInfo.FileName = #"C:\Test.Pdf";
AcrobatReader.Start();
}
The iTextSharp.text.Document is a class used to help bridge human concepts like Paragraph and Margin into PDF concepts. The bridge part is important. It is not a PDF file in any way so it should never be treated as a PDF. Doing so would be like treating System.Drawing.Graphics as if it were an image. This leads to one of your problems on the second to last line of code that tries to treat the Document as if it were a PDF by sending it directly to the output stream:
//This won't work
Response.Write(doc);
You will find many, many tutorials out there that do this and they are all wrong. Fortunately (or unfortunately), PDF is forgiving and allows junk data at the end so only a handful of PDF fail and people assume there was some other problem.
Your other problem is that you are missing a PdfWriter. If Document is the bridge, PdfWriter is the actual construction worker that puts that PDF together. It, however, is also not a PDF. Instead, it needs to be bound to a stream like a file, in-memory or the HttpResponse.OutputStream.
Below is some code that shows this off. I very strongly recommend separating your PDF logic from your ASPX logic. Do all of you PDF stuff first and get an actual "something" that represents a PDF, then do something with it.
At the beginning we declare a byte array that we'll fill in later. Next we create a System.IO.MemoryStream that will be used to write the PDF to. After creating the Document we then create a PdfWriter that's bound to the Document and our stream. Your internal code is the same and although I didn't test it it appears correct. Right before we're done with our MemoryStream we grab the active bytes into our byte array. Lastly we use the BinaryWrite() method to send our raw binary PDF to the requesting client.
//At the end of this bytes will hold a byte array representing an actual PDF file
Byte[] bytes;
//Create a simple in-memory stream
using (var ms = new MemoryStream()){
using (var doc = new Document()) {
//Create a new PdfWriter bound to our document and the stream
using (var writer = PdfWriter.GetInstance(doc, ms)) {
doc.Open();
//This is unchanged from the OP's code
//Sample HTML
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(#"<p>This is a test: <strong>مسندم</strong></p>");
//Path to our font
string arialuniTff = Server.MapPath("~/tradbdo.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(arialuniTff);
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Traditional Arabic Bold");
//Set the default encoding to support Unicode characters
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
//Parse our HTML using the stylesheet created above
List<IElement> list = HTMLWorker.ParseToList(new StringReader(stringBuilder.ToString()), ST);
//Loop through each element, don't bother wrapping in P tags
foreach (var element in list) {
doc.Add(element);
}
doc.Close();
}
}
//Right before closing the MemoryStream grab all of the active bytes
bytes = ms.ToArray();
}
//We now have a valid PDF and can do whatever we want with it
//In this case, use BinaryWrite to send it directly to the requesting client
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.BinaryWrite(bytes);
Response.End();

How to Create PDF file with Tamil Font by using itextsharp in C#?

we are creating pdf files in our C# application by passing TAMIL text(one of the indian language).So, I already installed AVVAIYAR.TTF(one of the tamil font) font for my tamil language font.But when i run the below mentioned pgm, the created pdf file does not contain any tamil font display.It shows empty lines instead of the tamil text...
C# Code:
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(#"C:\pdfout.pdf", FileMode.Create));
document.Open();
PdfContentByte pcb = writer.DirectContent;
Font ft = new Font();
FontFactory.Register(#"C:\WINDOWS\Fonts\AVVAIYAR.TTF", "AVVAIYAR");
ft = FontFactory.GetFont("AVVAIYAR");
Paragraph pr1 = new Paragraph("இது முதல் பேரா", ft);
Paragraph pr2 = new Paragraph("This is a Sub Paragraph");
Paragraph pr3 = new Paragraph("This is the Second Paragraph");
document.Add(pr1);
document.Add(pr2);
document.Add(pr3);
document.Close();
my output pdf file is :
<blank line>
This is a Sub Paragraph
This is the Second Paragraph
And also i have to support some more indian languages to create the PDF files.
Can you please try the following code snippet?
string fontpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\AVVAIYAR.TTF";
BaseFont basefont = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, true);
Font AVVAIYARFont = new iTextSharp.text.Font(basefont, 24, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLUE); /*For test color blue is placed with some foramtting.*/
Paragraph pr1 = new Paragraph("இது முதல் பேரா", AVVAIYARFont);
This should work...
An out of the box thought, iText has not full support for all the indic languages at moment...See here they say that they dont have enough volunteers to support this.
I have put a reference to ARIALUNI.TTF.I tried like this,
string fontpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\ARIALUNI.TTF";
BaseFont basefont = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, true);
Font font = new iTextSharp.text.Font(basefont, 24, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLUE);
Paragraph pr1 = new Paragraph("இது முதல் பேரா", AVVAIYARFont);
So, now tamil font is displayed in the pdf file.But simple simple spelling mistake... so i am doing some read on that issue...
Procees 1:
step 1:Go to http://software.nhm.in/services/converter website
step2:paste this word படிவம்-டி ஊதியம் சீட்டு /விடுப்பு அட்டை
step3:1.change the first drop down to Unicode , 2.change the second drop down to TAB
step5:click the button converter
step6.text will be alligned.
step 7:main process start from hear
1.change the second drop down to TSCII and click on convert.(dont panic no changes would exists)
2.now change the first drop down to Unicode automatticaly the tamil text would change to "ÀÊÅõ-Ê °¾¢Âõ º£ðÎ /Å¢ÎôÒ «ð¨¼";
3.copy the content and place it in notepad.
Process 2:
1.First download TSC-Sri.ttf(http://www.eaglefonts.com/download.php?action=zip&image_id=129859)
2.save or move it in c:\Windows\fonts
3.finally Pate the below code in the c# program
string fontpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\TSC-Sri.ttf";
BaseFont basefont = BaseFont.CreateFont(fontpath, BaseFont.IDENTITY_H, true);
iTextSharp.text.Font font = new iTextSharp.text.Font(basefont, 24, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLUE);
Paragraph pr1 = new Paragraph("ÀÊÅõ-Ê °¾¢Âõ º£ðÎ /Å¢ÎôÒ «ð¨¼ ", font);
4.run the itextsharp pdf program output as excepted 100% working.
A viable alternative is Quest PDF.
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
Document.Create(container =>
{
container.Page(page =>
{
page.Size(PageSizes.A4);
page.Margin(2, Unit.Centimetre);
page.PageColor(Colors.White);
page.DefaultTextStyle(x => x.FontSize(20));
page.DefaultTextStyle(x => x.FontFamily("Vijaya"));
page.Header()
.Text("தமிழ் PDF!")
.SemiBold().FontSize(36).FontColor(Colors.Blue.Medium);
page.Content()
.PaddingVertical(1, Unit.Centimetre)
.Column(x =>
{
x.Spacing(20);
x.Item().Text("தமிழ் (Tamil language) தமிழர்களினதும் தமிழ் பேசும் பலரின் தாய்மொழி ஆகும். தமிழ், உலகின் உள்ள முதன்மையான மொழிகளில் ஒன்றும் செம்மொழியும் ஆகும்.");
});
page.Footer()
.AlignCenter()
.Text(x =>
{
x.Span("பக்கம் ");
x.CurrentPageNumber();
});
});
})
.GeneratePdf("தமிழ்.pdf");

Categories

Resources