复制代码
- function Get-ImageScaleSize {
- [CmdletBinding()]
- [OutputType([double])]
- param(
- [Parameter(Mandatory = $true)]
- [ValidateScript( { $_ -gt 0 })]
- [double]$ContainerWidth,
- [Parameter(Mandatory = $true)]
- [ValidateScript( { $_ -gt 0 })]
- [double]$ContainerHeight,
- [Parameter(Mandatory = $true)]
- [ValidateScript( { $_ -gt 0 })]
- [double]$ImageWidth,
- [Parameter(Mandatory = $true)]
- [ValidateScript( { $_ -gt 0 })]
- [double]$ImageHeight
- )
- $rContainer = $ContainerWidth / $ContainerHeight
- $rImage = $ImageWidth / $ImageHeight
- if ($rImage -le $rContainer) {
- $h = $ContainerHeight
- $w = $ImageWidth * $ContainerHeight / $ImageHeight
- } else {
- $w = $ContainerWidth
- $h = $ImageHeight * $ContainerWidth / $ImageWidth
- }
- $w, $h
- }
- function Edit-WWWDoc {
- param (
- [string]$LiteralPath
- )
- $providerPath = Convert-Path -LiteralPath $LiteralPath
- $doc = $wordApp.Documents.Open($providerPath)
- $pageWidth = $doc.PageSetup.PageWidth
- $pageHeight = $doc.PageSetup.PageHeight
- $leftMargin = $doc.PageSetup.LeftMargin
- $rightMargin = $doc.PageSetup.RightMargin
- $topMargin = $doc.PageSetup.TopMargin
- $bottomMargin = $doc.PageSetup.BottomMargin
- $docWidth = $pageWidth - $leftMargin - $rightMargin
- $docHeight = $pageHeight - $topMargin - $bottomMargin
- # InlineShapes
- foreach ($shapeInline in $doc.InlineShapes) {
- $null = $shapeInline.ConvertToShape()
- }
- foreach ($shape in $doc.Shapes) {
- $shape.WrapFormat.Type = $wdWrapTopBottom
- $shape.WrapFormat.Side = $wdWrapBoth
- $shape.LockAspectRatio = $msoTrue
- $w, $h = Get-ImageScaleSize -ContainerWidth $docWidth -ContainerHeight $docHeight -ImageWidth $shape.Width -ImageHeight $shape.Height
- if ($w -lt $shape.Width) {
- $shape.Width = [float]$w
- }
- # $shape.Width
- # $shape.Height
- }
- # Tables
- foreach ($table in $doc.Tables) {
- $table.PreferredWidthType = $wdPreferredWidthPercent
- $table.PreferredWidth = 100
- }
- # clear all text and paragraph formattings
- $doc.Content.Select()
- $wordApp.Selection.ClearFormatting()
- # clear highlight color
- $wordApp.Selection.Range.HighlightColorIndex = $wdNoHighlight
- # remove blank paragraphs
- for ($i = $doc.Paragraphs.Count; $i -ge 1 ; $i--) {
- $para = $doc.Paragraphs[$i]
- if ([string]::IsNullOrWhiteSpace($para.Range.Text)) {
- $null = $para.Range.Delete()
- }
- }
- # Font
- $paraFont = $doc.Content.Font
- $paraFont.NameAscii = "Consolas"
- $paraFont.NameOther = "Consolas"
- $paraFont.NameFarEast = "微软雅黑"
- $paraFont.Size = 12
- # ParagraphFormat
- $paraFormat = $doc.Content.ParagraphFormat
- $paraFormat.LineSpacingRule = $wdLineSpaceExactly
- $paraFormat.LineSpacing = 12
- $paraFormat.LineUnitAfter = 0
- $paraFormat.LineUnitBefore = 0
- $paraFormat.SpaceAfter = 0
- $paraFormat.SpaceBefore = 0
- $doc.Close($true)
- }
- # const
- $wdPreferredWidthAuto = 1
- $wdPreferredWidthPercent = 2
- $wdPreferredWidthPoints = 3
- $wdInlineShapePicture = 3
- $wdInlineShapeLinkedPicture = 4
- $wdWrapSquare = 0
- $wdWrapBoth = 0
- $wdWrapTopBottom = 4
- $wdLineSpaceExactly = 4
- $wdLineSpaceSingle = 0
- $wdLineSpaceMultiple = 5
- $wdLineSpaceAtLeast = 3
- $wdNoHighlight = 0
- $msoTrue = -1
- $msoLinkedPicture = 11
- $msoPicture = 13
- # main
- $wordApp = New-Object -ComObject Word.Application -ErrorAction Stop
- Edit-WWWDoc -LiteralPath .\安.docx
- $wordApp.Quit()
复制代码
- # ParagraphFont
- $paraFont = $doc.Content.font
- $paraFont.NameFarEast = "微软雅黑"
- $paraFont.NameAscii = "Consolas"
- $paraFont.NameOther = "Consolas"
欢迎光临 批处理之家 (http://bathome.net./) | Powered by Discuz! 7.2 |