Monday, September 30, 2013

iTextSharp–Print PDF

To print html page, we use
but for PDF same can not be used as it won’t work. However iTextSharp provides a way to print PDF on client side with AddJavaScript method of PdfWriter.

Check out below code:

private void CreatePDF()
        string fileName = string.Empty;

        DateTime fileCreationDatetime = DateTime.Now;

        fileName = string.Format("{0}.pdf", fileCreationDatetime.ToString(@"yyyyMMdd") + "_" + fileCreationDatetime.ToString(@"HHmmss"));

        string pdfPath = @"E:\PDFs\" + fileName;

        using (FileStream msReport = new FileStream(pdfPath, FileMode.Create))
            //step 1
            using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 10f))
                    // step 2
                    PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport);

                    //open the stream 

                    Phrase phrase1 = new Phrase("Hello world. Checking Print Functionality");




                catch (Exception ex)
                    //handle exception




It won’t print the PDF directly but will open print dialog as soon as you open the PDF. One can choose one of the listed printer and print it. There won’t be any print preview. On web page to simulate the print functionality, one can assign pdf path to Iframe which will be hidden on page.

<iframe id="pdfViewer"  runat="server" style="display:none;" />

As you can see there’s no path assigned to src attribute in HTML but in code behind as shown below:
pdfViewer.Attributes["src"]=  ResolveClientUrl(string.Format("~/PDF/{0}",fileName));
It won’t display the PDF in browser but will ask user to print the PDF (without preview). Both Firefox and Chrome use in built pdf viewer.

Known Issues:
  • It will not display PDF in IE and Opera but download it if no pdf viewer is installed on machine and respective add-on is not installed on both the browsers. For opera 15 onwards, you can install this add-on which is based on Mozilla’s pdf.js.

  • If pdf viewer has a setting to view pdf in secure mode and it is enabled, it will not run javascript and gives warning.
I hope it helps!

Sunday, September 22, 2013

Copy as path in Windows 7/8

Many times we want to copy complete file path and we do that in two steps:
  • Copy path of the folder/drive in which file resides
  • Copy Filename
and use combination of it to solve the purpose.

One simple solution exists from win 7 onwards is to select file and press Shift key + Right Mouse click. Context menu will have an option “Copy as path”, select it and you’re done.