Beispiel #1
0
 private bool WriteToPDFDocument(string text, string path)
 {
     try
     {
         PDF.PdfDocument doc = new PDF.PdfDocument();
         doc.XmpMetaData.SetAuthor(Environment.UserName);
         doc.XmpMetaData.SetCreateDate(DateTime.Now);
         doc.XmpMetaData.SetCreator(Environment.UserName);
         doc.XmpMetaData.SetKeywords(string.Format("Календарь, {0}", this.dateTime.Year));
         doc.XmpMetaData.SetProducer(Environment.UserName);
         doc.XmpMetaData.SetSubject("Календари");
         doc.XmpMetaData.SetTitle(string.Format("Календарь на {0} г.", this.dateTime.Year));
         PDFGraphics.PdfMargins       margin        = new PDFGraphics.PdfMargins();
         PDFGraphics.PdfUnitConvertor unitConvertor = new PDFGraphics.PdfUnitConvertor();
         margin.Top    = unitConvertor.ConvertUnits(0.71f, PDFGraphics.PdfGraphicsUnit.Centimeter, PDFGraphics.PdfGraphicsUnit.Point);
         margin.Bottom = margin.Top;
         margin.Left   = unitConvertor.ConvertUnits(1.76f, PDFGraphics.PdfGraphicsUnit.Centimeter, PDFGraphics.PdfGraphicsUnit.Point);
         margin.Right  = margin.Left;
         PDF.PdfPageBase             page          = doc.Pages.Add(PDF.PdfPageSize.A4, margin);
         PDFGraphics.PdfTrueTypeFont timesNewRoman = new PDFGraphics.PdfTrueTypeFont(@"C:\Windows\Fonts\times.ttf", 16.0f);
         PDFGraphics.PdfTrueTypeFont courierNew    = new PDFGraphics.PdfTrueTypeFont(@"C:\Windows\Fonts\cour.ttf", 12.0f);
         PDFGraphics.PdfStringFormat alignCenter   = new PDFGraphics.PdfStringFormat(PDFGraphics.PdfTextAlignment.Center);
         page.Canvas.DrawString(string.Format("Календарь на {0} г.", this.dateTime.Year), timesNewRoman, new PDFGraphics.PdfSolidBrush(Color.Blue), page.Canvas.ClientSize.Width / 2, 0, alignCenter);
         float    y     = 43;
         string[] lines = text.Split(new char[] { '\n' }, StringSplitOptions.None);
         foreach (string item in lines)
         {
             bool isFound = false;
             if (item.Trim().StartsWith(@"Дн\Нед"))
             {
                 page.Canvas.DrawRectangle(new PDFGraphics.PdfSolidBrush(Color.Yellow), new RectangleF(0, y, courierNew.MeasureString(item, courierNew.Size).Width *item.Length, 13));
             }
             else
             {
                 for (int i = 0; i < daysOfWeek.Length; i++)
                 {
                     if (item.Trim().StartsWith(daysOfWeek[i]))
                     {
                         string temp = item.Substring(4, 3);
                         SizeF  size = courierNew.MeasureString(temp, courierNew.Size);
                         page.Canvas.DrawRectangle(new PDFGraphics.PdfSolidBrush(Color.Yellow), new RectangleF(size.Width * 4, y, size.Width * temp.Length, 13));
                         if (i >= 5)
                         {
                             page.Canvas.DrawString(item, courierNew, new PDFGraphics.PdfSolidBrush(Color.Red), 0, y);
                         }
                         else
                         {
                             page.Canvas.DrawString(item, courierNew, new PDFGraphics.PdfSolidBrush(Color.Black), 0, y);
                         }
                         isFound = true;
                         break;
                     }
                 }
             }
             if (!isFound)
             {
                 page.Canvas.DrawString(item, courierNew, new PDFGraphics.PdfSolidBrush(Color.Black), 0, y);
             }
             y += 13;
         }
         doc.SaveToFile(path, PDF.FileFormat.PDF);
         return(true);
     }
     catch (Exception exception)
     {
         MessageBox.Show(exception.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return(false);
     }
 }