Add Arabic to the pdf file iTextSharp - xamarin.android

Add Arabic language to pdf file using iTextSharplgpl .
Word print code in PDF file
English language appears only and Arabic does not appear Why
code
document.Add(new Paragraph("alie جيد جدا"));

You could try to below method to create the font from Assets folder.
AssetManager assets = this.Assets;
string content;
Stream stream = assets.Open("DroidNaskh-Bold.ttf");
var memorystrm = new MemoryStream();
stream.CopyTo(memorystrm);
byte[] t = memorystrm.ToArray();
if (t != null)
{
PdfFont f = PdfFontFactory.CreateFont(t, "UTF-8", true);
......
}

Related

path to resources directory in xamarin.android

I need the path to my resources directory to access my fonts folder inside it like the one in this code:
PdfFont russian = PdfFontFactory.createFont(
"src/main/resources/fonts/FreeSans.ttf", "CP1251", true);
but in Xamarin.android. I tried the following:
string uri = "android.resource://" + this.PackageName + "/font/ARIAL.TTF";
PdfFont russian = PdfFontFactory.CreateFont(
uri, "CP1251", true);
but it doesn't work. I tried this code too:
var path2 = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
filePath = System.IO.Path.Combine(path2.ToString(), "myfile4.pdf");
stream = new FileStream(filePath, FileMode.Create);
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdf2 = new iText.Kernel.Pdf.PdfDocument(writer);
Document document2 = new Document(pdf2, PageSize.A4);
AssetManager assets = this.Assets;
string content;
Stream stream2 = assets.Open("ARIAL.TTF");
var memorystrm = new MemoryStream();
stream2.CopyTo(memorystrm);
byte[] t = memorystrm.ToArray();
Toast.MakeText(this, t.Length.ToString(), ToastLength.Long);
if (t != null)
{
PdfFont russian = PdfFontFactory.CreateFont(t, "UTF-8", true);
document2.SetFont(russian);
Paragraph p = new Paragraph("Hello World! ")
.Add(new Text("صباح! ").SetFontSize(14)).Add(new Text("Bonjour le monde! ").SetFontSize(10));
document2.Add(p);
document2.Close();
Toast.MakeText(this, "done", ToastLength.Long);
}
else
{
Toast.MakeText(this, "error", ToastLength.Long);
}
no code was exceuted
The path of the folder of the Xamarin.Android project is different with the native Android project.
If you want to save the font file in the project to access the file, try to save the files in the Asset folder.Set the Build Action for this files to AndroidAsset.
string content;
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader(assets.Open("read_asset.txt")))
{
content = sr.ReadToEnd();
}
Check the tutorial:
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/resources-in-android/android-assets?tabs=windows
Update
i'll add my code, it didn't work. no code was executed
It seems that you forgot to add the .Show() code such as Toast.MakeText(this, "done", ToastLength.Long).Show().

How to get Image Name or Id from docx file using Open xml in C#?

I am using Mvc.Net API with open XML. I need to replace multiple images in .docx file.I replace the images in current scenario but I don't get any Id or Name of the Image at my code side so facing difficulties to replace those images.
Here is my code
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
string Id=doc.MainDocumentPart.GetIdOfPart(imgPart);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
Can I get the name of Image in ImagePart?
I would do something like this:
List<ImagePart> imgPartList = doc.MainDocumentPart.ImageParts.ToList();
foreach(ImagePart imgPart in imgPartList)
{
var imageId = document.MainDocumentPart.GetIdOfPart(imgPart.Id);
byte[] imageBytes = File.ReadAllBytes(ImagePath);
BinaryWriter writer = new BinaryWriter(imgPart.GetStream());
writer.Write(imageBytes);
writer.Close();
}
Also
This answer could help you

Exported excel showing non-recognised characters in iphone

i have this code that converts data
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt);
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
//string strpath = Server.MapPath("~/SavedFolder/hello.xlsx");
//wb.SaveAs(strpath);
HttpContext.Response.Clear();
HttpContext.Response.Buffer = true;
HttpContext.Response.Charset = "";
HttpContext.Response.ContentType = "application/vnd.ms-excel";
//HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Response.AddHeader("Content-Disposition", "attachment;filename= ResultReport.xls");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(HttpContext.Response.OutputStream);
HttpContext.Response.Flush();
HttpContext.Response.End();
}
Works fine on pc but on iphone or ipad it shows error unable to read.
So What i did ,convert the content type to CSV format,
then the excel was opening but data shown is not English its gibberish .
I tried opening excel using free apps for iphone to open documents same result
can someone help.

How to convert MS-WORD file into HTML in ASP.NET MVC?

I am doing a project in which i have to to read a word document and perform some operation on file content. I have read the file successfully using
Open XML, but that's not enough. It read the file but does not retain the format and indentation. So the solution i found is that first read the file and convert it into HTML. Another thing i want all file content in "String" because i have to perform further operation on file contents.
So is there any way to read the MS-WORD file and convert into HTML and store in "string"?
Here i am showing the code of reading MS-WORD file.
[HttpPost]
public ActionResult OpenFile(HttpPostedFileBase file)
{
Rules R = new Rules();
string FileText = "";
if (file != null && file.ContentLength > 0)
{
string filename = Path.GetFileName(file.FileName);
R.name = filename;
string path = Path.Combine(Server.MapPath("~/Uploads"), filename);
file.SaveAs(path);
string filepath = path;
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filepath, true))
{
Body body = wordDoc.MainDocumentPart.Document.Body;
foreach (var paragraph in body.Elements<Paragraph>())
{
FileText += " <br> ";
}
}
R.Content += FileText;
}
return View(R);
}

Could not upload a local image file embedded into HTML as a data URI into google doc via java api

I need upload a image file from local disk. I converted the image into base64 format, and write the html file into local disk as well, that html file can be opened in browser with image showing, but the document in google doc just an empty file, even I drag that html file into google docs the image still not there. My code is below:
DocsService client = new DocsService("testappv3");
client.setUserCredentials("username", "password");
File file = new File("c:/test.bmp");
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
out.write(buff, 0, read);
}
out.flush();
String base64 = Base64.encode(out.toByteArray());
String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
String html = "<html><body><img src=\"data:" + mimeType + ";base64," + base64 + "\"/></body></html>";
URL destFolderUrl = new URL("https://docs.google.com/feeds/default/private/full/<FOLDER_ID>/contents");
DocumentEntry newDocument = new DocumentEntry();
newDocument.setTitle(new PlainTextConstruct("test"));
newDocument.setMediaSource(new MediaByteArraySource(html.getBytes(), "text/html"));
newDocument = client.insert(destFolderUrl, newDocument);
This looks like a bug, we are trying to fix it, and I will get back to you when we have a fix.
Thanks for your patience.

Resources