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.