以编程方式设置 MS Word 纸张大小为 PostScript 自定义页面大小

3

现场

我正在尝试以编程方式将MS Word(.docx)文件转换为PostScript文件(.ps)。 我通过使用与MS Windows捆绑的默认PostScript打印机驱动程序创建一个PostScript打印机,然后从Word使用此打印机打印Word文档来实现这一点。 问题是我正在尝试使用自定义页面大小进行此操作,即高度和宽度与任何标准纸张大小(例如A4,A3,Letter等)不匹配。

如果我在MS Word中手动执行此操作,则一切都按预期工作,但前提是我将“页面设置”纸张大小设置为 PostScript自定义页面大小 。 如果未设置为此值,则输出页面大小为预定义的页面大小之一,即B5(默认)。

Word default paper size - B5

但是,如果我将纸张大小设置为PostScript自定义页面大小,然后使用同一台打印机打印,输出文件的高度和宽度就与文档设置的正确匹配,例如,此处为181mm x 260mm。

PostScript Custom Page Size

问题

我无法以编程方式将页面设置纸张大小值设置为“PostScript自定义页面大小”,如果我不设置此值,则自定义高度和宽度将被忽略。

我已经尝试过的方法

我已经尝试了以下方法:

  1. Using the Word COM objects in PowerShell

    ...
    #create com object
    $word = New-Object -com Word.Application
    
    #dont open word UI
    $word.visible = $false
    
    #open input file
    $doc = $word.Documents.Open($inputfile)
    
    $width = [double]$word.MillimetersToPoints($widthInMM)
    $height = [double]$word.MillimetersToPoints($heightInMM)
    
    #set page setup width and height
    $doc.PageSetup.PageWidth = $width
    $doc.PageSetup.PageHeight = $height
    
    #save the changes
    $doc.Save()
    
    $pBackGround = 0
    $pAppend = 0
    $pRange = 0
    
    #print the file to default printer (i.e. ps printer)
    $doc.printout([ref]$pBackGround,[ref]$pAppend,[ref]$pRange,[ref]$outputfile)
    ...
    

    Looking at the MS docs, the PageSetup object has a PageSize property, which says the following on the page

将页面高度或宽度属性设置更改为wdPaperCustom的纸张大小属性。PaperSize属性是一个枚举类型WdPaperSize,其中包含以下值values。但是,正如您可以从上面的引用中看到的那样,如果设置高度和宽度,则纸张大小将设置为wdPaperCustom值。但这与PostScript自定义页面大小不同,根据我所读的,这不是有效的枚举值之一。
纯PowerShell:打印word(docx)文件的唯一方法是使用具有Print动词的Start-Process命令。如果您不想使用默认打印机,则可以将其导出到out-printer命令。
Start-Process $file -verb Print | out-printer -name "PrinterName"  

这会打印文档,但实际上会打开 Word 进行打印,存在两个问题:
a. 您必须手动指定输出文件名
b. 它仍然使用 MS Word 的默认页面设置
  1. Recording a VBA Macro: Recording setting the correct paper size doesn't record setting it to PostScript Custom Page Size. This is what the macro looks like

    With Selection.PageSetup
    .LineNumbering.Active = False
    .Orientation = wdOrientPortrait
    .TopMargin = MillimetersToPoints(13)
    .BottomMargin = MillimetersToPoints(13)
    .LeftMargin = MillimetersToPoints(13)
    .RightMargin = MillimetersToPoints(13)
    .Gutter = MillimetersToPoints(3)
    .HeaderDistance = MillimetersToPoints(12.5)
    .FooterDistance = MillimetersToPoints(12.5)
    .PageWidth = MillimetersToPoints(181)
    .PageHeight = MillimetersToPoints(260)
    .FirstPageTray = wdPrinterDefaultBin
    .OtherPagesTray = wdPrinterDefaultBin
    .SectionStart = wdSectionNewPage
    .OddAndEvenPagesHeaderFooter = True
    .DifferentFirstPageHeaderFooter = True
    .VerticalAlignment = wdAlignVerticalTop
    .SuppressEndnotes = False
    .MirrorMargins = True
    .TwoPagesOnOne = False
    .BookFoldPrinting = False
    .BookFoldRevPrinting = False
    .BookFoldPrintingSheets = 1
    .GutterPos = wdGutterPosLeft
    End With
    

    As you can see above there is no mention of the paper size being set to any value. I haven't tried this in c# or .NET because they all seem to use the COM Object API which reverts to my issues in 1.

