Thursday 29 June 2023

Enable the seconds on the Windows 11 clock

Step 1:- Right click on the taskbar => Choose Taskbar settings











Step 2:- Click the down arrow next to Taskbar behaviors


Step 3:- Scroll down bottom => Click the checkbox for "Show seconds in the system tray clock (uses more power)"

Step 4:- Done








Wednesday 21 June 2023

Develop And Deploy Azure WebJobs In Azure App Service

 Step 1: - Create Console application => Click “Next” => Project Name “TestWebJob” => Click “Next“ => Click “Create


























The console application created successfully

Step 2: - Go to application containing folder => expand “bin” folder => expand “Release” folder => select “net6.0” folder and compress to zip








Step 3: - Login to Microsoft Azure Portal

Step 4: - Search “App Service” in search bar => Select Web APP (WebJobUAT) in the list












Step 5:- Select “WebJobs” from the left pane => Click “Add












Step 6:- Fill Details => Click “Create Webjob













Step 7:- Click “Refresh












Step 8:- Click “Run” => Click “Run













Step 9:- Click “Logs












Step 10:- Click “TestWebJob” name












Step 11:- Click “Timing












Step 12:- Job Run Successfully












Step 13:- Delete the “TestWebJob



Merge Multiple PDFs into a single PDF using iTextSharp in C#

Do not forget to install iTextSharp.dll using Manage NuGet Packages

using
iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
 
namespace ConsoleApplication1
{
    public class MergePDF
    {
        public void ProcessMergePDF()
        {
            string[] files = Directory.GetFiles(@"E:\Form\");
            string outputPath = @"E:\Output\Merge.pdf";
            using (Document doc = new Document())
            {
                using (PdfCopy writer = new PdfCopy(doc, new FileStream(outputPath, FileMode.Create)))
                {
                    if (writer == null)
                        return;
                    doc.Open();
                    foreach (string file in files)
                    {
                        using (PdfReader reader = new PdfReader(file))
                        {
                            reader.ConsolidateNamedDestinations();
                            for (int i = 1; i <= reader.NumberOfPages; i++)
                            {
                                PdfImportedPage page = writer.GetImportedPage(reader, i);
                                writer.AddPage(page);
                            }
                        }
                    }
                }
            }
        }
    }

} 

Sunday 18 June 2023

Remove items from one list in another in C#

List<string> list1 = new List<string>() { "A", "B", "C", "D" };
List<string> list2 = new List<string>() { "B", "D" }; 
List<string> list3 = list1.Except(list2).ToList();

Here, in the list3 object, we will get the value A & C.

Convert SSIS recordset into datatable

Unable to cast COM object of type 'System.__ComObject' to class type 'System.Data.DataTable'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however, they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

 

DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(); 
da.Fill(dt, Dts.Variables["User::objList"].Value);

Convert Byte array to Base64 string in c#

 string byteString = Convert.ToBase64String(bytes);

How to Generate an SSRS Report as a Password-Protected PDF Using ASP.Net C#

Case scenario, apply password on pdf generated with SSRS report.

DLL Required.


Microsoft.ReportViewer.WebForms.dll
Microsoft.ReportViewer.Common.dll
itextsharp.dll


using iTextSharp.text.pdf;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
 
namespace ConsoleApplication1
{
    public class SSRSReport
    {
        public void Report()
        {
            string path = "Report Path";
            string ssrsURL = "Report Server URL";
            string ssrsUser = "Report Server User";
            string ssrsPass = "Report Server Password";
 
            ReportViewer reportViewer = new ReportViewer();
            reportViewer.ProcessingMode = ProcessingMode.Remote;
            reportViewer.ServerReport.ReportServerCredentials
                = new ReportCredentials(new NetworkCredential(ssrsUser.Trim(), ssrsPass.Trim(), ""));
            reportViewer.ServerReport.ReportServerUrl = new Uri(ssrsURL);
            reportViewer.ServerReport.ReportPath = path;
            reportViewer.ServerReport.SetParameters(new List<ReportParameter>() {
                new ReportParameter("Param1", "Value1"),
                new ReportParameter("Param2", "Value2")
            });
            reportViewer.ServerReport.Refresh();
 
            byte[] bytes = reportViewer.ServerReport.Render("pdf");

            using (MemoryStream inputData = new MemoryStream(bytes))
            {
                using (MemoryStream outputData = new MemoryStream())
                {
                    string pdfPassword = "test@123";
                    PdfReader reader = new PdfReader(inputData);
                    PdfEncryptor.Encrypt(reader, outputData, true, pdfPassword, pdfPassword, PdfWriter.ALLOW_SCREENREADERS);
                    byte[] passwordFileBytes = outputData.ToArray();
 
                    if (!Directory.Exists(@"D:\Report\"))
                        Directory.CreateDirectory(@"D:\Report\");
 
                    File.WriteAllBytes(@"D:\Report\WithoutPassword.pdf", bytes);
                    File.WriteAllBytes(@"D:\Report\WithPassword.pdf", passwordFileBytes);
 
                    //Line of code used in the web application
                    //Response.ContentType = "application/pdf";
                    //Response.AddHeader("content-disposition", "attachment;filename=Sample.pdf");
                    //Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    //Response.BinaryWrite(passwordFileBytes);
                    //Response.End();
                }
            }
        }
    }
}