0

I'm using this Answer to embed a PDF to a word document This code should be included in a STA thread according to the answer.

This is the function that I need to include in the STA thread.

private void EmbedPDFInWord(string first_source, string second_source)
    {

        string embeddedDocumentPath = @second_source;
        string packagePath = @"E:\RR\Files\Temp\test.bin";
        string packageIconPath = @"E:\RR\Files\Temp\test.emf";

        Packager.PackageFile(packagePath, packageIconPath, embeddedDocumentPath);

        using (var doc = WordprocessingDocument.Create(@first_source, WordprocessingDocumentType.Document))
        {
            var main = doc.AddMainDocumentPart();

            // add icon embedding
            var imagePart = main.AddImagePart(ImagePartType.Emf);
            imagePart.FeedData(File.OpenRead(packageIconPath));

            // add package embedding
            var objectPart = main.AddEmbeddedObjectPart("application/vnd.openxmlformats-officedocument.oleObject");
            objectPart.FeedData(File.OpenRead(packagePath));

            // build a sample doc
            main.Document = BuildDocument(main.GetIdOfPart(imagePart), main.GetIdOfPart(objectPart), "Package");
        }

    }

This is how I've done it so far

  Thread thread = new Thread(() => EmbedPDFInWord(wordPdfPath, excelPdfPath));
                thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA

                thread.Start();

                thread.Join();

But I get an error from the "CheckHr" function when calling "OleCreateFromFile" in Packager class.
Can anyone help me on including my method correctly in an STA thread ? Thanks.

  • you must change to STA before doing anything else. – Simon Mourier Jul 30 '19 at 13:07
  • I changed to STA but I'm still having the problem – Sachintha Nayanajith Jul 31 '19 at 04:59
  • I edited the code – Sachintha Nayanajith Jul 31 '19 at 05:02
  • I've tested my sample, it still works fine for me, also creating a Thread (you don't need Sleep and TrySetApartementState, just [SetApartmentState + Start + Join]). However, the registry key can vary. Have you tried `HKEY_CLASSES_ROOT\Acrobat.Document.DC\PackageOnFileDrop` instead of `HKEY_CLASSES_ROOT\AcroExch.Document.DC\PackageOnFileDrop` – Simon Mourier Jul 31 '19 at 07:31
  • @SimonMourier - Thank you so much for your time. I removed Sleep and TrySetApartementState. I edited the code. I might be adding the registry key in a wrong way. In the registry editor I went to the location "HKEY_CLASSES_ROOT\AcroExch.Document.DC" and created a new key named "PackageOnFileDrop" and set the data of the key as "HKEY_CLASSES_ROOT\Acrobat.Document.DC\PackageOnFileDrop" Could you tell me if this is correct ? – Sachintha Nayanajith Jul 31 '19 at 08:00
  • There's no "data" to set under the key, just create a HKEY_CLASSES_ROOT\AcroExch.Document.DC\PackageOnFileDrop key and/or another HKEY_CLASSES_ROOT\Acrobat.Document.DC\PackageOnFileDrop key – Simon Mourier Jul 31 '19 at 08:35
  • @SimonMourier - Thank you so much for your support . – Sachintha Nayanajith Aug 01 '19 at 03:18

0 Answers0