Interop Word:DOCX转PDF时页面尺寸异常问题技术问询
Hey Alan, let’s break down your questions and the root of the problem you’re facing:
1. Could Windows Server 2012 system configurations cause this issue?
Absolutely, and your observation about RDP sessions gives a crucial clue here.
Word Interop relies heavily on Word’s desktop rendering engine, which is tightly tied to the active user session context. Here’s what’s likely happening:
- When connected via RDP, your user session is active, so Word loads your profile’s default settings—including the default printer’s paper size (which is set to A4, matching your expected output).
- When you disconnect RDP but leave the session open, the session enters a "disconnected" state. In this state, Windows doesn’t maintain the full desktop context for the session. Word may fall back to using the system-wide default printer (or a virtual printer configured with a non-A4 size) instead of your user’s default, which causes the PDF to inherit that incorrect paper size.
Since the server is rarely accessed, a silent Windows or Office update could have subtly changed system-level default printer settings, or adjusted how disconnected sessions handle desktop rendering.
2. Can you specify page size via Interop during DOCX to PDF conversion?
Yes! You can explicitly enforce the A4 page size in your code, which eliminates dependency on external system or session settings. This is the most reliable fix for your issue.
Here’s how to modify your existing code to force A4 dimensions:
public string convert(FileInfo wordFile) { Document doc = null; object outputFileName = null; try { Object filename = (Object)wordFile.FullName; word.Visible = false; word.ScreenUpdating = false; // Use the dummy value as a placeholder for optional arguments 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(); // Explicitly set all document sections to A4 paper size foreach (Section section in doc.Sections) { section.PageSetup.PaperSize = WdPaperSize.wdPaperA4; // Optional: Hardcode dimensions as a safety net to override printer-dependent quirks section.PageSetup.PageWidth = Word.Application.InchesToPoints(8.27f); // A4 width (inches converted to Word's point unit) section.PageSetup.PageHeight = Word.Application.InchesToPoints(11.69f); // A4 height } outputFileName = wordFile.FullName.Replace(".docx",".pdf"); object fileFormat = WdSaveFormat.wdFormatPDF; // Save document into PDF Format doc.SaveAs(ref outputFileName, ref fileFormat, 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); } catch (Exception e) { throw e; } finally { // 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; } return outputFileName.ToString(); }
Key details about this modification:
- We loop through all sections in the document (in case the DOCX has multiple sections with different sizes) to ensure every part uses A4.
- The explicit
PageWidthandPageHeightvalues act as a safety net, guaranteeing correct dimensions even if thePaperSizeenum isn’t respected due to printer-specific quirks.
Extra Tips to Prevent Future Issues
- Verify system default printer: Check that the server’s system-wide default printer is configured for A4 paper. Even with the code fix, this avoids other unexpected rendering problems.
- Log off instead of disconnect: When closing RDP, log off your session entirely instead of just disconnecting. This ensures the session is fully terminated, so the next time your app runs, it starts with a fresh session and correct user settings.
内容的提问来源于stack exchange,提问作者Alan V.B.

