Creating Word Document with different paper sizes
In This Topic
Word for WPF gives you the flexibility to create a word document with different paper sizes. You can use PaperKind enum to specify any of the available standard paper sizes.
Note that a class named WordUtils is used in the code given below. It is available in the product sample located at the following location on your system:
Documents\ComponentOne Samples\WPF\WordCreator
You can use these classes in your application from the mentioned location.
The implementation of PaperKind enum is given in the following code:
' create one page with each paper size
Dim firstPage As Boolean = True
For Each fi As var In GetType(PaperKind).GetFields(BindingFlags.[Static] Or BindingFlags.[Public])
' Silverlight/Phone doesn't have Enum.GetValues
Dim pk As PaperKind = DirectCast(fi.GetValue(Nothing), PaperKind)
' skip custom size
If pk = PaperKind.[Custom] Then
Continue For
End If
' add new page for every page after the first one
If Not firstPage Then
word.PageBreak()
End If
firstPage = False
' set paper kind and orientation
'rtf.PaperKind = pk;
word.Landscape = Not word.Landscape
' draw some content on the page
rc = WordUtils.PageRectangle(word)
rc = WordUtils.Inflate(rc, -6, -6)
Dim text As String = String.Format("PaperKind: [{0}];" & vbCr & vbLf & "Landscape: [{1}];" & vbCr & vbLf & "Font: [Tahoma 18pt]", pk, word.Landscape)
word.DrawString(text, font, Colors.Black, rc, sf)
word.DrawRectangle(Colors.Black, rc)
Next
// create one page with each paper size
bool firstPage = true;
foreach (var fi in typeof(PaperKind).GetFields(BindingFlags.Static | BindingFlags.Public))
{
// Silverlight/Phone doesn't have Enum.GetValues
PaperKind pk = (PaperKind)fi.GetValue(null);
// skip custom size
if (pk == PaperKind.Custom) continue;
// add new page for every page after the first one
if (!firstPage) word.PageBreak();
firstPage = false;
// set paper kind and orientation
//rtf.PaperKind = pk;
word.Landscape = !word.Landscape;
// draw some content on the page
rc = WordUtils.PageRectangle(word);
rc = WordUtils.Inflate(rc, -6, -6);
string text = string.Format("PaperKind: [{0}];\r\nLandscape: [{1}];\r\nFont: [Tahoma 18pt]", pk, word.Landscape);
word.DrawString(text, font, Colors.Black, rc, sf);
word.DrawRectangle(Colors.Black, rc);
}