我认为问题在于Word似乎忽略了打印机设置,甚至微软也承认了这一点
我正在使用打印机创建一个PostScript打印机,定义特定的纸张大小、高度和宽度,但是当打印时,MS Word会忽略这些设置并使用自己的默认设置。即使在Word中页面的高度和宽度被正确设置,但纸张大小属性似乎仍然搅乱了事情。
所以我能想到的唯一合理的做法就是将Word从混合物中移除。但问题在于我找不到任何可以正确处理Word的东西。你可以在PowerShell中直接将文件发送到打印机,但似乎仍然会打开Word并再次使用Word的设置。
有人知道解决此问题的方法或以编程方式设置纸张大小为PostScript自定义页面大小吗?

我无法提供帮助,只能为您已经收集到的信息提供另一个细微差别的信息:打印机/打印机驱动程序可以向这些列表中添加条目。 如果没有可用的PostScript打印机(已安装驱动程序),则您正在寻找的条目不会出现在列表中。 这就是为什么没有“wd”枚举器可用的原因。 - Cindy Meister
由于我无法访问这样的打印机,所以我无法测试我想到的唯一方法:每个wdPaperSize枚举都有一个数字等价物。您可以尝试使用“超出”枚举范围的值(从42开始)设置属性,以查看1)Word是否接受这些值,2)如果是,则该值在您的列表中对应哪个选项。请注意,即使这样做有效,与其他计算机上的相同,对应的值可能不同...(请注意,我会在VBA中进行测试,因为这比进程外代码更快,更“直接”)。 - Cindy Meister
@CindyMeister - 感谢您的小贴士。对我来说,打印机已经安装好了,但是我假设枚举类型是根据微软文档固定的,但我确实注意到枚举类型有数值,是的,我还没有尝试使用大于42的值来查看是否有效,问题在于它可能在一台机器上工作,但不一定适用于每台机器 - 不能保证值将是相同的,但感谢您的想法。 - Andrew Kew
@JohnKorchok - 我已将我正在使用的打印机设置为默认打印机,因此它是活动打印机,同时在测试时我也使用了激活打印机来运行任何逻辑。这不是关于更改纸张大小,而是更改为MS枚举中没有的特定值。 - Andrew Kew
打印机设置不属于Word。Word从活动打印机驱动程序获取可能的设置。因此,如果您的驱动程序不支持该设置,则Word也无法支持。 - John Korchok
显示剩余2条评论
1个回答

2
对于任何对我是如何解决这个问题感兴趣的人,我说解决了,但更多的是为了实现相同的结果而采取了不同的解决方案。
我仍然没有找到一种方法来将纸张大小更改为“PostScript自定义页面大小”,但是我能够根据文档中设置的宽度和高度来更改纸张大小(按照文档),这对我来说感觉更好。因此,这些是我解决问题的步骤:
  1. Chose a PostScript driver that I want to use. I decided to use the Xerox PS Class Driver, which is a PostScript driver bundled with Windows.

  2. Find where the driver is located. Printer drivers on Windows are located in following directory

     C:\Windows\System32\DriverStore\FileRepository\
    

    You can located the driver you are after using the following grep like command

     findstr /S /I /M /C:"Xerox PS Class Driver" C:\Windows\System32\DriverStore\FileRepository\*.*
    
  3. Edited the PPD file and added the paper size that I am looking for and set it as the default paper size. The most import part to update is PageSize, it provides an invocation value to invoke supported page sizes. I removed all other page sizes and just added the one I was after, calling it Custom

    *% Page Size
    *OpenUI *PageSize: PickOne
    *OrderDependency: 40 AnySetup *PageSize
    *DefaultPageSize: Custom
    *PageSize Custom/Custom: "featurebegin{<< /PageSize [369 522] >> setpagedevice}featurecleanup"
    *CloseUI: *PageSize
    
这段代码中的数值以点为单位,因此您需要进行转换。上面我使用的是130毫米 x 184毫米约等于369点 x 522点。
有关PPD文件的更多信息,请参阅规范文档
  1. Added a printer using this adjusted printer driver

    Add-Printer -Name "PrinterName" -DriverName "Xerox PS Class Driver" -PortName "file:"
    

    To keep things simple I named my printer the size of the page i.e. 130x184 so its easy to use programmatically

  2. Created a new form in the print server properties which matches my new paper size. To do this open Devices and Printers > Click your printer > Click Printer Server Properties in top menu > Check "Create a new form" checkbox > Add a name and set your dimenions > Save Form

enter image description here

使用我上面的PowerShell代码,当我的文档页面尺寸正确设置并且我的新打印机被设置为默认时,我们的打印机现在可以处理新的页面尺寸,所以我刚刚创建的新表格被发现了。在我的脚本中,我实际上将打印机设置为Windows上的默认打印机,只是我省略了这一部分,所以要么将其添加到脚本中,要么手动将打印机设置为默认打印机。

  • 使用新打印机打印PostScript文件

  • 希望这也能对其他人有所帮助。


    谢谢分享 - 点赞! - Cindy Meister

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接