xyzio

Posts Tagged ‘c#

Return pdf to browser in a C# razor page using itext7

leave a comment »

Sample code to return a PDF file to the browser in a Razor c# page using itext7.

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.IO;

public IActionResult OnPostManipulatePdf(String dest)
{
    MemoryStream ms = new MemoryStream();

    PdfWriter writer = new PdfWriter(ms);
    PdfDocument pdfDoc = new PdfDocument(writer);
    Document doc = new Document(pdfDoc);
    writer.SetCloseStream(false);

    Paragraph header = new Paragraph("header");

    doc.Add(header);
    doc.Close();

    byte[] byteInfo = ms.ToArray();
    ms.Write(byteInfo, 0, byteInfo.Length);
    ms.Position = 0;

    FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf");

    //Uncomment this to return the file as a download
    //fileStreamResult.FileDownloadName = "Output.pdf";

    return fileStreamResult;
}

Source:
https://stackoverflow.com/questions/1510451/how-to-return-pdf-to-browser-in-mvc

Written by M Kapoor

September 23, 2020 at 2:49 pm

Posted in c#, Programming

Tagged with , , ,

Split an Excel file’s tabs into CSV with C#

leave a comment »

Split an Excel file into csv, with each tab going into a separate csv file, in the background using C#.

using System.IO;
using Microsoft.Office.Interop.Excel;

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

//Run in background
app.DisplayAlerts = false;

//Open excel file
Workbook wb = app.Workbooks.Open("path_to_excel.xls");

//Iterate through sheets in the excel file
foreach (Worksheet sheet in wb.Worksheets)
{
    string sheetName = sheet.Name;
    
    //Output file name
    string outputFilepath = sheetName + ".csv";
    
    //Save the sheet as CSV
    sheet.SaveAs(outputFilepath, XlFileFormat.xlCSVWindows);
}

//Clean-up
wb.Close();
app.Quit();


Written by M Kapoor

December 24, 2019 at 3:19 am

Posted in c#, Programming

Tagged with ,

Change Font Type in Clipboard with C#

leave a comment »

Convert text in the Clipboard to another font and re-save it to the clipboard in C#.

RichTextBox rtb = new RichTextBox();

//The new font is GenericMonospace
Font font = new Font(FontFamily.GenericMonospace, 10);

//Get text from the clipboard
rtb.Text = Clipboard.GetText();

//Select all text in the RichTextBox and apply fontstyle
rtb.SelectAll();
rtb.SelectionFont = font;

//Copy updated text back to the clipboard
rtb.Copy();

Written by M Kapoor

December 23, 2019 at 9:39 pm

Posted in Programming

Tagged with ,

Free Bingo Card and Wordsearch Designer

leave a comment »

If you are looking for a free Bingo Card or Wordsearch Designer then look no more – BingoWords Puzzle Designer is now free for everybody!  Click here to go to the website!

Features:

  • Create arbitrary sized Wordsearch puzzles
  • Create Bingo Cards up to 5×5 squares in size
  • Save your wordlists for future use and to share with co-workers
  • Print an unlimited number of Bingo Cards and Word Search Puzzles
  • Print up to 8 Bingo Cards per page
  • Quick access to common word lists using the built in Word Wizard

For more details, read on.

Why is it Free?

Originally, I was planning to sell Bingo Words Puzzle Designer but then I realized that I’d rather spend my free time working on new code instead of on marketing and all the hassle that comes with running a business.  Besides, putting together something like this only took me a couple weeks of part-time work and it annoys me that some people are charging over $30/copy for something that is this simple to make.

My hope is that it will be useful to someone and that they will pay it forward some day.

About

BingoWords Puzzle Designer is a project I worked on in order to teach myself C#/ASP.NET.  The application is made with C# and Winforms.  The website is build using ASP.NET and is hosted using Mono on a  virtual server at Linode.  I was able to learn every aspect of creating an app with this project – from drawing on the screen to printing and to saving files to generating serial numbers.

Feel free to contact me with questions or suggestions for improvement!

Written by M Kapoor

February 19, 2010 at 4:06 am