询问者
MSWord hangs while converting the docx file to pdf

问题
-
Hi,
wordDoc->ExportAsFixedFormat() gets hung, while trying to convert specific docx files. This is observed on windows 2012 server, MS Office 2010.
Please let me know if you want some more information. Code is in C++.
Please let me know how to attach or send the problematic document.
Note: Surprisingly If I move the content in 42nd page to 41st page and move back to 42nd page then conversion is successful.
Thanks,
Shiva.
- 已编辑 shivamnkumar 2015年2月17日 3:39
2015年2月17日 3:36
全部回复
-
Hello Shiva,
Where and when do you run the code?
Be aware, the Considerations for server-side Automation of Office article states the following:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.2015年2月17日 12:30 -
Hi shivamnkumar
I fully concur with Eugene's concerns about server-side automation...
<<wordDoc->ExportAsFixedFormat() gets hung, while trying to convert specific docx files...Note: Surprisingly If I move the content in 42nd page to 41st page and move back to 42nd page then conversion is successful.>>
My initial thought is that the document structures may be damaged in some way. Would the problem documents all originate from the same source - template, code, whatever - and contain similar content on this 42nd page? Cutting or copying, then pasting, makes Word do some internal conversions and these are probably throwing out content Word recognizes as being "wrong" when it encounters it during the paste method.
Cindy Meister, VSTO/Word MVP, my blog
2015年2月17日 15:53 -
Thanks for your reply. Here are couple of observations.
The same document is converted to PDF successfully in windows xp and office 2007.
on Windows 7 with office 2007, pdf conversion makes msword process hung. But rtf and html conversions are successful with the original document.
Thanks,
Shiva
2015年2月17日 17:05 -
Hi Shiva
Do you see the same behavior if you try to convert the document as a user, via "Office button"/Save As?
If yes, then the better place to discuss this problem would be the IT Pro forum. People there probably have more experience with conversion processes; this forum targets the Word object model and it's unlikely that the object model is a factor...
Cindy Meister, VSTO/Word MVP, my blog
2015年2月17日 17:44 -
Hi Bhaskara,
>>Office button/saveAs is working properly. Event C# code is working properly. Only C++ code is not working properly<<
Please note, the Office automation on server side is not recommend and support as Eugene metioned. As a workaround, you may need to consider to use a alternatives way. You can get more detail about this topic from link below:
Considerations for server-side Automation of Office
Regards & Fei
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.2015年2月19日 6:10 -
Hi Bhaskara,
Did the code work on the clint side? If not, would you mind sharing us a code sample to help us to repreduce this issue?
Regards & Fei
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.2015年3月3日 9:06 -
public static void ConvertWordtoPDF(String inFilePath, String outFilePath)
{
// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
word.Visible = false;
word.ScreenUpdating = false;
// Cast as Object for word Open method
Object filename = (Object)inFilePath;// "e:\\Testme_original.docx";
// Use the dummy value as a placeholder for optional arguments
Document doc = word.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
doc.Activate();
String outputFileName = outFilePath;// "e:\\Testme_original.docx".Replace(".docx", ".pdf");
object fileFormat = WdSaveFormat.wdFormatPDF;
// Save document into PDF Format
doc.ExportAsFixedFormat(outputFileName,
Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, false,
Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint,
Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 1,1,Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentWithMarkup,false, true,
Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks,
true,true,false, ref oMissing);
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
}
}2015年4月1日 8:39 -
Hi, I recommend the following solution which does not require MS Office to be installed on neither developer's or client's machine.
using System; using Spire.Doc; using Spire.Doc.Documents; namespace DoctoPDF { class toPDF { static void Main(string[] args) { //Load Document Document document = new Document(); document.LoadFromFile(@"E:\work\documents\TestSample.docx"); //Convert Word to PDF document.SaveToFile("toPDF.PDF", FileFormat.PDF); } } }
- 已编辑 Ezreal93 2020年10月28日 7:05
2020年10月28日 6:50