'Interop Word - Wrong page size when converting a DOCX file to PDF
I'm running a .NET program in a Windows Server 2012 that converts docx files to pdf. The program has been developed using the interop library and it has been running for around a year in the same server with no issues (docx files converted result in a correct pdf with A4 page size). Somehow, in the last month, we started to have problems with the page size of the result pdf.
I've been struggling for a while finding the way to determinate if we've done any changes on the server that can cause this "wrong size format" but the truth is that this server it's barely open by anyone because it's only up for this purpose.
The thing is that when someone connects to this server via Remote Desktop the issue disapears and the conversion starts working correctly (the page size of the result pdf is correct). When the person disconnects (using Remote Desktop) without closing the session on the server it begins to misbehave.
So, here are my questions:
Is there any configuration on Windows Server 2012 that can cause this issue?
Is there a way to specify the page format size (interop library) in the conversion of docx files to pdf?
The part of the code that makes the conversion:
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();
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();
}
Thanks for your help in advance.
Solution 1:[1]
If anyone has this problem in the future apply this line of code:
doc.PageSetup.PaperSize = WdPaperSize.wdPaperA4;
before saving the document
// 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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alan V.B. |
