$Doc = $Word.Documents.Add();
$Section = $Doc.Sections.Item(1);
$Header = $Section.Headers.Item(1);
$Header.Range.Text = "document1_${FirstName}_${SecondName}_${StaffID}.doc";
$objRange = $Doc.Range()
$objRange.Font.Name = “Arial”
$objRange.Font.Size = 12
$Doc.SaveAs("$fileserver\document1_${FirstName}_${SecondName}_${StaffID}.docx");
$Doc.Close();
I am using this script to create word documents and deploy them to staff areas on a fileserver. Is there a way to add page numbers into the documents?
The easiest way of doing this is as below:
$Word = New-Object -ComObject word.application
$word.Visible = $false
$Doc = $Word.Documents.Add()
$Section = $Doc.Sections.Item(1)
$Header = $Section.Headers.Item(1)
$Header.Range.Text = "document1_${FirstName}_${SecondName}_${StaffID}.docx"
$objRange = $Doc.Range()
$objRange.Font.Name = "Arial"
$objRange.Font.Size = 12
# ad a pagenumber (for demo centered to the page width)
# for other values see https://learn.microsoft.com/en-us/office/vba/api/word.wdpagenumberalignment
$wdAlignPageNumberCenter = 1
[void]$Section.Footers(1).PageNumbers.Add($wdAlignPageNumberCenter)
$Doc.SaveAs("$fileserver\document1_${FirstName}_${SecondName}_${StaffID}.docx")
$Doc.Close()
# IMPORTANT Cleanup COM objects after use
$Word.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objRange)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Section)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Header)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
I’m trying to make a signature maker and I want to write name and job in Hebrew but it starts from the left like English. There is a way that the text will start from the right?
This is my code for example:
Add-Type -AssemblyName System.Drawing
$uname='סער'#Read-Host "insert name:"
$job='יינחןינצל'#Read-Host "insert job:"
$filename = "$home\desktop\sign.png"
$bmp = new-object System.Drawing.Bitmap 600,200
#Get the image
#$source=Get-Item
#$img = [System.Drawing.Image]::FromFile($_.FullName)
#Create a bitmap
#$bmp = new-object System.Drawing.Bitmap([int]($img.width)),([int]($img.height))
$font = new-object System.Drawing.Font Consolas,18
$brushBg = [System.Drawing.Brushes]::White
$brushFg = [System.Drawing.Brushes]::Black
$graphics = [System.Drawing.Graphics]::FromImage($bmp)
$graphics.FillRectangle($brushBg,0,0,$bmp.Width,$bmp.Height)
$graphics.DrawString($uname,$font,$brushFg,10,10)
$graphics.DrawString($job,$font,$brushFg,90,100)
$graphics.Dispose()
$bmp.Save($filename)
Invoke-Item $filename
As commented, the DrawString() method has a constructor with which you can give it a StringFormat object where you can set RightToLeft text direction.
In your code try:
$rtlFormat = [System.Drawing.StringFormat]::new([System.Drawing.StringFormatFlags]::DirectionRightToLeft)
$graphics.DrawString($uname, $font, $brushFg, [System.Drawing.PointF]::new(10,10), $rtlFormat)
$graphics.DrawString($job, $font, $brushFg, [System.Drawing.PointF]::new(90,100), $rtlFormat)
If you are using a PowerShell version that is too old for the [type]::new() syntax, use
$rtlFormat = New-Object -TypeName System.Drawing.StringFormat('DirectionRightToLeft')
$graphics.DrawString($uname, $font, $brushFg, (New-Object -TypeName System.Drawing.PointF(10,10)), $rtlFormat)
$graphics.DrawString($job, $font, $brushFg, (New-Object -TypeName System.Drawing.PointF(90,100)), $rtlFormat)
$sf = [System.Drawing.StringFormat]::New()
$sf.Alignment = 'far'
$sf.LineAlignment = 'far'
$graphics.FillRectangle($brushBg,0,0,$bmp.Width,$bmp.Height)
$graphics.DrawString($uname,$font,$brushFg,500,100,$sf)
$graphics.DrawString($job,$font,$brushFg,500,200,$sf)
This is what I did in the end
Thaks Theo
I am using powershell to create a docx file.
I was able to create a file and set the style, font and alignment.
My problem is with changing the text direction from default (LTR) to RTL.
This is my working code:
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$doc = $word.documents.add()
$selections = $word.Selection
$selections.Font.Name = "Arial"
$selections.Font.Size = 16
$selections.paragraphFormat.Alignment = 2
$selections.TypeText("Hello World!")
$out_path = ".\file.docx"
$doc.SaveAs($out_path)
$doc.Close()
$word.Quit()
I did not find any information to set the text direction.
I will appreciate any help,
Thank you.
in your program use this:
$doc = $word.documents.add()
$doc.Paragraphs.ReadingOrder = 0
I am trying to use Powershell to do a find and replace of some text within a Text Box that is within the Header of a Word Document (.docx). I was able to get it working for text outside of the Header but not that within. I think it is failing because I am not correctly accessing the contents of the Text Box, so I added in the final line (before quit and save) to see what the text was but it printed out blank for each of the three Items in my Header. This is my first time using Powershell and I think I have perhaps spent more time learning this and writing it than I will save by using it...
The relevant snippet of the script is below:
$word = New-Object -COM "Word.Application";
$word.Visible = $false;
$doc = $word.Documents.Open($FullPath);
$selection = $word.Selection;
$section = $doc.sections.item(1);
$header = $section.headers.Item(3);
$FindText = "Cnnn";
$MatchCase = $False;
$MatchWholeWord = $False;
$MatchWildcards = $False;
$MatchSoundsLike = $False;
$MatchAllWordForms = $False;
$Forward = $True;
$wdFindContinue = 1;
$Wrap = $wdFindContinue;
$Format = $False;
$wdReplaceNone = 0;
$ReplaceAll = 2;
$ReplaceWith = "C" + $newString;
$a = $header.Find.Execute($FindText,$MatchCase,$MatchWholeWord, `
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith, $ReplaceAll);
Write-Host ("Header is: " + $header.Text);
$doc.Save();
$word.Quit();
You need to apply your search to the .TextFrame.TextRange.Find object from the TextBox or any shape containing text.
You could try something like this:
If ($header.ShapeRange.Count) {
ForEach ($shp in $header.ShapeRange) {
If ($shp.TextFrame.HasText) {
$obj = $shp.TextFrame.TextRange.Find
$a = $obj.Execute($FindText,$MatchCase,$MatchWholeWord,`
$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,`
$Wrap,$Format,$ReplaceWith,$ReplaceAll)
}
}
}
$Word = New-Object -ComObject Word.Application;
$Word.Visible = $true;
$Doc = $Word.Documents.Add();
$Section = $Doc.Sections.Item(1);
$Header = $Section.Headers.Item(1);
$Footer = $Section.Footers.Item(1);
$Footer.PageNumbers.Add($wdAlignPageNumberCenter);
$Header.Range.Text = "abcd";
$selection.Font.Name="Courier New";
$selection.Font.Size=11;
**$selection.Font.Spacing=1.0;**
$selection=$word.Selection;
Even though I specified my font spacing as 1.0, it is taking a default value and it seems that this is not working. Do I need to make any changes?
Also, this displays the page number in the footer column i.e. 1 or 2 or 3 like this but I want it to be like Page 1, Page 2 ,Page 3. How do I do that?
ok i have a temporary solution after some groundwork for displaying page 1,page 2 ,page 3
$page="`tPage";
$Word = New-Object -ComObject Word.Application;
$Word.Visible = $true;
$Doc = $Word.Documents.Add();
$Section = $Doc.Sections.Item(1);
$Footer = $Section.Footers.Item(1);
$Footer.Range.Text=$page;
$Footer.PageNumbers.Add($wdAlignPageNumberCenter);