Reputation: 89
I am working with C#.net 2.0, Office 2010 and Visual Studio 2005.
I have an excel file with embedded .pdf
and .doc
files.
I am able to read and save the .doc
file with this code:
if (inlineShape.OLEFormat.progID.StartsWith("word.document."))
{
inlineShape.OLEFormat.Activate();
Word.Document document = inlineShape.OLEFormat.Object as Word.Document;
FileInfo wfi = new FileInfo(fileName);
object wfileName = (object)(explodedDirectory + wfi.Name + "." + docCount.ToString() + ".doc");
object fileFormat = Word.WdSaveFormat.wdFormatDocument;
document.SaveAs(ref wfileName, ref fileFormat, ref _missing, ref _missing,
ref _missing, ref _missing, ref _missing, ref _missing, ref _missing,
ref _missing, ref _missing, ref _missing, ref _missing, ref _missing,
ref _missing, ref _missing);
document.Close(ref saveChanges, ref originalFormat, ref routeChanges);
document = null;
}
But I can't do anything with .pdf
.
I need to extract the .pdf
file and save it in to a folder, thanks in advance.
Upvotes: 3
Views: 12116
Reputation: 2887
The code you are using for the Word document assumes knowledge of the contents of the embedded OLE object, and uses Word itself to save the document. Since there is no native PDF editor in MS Office, there is no easy way to do the same for PDFs, as @Shadow Wizard's link points out.
There are still ways to get at the data; if, for example, you are using the newer .xslx format (as opposed to the .xls format), things are much easier (or at least, more apparent). The .xslx file is actually an Office Open XML file, and you can use something like the Open XML SDK to manipulate it.
Another approach would be to try to extract the files from the .xslx file - this can be done quite easily, as it's essentially just a .zip file. To play around with this manually, rename your .xlsx file to .zip and extract it. Inside, you will find a folder like "xl", and inside there, "embeddings". Your PDF document will be here, unfortunately encoded as an OLE Object. You can then try some get the data out of it, one example is here.
Unfortunately I don't know enough about the specifics here, otherwise I would have given a more direct answer. Hope this helps.
Upvotes: 3