Well, I am trying to export some data from my WinForms application to a PDF file. I have downloaded some fonts which support Turkish language characters. In the Turkish language, there are some letters like ç,ğ,ş,ö,ü,ı. My code has no problems with showing ç,ö,ü but somehow when the user inputs ğ, ş or ı, these letters get represented as blank in the PDF file.
My code is below:
Document doc= new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri=PdfWriter.GetInstance(doc, new FileStream("Test.pdf", FileMode.Create));
doc.Open();
BaseFont bf = BaseFont.CreateFont(#"C:\aller.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
Paragraph p1 = new Paragraph(new Chunk("çğşöüı", font));
doc.AddLanguage("tr-TR");
wri.SetLanguage("tr-TR");
doc.Add(p1);
doc.Close();
So, where is my mistake?
after tryings, I found the answer;
BaseFont bF = BaseFont.CreateFont("C:\\windows\\fonts\\arial.ttf", "windows-1254", true);
iTextSharp.text.Font f = new iTextSharp.text.Font(bF, 12f, iTextSharp.text.Font.NORMAL);
Chunk c = new Chunk();
c.Font = f;
iTextSharp.text.Document document = new iTextSharp.text.Document();
PdfWriter.GetInstance(document, new FileStream(#"C:\gorsel.pdf", FileMode.Create));
string text = "küçük harf türkçe karakterler : ç ğ ı ö ş ü \n" +
" BÜYÜK TÜRKÇE KARAKTERLER : Ç Ğ İ Ö Ş Ü";
c.Append(text);
document.Open();
document.Add(new Paragraph(c));
document.Close();
now I can use all special characters in my PDF file.
Related
I can add a text to an existing pdf as it is explained in the url below.
But the texts stay below the images in the pdf when I add them. How can I fix this problem?
ITextSharp insert text to an existing pdf
Edit :
public void createFromPDF(string mapPath)
{
string oldFile = mapPath.Replace("Home", "") + "Content\\Uploads\\fiyat-listesi.pdf";// "oldFile.pdf";
string newFile = mapPath.Replace("Home", "") + "Content\\Uploads\\new.pdf";//"newFile.pdf";
// open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
// select the font properties
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(Color.RED);
cb.SetFontAndSize(bf, 8);
// write the text in the pdf content
cb.BeginText();
string text = "Some random blablablabla...";
// put the alignment and coordinates here
cb.ShowTextAligned(1, text, 520, 640, 0);
cb.EndText();
cb.BeginText();
text = "Other random blabla...";
// put the alignment and coordinates here
cb.ShowTextAligned(2, text, 100, 200, 0);
cb.EndText();
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
document.NewPage();
Paragraph p = new Paragraph("aaaaaaaaaaaaaaaaaa", new Font(bf));
document.Add(p);
PdfImportedPage page2 = writer.GetImportedPage(reader, 2);
cb.AddTemplate(page2, 0, 0);
document.NewPage();
Paragraph pwe = new Paragraph("aaaaaaaaaaaaaaaaaa", new Font(bf));
document.Add(p);
cb.EndLayer();
PdfImportedPage page3 = writer.GetImportedPage(reader, 3);
cb.AddTemplate(page3, 0, 0);
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
}
Two remarks:
You add the text first, then you add the image. Hence the image covers the text. That's elementary logic. If you switch the order and add the image first, then the text, the text will cover the image. That's pure common sense.
You manipulate an existing PDF by importing a PdfImportedPage and PdfWriter. That proves thaf you didn't read the documentation. You should use PdfStamper instead!
Your code is too complex. Switch to PdfStamper and add text using the ColumnText object. Don't use BeginText() / EndText(). Also: why are you using EndLayer()??? Do you have any idea what that method is meant for?
I'm still a beginner in C# and I want to know is there an option of writing slavic letters č,ć,š,ž in PDF using iTextSharp. I was reading other posts about it but I can't apply their solution to my problem, maybe it's a bit to complicate to me as a beginner. This is my code:
SaveFileDialog pdfFile = new SaveFileDialog();
pdfFile.Filter = "PDF|*.pdf";
pdfFile.Title = "Spremi PDF";
pdfFile.FileName = "Ispit";
if (pdfFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Document document = new Document(iTextSharp.text.PageSize.LETTER, 25, 25, 35, 35);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFile.FileName, FileMode.Create));
document.Open();
foreach (List<string> question in questions)
{
document.NewPage();
foreach (string field in question)
{
document.Add(new Paragraph(field));
}
}
document.Close();
}
This code is maybe to simple and maybe there's a lots of better ways to do this but this is one of my first codes in C#.
I have solved my problem. This is the code that helped me:
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
Font titleFont = new Font(bf,20);
Font infoFont = new Font(bf,16);
Thank you all
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringReader html = new StringReader(sb.ToString());
byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
MemoryStream stream = new MemoryStream(byteArray);
Response.Clear();
using (iTextSharp.text.Document document = new iTextSharp.text.Document())
{
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(
writer, document, stream, new System.Text.UTF8Encoding()
);
}
Response.End();
So, what could be the reason that pdf doesn't display unicode characted since I have
byte[] byteArray = Encoding.UTF8.GetBytes(sb.ToString());
and
.ParseXHtml(writer, document, stream, new System.Text.UTF8Encoding());
Here is the few steps to display unicode characters in converting Html to Pdf
Create a HTMLWorker
Register a unicode font and assign it
Create a style sheet and set the encoding to Identity-H
Assign the style sheet to the html parser
Check below code
TextReader reader = new StringReader(html);
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(FileName, FileMode.Create));
HTMLWorker worker = new HTMLWorker(document);
document.Open();
FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
ST.LoadTagStyle("body", "encoding", "Identity-H");
worker.Style = ST;
worker.StartDocument();
Check below link for more understanding....
Display Unicode characters in converting Html to Pdf
Hindi, Turkish, and special characters are also display during converting from HTML to PDF using this method. Check below demo image.
StringReader reader = new StringReader(PDFText);
//Create PDF document
Document doc = new Document(PageSize.A4);
HTMLWorker parser = new HTMLWorker(doc);
string PDF_FileName = Server.MapPath("~") + "/App_Data/PDF_File.pdf";
PdfWriter.GetInstance(doc, new FileStream(PDF_FileName, FileMode.Create));
doc.Open();
doc.Add(new Paragraph("This is a Red Font Test using Times Roman", times));
try
{
parser.Parse(reader);
}
This is converted in pdf but it gives some other fonts like big letters and properly not aligned.
I created a simple pdf with iText.
But why is the position of the text on the first page higher than on all the other pages.
Here is some test code to see where the problem situates:
MemoryStream PDFData = new MeMemoryStream PDFData = new MemoryStream();
Document document = new Document(PageSize.A4, 50, 50, 80, 50);
PdfWriter PDFWriter = PdfWriter.GetInstance(document, PDFData);
document.Open();
Moviecollection movCol = new Moviecollection();
foreach (Movie mov in movCol.Movies)
{
Phrase phr = new Phrase(mov.Description);
document.Add(phr);
document.Add(Chunk.NEWLINE);
}
document.Close();
Any ideas?
thanks,
Filip
I think its to do with Chunk.NEWLINE addition.
I'm guessing you are simulating a paragraph with that Phrase + Newline combination. If you switch to Paragraph object instead, the problem is solved (tested on my machine with your code).
using(MemoryStream PDFData = new MemoryStream())
using(Document document = new Document(PageSize.A4, 50, 50, 80, 50))
{
PdfWriter PDFWriter = PdfWriter.GetInstance(document, PDFData);
document.Open();
Moviecollection movCol = new Moviecollection();
foreach (Movie mov in movCol.Movies)
document.Add(new Paragraph(mov.Description));
}