Archive for the ‘Ostatné/Other VC#’ Category

TextBox – enable only number and backspace/delete

streda, február 15th, 2012
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    int num;
    if ((!Int32.TryParse(e.KeyChar.ToString(), out num)) && !(e.KeyChar == (char)8)) e.Handled = true;
}

Add all subdirectory name to ComboBox

streda, február 15th, 2012
private void comboBox1_DropDown(object sender, EventArgs e)
{
 comboBox1.Items.Clear();
 string baseDir = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
 string[] dirPaths = Directory.GetDirectories(baseDir);
 foreach(string dir in dirPaths)
  {
   comboBox1.Items.Add(new DirectoryInfo(dir).Name);
  }
}

OpenXML .XLSX get cell value

piatok, november 11th, 2011
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
...
        public string GetCellValue(string filename, int row, int column, int sheet_number)
        {
                SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, false);
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                Worksheet sheet = workbookPart.WorksheetParts.ElementAtOrDefault(sheet_number).Worksheet;
                SharedStringTablePart stringTablePart = workbookPart.SharedStringTablePart;
                Cell cell = sheet.Descendants<Row>().ElementAt(row).Descendants<Cell>().ElementAt(column);
            
                string value = cell.CellValue != null ? cell.CellValue.InnerXml : String.Empty;

                if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
                 return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
                else
                 return value;
        }

OpenXML .XLSX get sheet count

piatok, november 11th, 2011
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
...
public string GetSheetCount(string filename)
{
    return SpreadsheetDocument.Open(filename, false).WorkbookPart.WorksheetParts.Count().ToString();
}

PDFSharp – Add to exist PDF

utorok, október 25th, 2011
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;
using System.Diagnostics;
using System.IO;
...
            string file = "test.pdf";
            PdfDocument outputDocument = new PdfDocument();
            PdfDocument doc = PdfSharp.Pdf.IO.PdfReader.Open(file, PdfDocumentOpenMode.Import);
            PdfPage page = doc.Pages[0];
            
            outputDocument.AddPage(page);
            PdfPage page2 = outputDocument.Pages[0];
            XGraphics gfx = XGraphics.FromPdfPage(page2);
            XImage image = XImage.FromFile("image.jpg");
            gfx.DrawImage(image, 5, 100);
            
            XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
            XFont font = new XFont("ArialCE", 13, XFontStyle.Regular, options);
            gfx.DrawString("Test text: ľščťžňýáíéúäô - " + DateTime.Now, font, XBrushes.Black, 5, 100);

            if(File.Exists("out.pdf")){File.Delete("out.pdf");}
            outputDocument.Save("out.pdf");

            Process.Start("out.pdf");