| 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 |
| |
| 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 |
| } |
| |
| |
| } |
| |
| foreach ($table in $doc.Tables) { |
| $table.PreferredWidthType = $wdPreferredWidthPercent |
| $table.PreferredWidth = 100 |
| } |
| |
| $doc.Content.Select() |
| $wordApp.Selection.ClearFormatting() |
| |
| $wordApp.Selection.Range.HighlightColorIndex = $wdNoHighlight |
| |
| for ($i = $doc.Paragraphs.Count; $i -ge 1 ; $i--) { |
| $para = $doc.Paragraphs[$i] |
| if ([string]::IsNullOrWhiteSpace($para.Range.Text)) { |
| $null = $para.Range.Delete() |
| } |
| } |
| |
| $paraFont = $doc.Content.Font |
| $paraFont.NameAscii = "Consolas" |
| $paraFont.NameOther = "Consolas" |
| $paraFont.NameFarEast = "微软雅黑" |
| $paraFont.Size = 12 |
| |
| |
| $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) |
| } |
| |
| $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 |
| |
| |
| $wordApp = New-Object -ComObject Word.Application -ErrorAction Stop |
| |
| Edit-WWWDoc -LiteralPath .\安.docx |
| |
| $wordApp.Quit()COPY |