Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
645
WordDocumentWriter - pagesize and cultureinfo/language
posted

Hi,

I am creating a word-document using this approach (only a stub of beginning included):

Private Sub CreateWordReport()
Me.UltraStatusBar.Text = ""
Dim docWriter As WordDocumentWriter = Nothing
Try
docWriter = WordDocumentWriter.Create("c:\testfolder\" & CType(Me.JournalYearToolStripTextBox.Text, String) & "-" & _
CType(Me.JournalNoToolStripTextBox.Text, String) & ".docx")
Catch ex As Exception
Select Case ex.HResult
Case -2147024864
Me.UltraStatusBar.Text = "Feil oppstod ved opprettelse av dokumentet. Vennligst lukk evnt. åpne svarbrev på samme sak."
Exit Sub

End Select

End Try


docWriter.StartDocument()
docWriter.DocumentProperties.LatinCulture = "no-bm"

.....................

The code I have written works fine, but there is two things that I struggle with:

1. I am not able to specify that I want the ".docx" to be a "A4"-document instead of a "letter" (which seems to be default).

2. In MS word my default language is "Norwegian - bokmal", but in documents created  this way the language setting is always reverted to english. I have tried to use the following line: 'docWriter.DocumentProperties.LatinCulture = "no-bm"', but this does not help.

Regards

Kai

Parents
  • 48586
    Verified Answer
    posted

    Hello ,

     

    Paper size if a word page is defined by PageSize property of the SectionProperties to which this page belongs (in most cases you should apply this to the “FinalSectionProperties”). Also you should note that Word works with inches and size is in “pixels”(I don’t pretend that the formula used for conversion is absolutely accurate) So here is a simple code snipped:

     

    WordDocumentWriter word = WordDocumentWriter.Create("test.docx");
    word.StartDocument();
    word.FinalSectionProperties.PageSize = GetSize(PaperKind.A4);
    word.EndDocument(true);

    Process.Start("test.docx");

    private SizeF? GetSize(PaperKind paperKind)
    {

                PrinterSettings settings = new PrinterSettings();
                PaperSize size = settings.PaperSizes.Cast<PaperSize>().FirstOrDefault( p => p.Kind == paperKind);
                if(size != null)
                    return new SizeF(toInch(size.Width), toInch(size.Height));
                else
                    throw new Exception();
    }

     

            private float toInch(float pixels)
            {
                return pixels * 71f / 98f;
            }

     

    I hope that this will helps you.

Reply Children