c++ - Invoking OLE in C# through OleCreateFromFile does not work for pdf files -


i trying embed pdf files open xml document. requires creating *.bin files. dont want use automation.

approach ive taken this question works file types ive tested except *.pdf.

for reason pdf files result olecreatefromfile(..) 0x80004005 , pole null.

i new on field of invoking , ole. reason approach not working pdf? (i have newest adobe reader, win8, invoking ole32.dll, projects build target x86 , ive test call couninitialize() , coinitializeex((system.intptr)null, ole32.coinit.apartmentthreaded), able embed pdf files in msword application).

here function use it:

public static string exportolefile(string _inputfilename, string oleoutputfilename, string emfoutputfilename)         {             stringbuilder resultstring = new stringbuilder();                        string newinput = multibytetounicodenetonly(_inputfilename, 1252);              microsoft.visualstudio.ole.interop.istorage storage;             var result = ole32.stgcreatestorageex(oleoutputfilename,                 convert.toint32(ole32.stgm.stgm_readwrite | ole32.stgm.stgm_share_exclusive | ole32.stgm.stgm_create | ole32.stgm.stgm_transacted),                 convert.toint32(ole32.stgfmt.stgfmt_docfile),                 0,                 intptr.zero,                 intptr.zero,                 ref ole32.iid_istorage,                 out storage             );//vytvoří bin              resultstring.appendline("createstorageex result: " + result.tostring());              var clsid_null = guid.empty;               microsoft.visualstudio.ole.interop.formatetc f = new formatetc();             microsoft.visualstudio.ole.interop.ioleobject pole;             result = ole32.olecreatefromfile(                 ref clsid_null,                 newinput,                 ref ole32.iid_ioleobject,                 (uint)microsoft.visualstudio.ole.interop.olerender.olerender_none,                 ref f,                 null,                 storage,                 out pole             );              resultstring.appendline("olecreatefromfile result: " + result.tostring());             try             {                  result = ole32.olerun(pole);             }             catch (exception ex)             {                 resultstring.appendline(ex.tostring());                 return resultstring.tostring();             }              resultstring.appendline("olerun result: " + result.tostring());              try             {                  intptr unknownfromole = marshal.getiunknownforobject(pole);                 intptr unknownfordataobj;                 marshal.queryinterface(unknownfromole, ref ole32.iid_idataobject, out unknownfordataobj);                 var pdo = marshal.getobjectforiunknown(unknownfordataobj) system.runtime.interopservices.comtypes.idataobject;                  var fetc = new system.runtime.interopservices.comtypes.formatetc();                  fetc.cfformat = (short)ole32.clipformat.cf_enhmetafile;                 fetc.dwaspect = system.runtime.interopservices.comtypes.dvaspect.dvaspect_content;                 fetc.lindex = -1;                 fetc.ptd = intptr.zero;                 fetc.tymed = system.runtime.interopservices.comtypes.tymed.tymed_enhmf;                  var stgm = new system.runtime.interopservices.comtypes.stgmedium();                 stgm.unionmember = intptr.zero;                 stgm.tymed = system.runtime.interopservices.comtypes.tymed.tymed_enhmf;                 pdo.getdata(ref fetc, out stgm);                  var hemf = gdi32.copyenhmetafile(stgm.unionmember, emfoutputfilename);                 storage.commit((int)ole32.stgc.default);                  pole.close(0);                 gdi32.deleteenhmetafile(stgm.unionmember);                 gdi32.deleteenhmetafile(hemf);             }             catch (exception ex)             {                 resultstring.appendline(ex.tostring());                 return resultstring.tostring();             }                      return resultstring.tostring();         } 

actually embedding files in openxml, necessary work old ole functions. there no other way around need 2 pieces:

  • a file going embedded
  • a picture shows content of file, screenshot of first page

i did write blog entry that: embedd pdf powerpoint usage of openxml. not requirement works identically.

there 2 issues pdfs when comes embedding:

  • embedded pdf documents have different content original pdf file. other ole formats know (excel, word, powerpoint, ...) not case. can use file on hard disk, pdf cannot.
  • you need take picture of first page. use pdfium or - there quite tools out there rendering pdf, adobe reader free, , job 100%.

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -