Add Windows context menu as "Merge PDF" when selecting multiple PDF files. After the addition, display only if multiple PDF files are selected. When the user clicks on the "Merge PDF", starts as a single instance of the application and loads all selected pdf on the listview.
Add Windows context menu as "Open PDF" when selecting single PDF files. After the addition, display only if single PDF files are selected. When the user clicks on the "Merge PDF", starts as a single instance of the application and displays a file name in the message box.
Actually, the I wrote original code in vb. As we know C# is used more widely than vb for that reason, I just made a conversion into the C# for easy understanding for C# developers.
I trying and add it to the Windows context menu.
private void cmdRegisterContextMenu_Click(object sender, EventArgs e)
{
// Registry path for PDF files
string pdfKeyPath = @"SystemFileAssociations\.pdf\Shell";
string openKeyPath = pdfKeyPath + @"\Open PDF";
string mergeKeyPath = pdfKeyPath + @"\Merge PDF";
// Path to your application executable
string appPath = "\"" + Application.ExecutablePath + "\"";
// Register "Open PDF" for single file selection
using (RegistryKey openKey = Registry.ClassesRoot.CreateSubKey(openKeyPath))
{
openKey.SetValue("", "Open PDF");
openKey.SetValue("Icon", appPath);
}
using (RegistryKey openCommandKey = Registry.ClassesRoot.CreateSubKey(openKeyPath + @"\Command"))
{
openCommandKey.SetValue("", appPath + " \"%1\"");
}
// Register "Merge PDF" for multiple file selection
using (RegistryKey mergeKey = Registry.ClassesRoot.CreateSubKey(mergeKeyPath))
{
mergeKey.SetValue("", "Merge PDF");
mergeKey.SetValue("App", appPath);
mergeKey.SetValue("Icon", @"E:\My Software\WindowsApp29\Project\Extra\graph_chart.ico");
}
using (RegistryKey mergeCommandKey = Registry.ClassesRoot.CreateSubKey(mergeKeyPath + @"\Command"))
{
mergeCommandKey.SetValue("", appPath + " \"%*\"");
}
MessageBox.Show("Context menu entries registered successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void ContextMenu_Load(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
var pdfFiles = new List<string>();
for (int i = 1, loopTo = args.Length - 1; i <= loopTo; i++)
pdfFiles.Add(args[i]);
LoadPDFsIntoListView(pdfFiles);
}
}
public void LoadPDFsIntoListView(List<string> pdfFiles)
{
foreach (string pdfFile in pdfFiles)
{
var item = new ListViewItem(pdfFile);
ListView1.Items.Add(item);
}
}
But, the few required and still serching the solution.
"Merge PDF" multiple files not working
mergeCommandKey.SetValue("", appPath + " \"%*\"");
"Merge PDF" only dispaly when multiple pdf files are selected.
"Merge PDF" start with single instance.
"Open PDF" only dispaly when single pdf file is selected.
"Open PDF" start with single instance.