Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. public static void WriteDocument()
  2.         {
  3.             //Declare a itextSharp document
  4.             Document document = new Document(PageSize.A4);
  5.  
  6.             //Create our file stream and bind the writer to the document and the stream
  7.             PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Users\User\Downloads\test.pdf", FileMode.Create));
  8.  
  9.             //Open the document for writing
  10.             document.Open();
  11.  
  12.             //Add a new page
  13.             document.NewPage();
  14.  
  15.             //Reference a Unicode font to be sure that the symbols are present.
  16.             BaseFont bfArialUniCode = BaseFont.CreateFont(@"C:\Users\User\Downloads\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  17.             //Create a font from the base font
  18.             Font font = new Font(bfArialUniCode, 12);
  19.  
  20.             //Use a table so that we can set the text direction
  21.             PdfPTable table = new PdfPTable(1);
  22.             //Ensure that wrapping is on, otherwise Right to Left text will not display
  23.             table.DefaultCell.NoWrap = false;
  24.  
  25.             //Create a regex expression to detect hebrew or arabic code points
  26.             const string regex_match_arabic_hebrew = @"[\u0600-\u06FF,\u0590-\u05FF]+";
  27.             if (Regex.IsMatch("מה קורה", regex_match_arabic_hebrew, RegexOptions.IgnoreCase))
  28.             {
  29.                 table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
  30.             }
  31.  
  32.             //Create a cell and add text to it
  33.             PdfPCell text = new PdfPCell(new Phrase("مرحبا يا رجل كيف حالك", font));
  34.             //Ensure that wrapping is on, otherwise Right to Left text will not display
  35.             text.NoWrap = false;
  36.  
  37.             //Add the cell to the table
  38.             table.AddCell(text);
  39.  
  40.             //Add the table to the document
  41.             document.Add(table);
  42.  
  43.             //Close the document
  44.             document.Close();
  45.  
  46.             //Launch the document if you have a file association set for PDF's
  47.             Process AcrobatReader = new Process();
  48.             AcrobatReader.StartInfo.FileName = @"C:\Users\User\Downloads\test.pdf";
  49.             AcrobatReader.Start();
  50.  
  51.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement