1

I am trying to paste text, image and pdf documents to clipboard first and then to whatsapp textbox control.

enter image description here

1) For Text.

Clipboard.SetText(message); 

Ctrl + v pastes the text into the whatsapp textbox.

2) For Image

var imagePath = "E:\Downloads_SSD\goodmorning.jpg";

Clipboard.SetImage(Image.FromFile(@imagePath ));

Ctrl + v pastes the Image into whatsapp.

3) For Documents.

StringCollection paths = new StringCollection();

paths.Add(@"E:\Downloads_SSD\Hatred-in-the-belly_-Politics-behind-the-appropriation-of-Dr-Ambedkars-writings.pdf");

Clipboard.SetFileDropList(paths);

Ctrl + v works when copying into a folder, but does not work for copying into whatsapp message textbox.

How do I make it to work in case of pdf documents.

sham
  • 691
  • 8
  • 28

1 Answers1

0

To directly answer your question - copying & pasting .pdf files from the clipboard will not work - that is not the way WhatsApp web does it. Surprisingly there is a way to achieve something similar which does not involve pasting.

clip button

Basically you have to trigger a click event on that clip-button and run some javascript.

You did not mention what programing language you are using so I will use what works for me. In this example I am using C# PuppeteerSharp.

 // load your pdf bytes here
 byte[] fileBytes = YourFunctionToGetFileBytes("https://link-to-pdf-file");

                        if (fileBytes != null)
                        {
                            // first, we get the clip-button
                            var clipImage = _whatsAppPage.QuerySelectorAsync("div[data-testid=\"conversation-clip\"]").Result;

                            // focus and click on that button
                            await clipImage.FocusAsync();
                            Thread.Sleep(600);
                            await clipImage.ClickAsync();
                            Thread.Sleep(600);

                            // wait for the documents button to become available
                            await _whatsAppPage.WaitForSelectorAsync("li[data-testid=\"mi-attach-document\"]");

                            // this is where the real magic happens
                            // see the next block of code for full details
                            await SetPdfBlobFile(fileBytes, "test-file-name.pdf", "document.querySelector(\"li[data-testid='mi-attach-document']\").querySelector(\"input[type='file']\")");
                            Thread.Sleep(600);

                            // after the pdf is finally set, we need to wait for the send button to become available
                            await _whatsAppPage.WaitForSelectorAsync("div[role=\"button\"][aria-label=\"Send\"]");
                            var sendButtonPic = _whatsAppPage.QuerySelectorAsync("div[role=\"button\"][aria-label=\"Send\"]").Result;
                            Thread.Sleep(600);

                            await sendButtonPic.ClickAsync();

                            Thread.Sleep(1600);
                        }

If you inspect the "documents" button, you will see it contains a hidden input button which is in reality the one responsible for loading your files. My "SetPdfBlobFile" function is simply targeting that input button.

hidden input file button

Please note that the next code block is basically setting our .pdf file into that hidden "input" file element. You must understand that it is not possible to simply set the "value" field of the input button to the path of your .pdf file. But it is possible to set a .pdf file from a blob. Here's the code for accomplishing that:

    private async Task<Newtonsoft.Json.Linq.JToken> SetPdfBlobFile(byte[] pdfBytes, string fileName, string selector)
    {
        var pasteResults = await _whatsAppPage.EvaluateFunctionAsync(@"(pdfBytes, fileName) => {
                return new Promise(async (resolve, reject) => {

                    // logic to create our blob
                    const base64Response = await fetch(pdfBytes);
                    const pdfBlob = await base64Response.blob();

                    try {
                        const fileInput = " + selector + @"; 
                        const dataTransfer = new DataTransfer();

                        // create a new file using our pdf blob
                        const file = new File([pdfBlob], fileName, { type: 'application/pdf' });

                        // add our file to the hidden input button
                        dataTransfer.items.add(file);
                        fileInput.files = dataTransfer.files;

                        // our file has been added to the hidden input button
                        // but lastly we need to trigger a 'change' event
                        // so WhatsApp loads the send button
                        fileInput.dispatchEvent(new window.Event('change', { bubbles: true }));

                        resolve(true);
                    } catch (ex) {
                        resolve(ex.message);
                    }
                });
            }"
            , "data:application/pdf;base64," + Convert.ToBase64String(pdfBytes), fileName);

        return pasteResults;
    }

That's it! Have fun sending .pdf files!

fictus
  • 286
  • 2
  • 5