none
Shrink selection to one page RRS feed

  • Question

  • Hello. I'm writing an application that inserts some information into the document, based on dot-template. I open the document programmatically and iterate through it's merged fields to replace them with information from files.

    foreach (dynamic fld in oWordDoc.Fields)
    {
      //...

      if (fieldName == "certainField")
      {
          fld.Select();
     
          oWord.Selection.InsertFile(certainFieldFileName);
      }

      //...
    }

    The problem is that inserted information must fit in one word page, but, as usual, it's greater than one page, so I must decrease it's size (make font smaller, or something else) to fit it into the current page.

    Question: How can I select an information that was inserted with InsertFile method and decrease it's size to fit it to one page? Thank you.

    • Edited by KirillMedvedev Monday, December 27, 2010 8:04 AM source code formatting
    Monday, December 27, 2010 8:02 AM

Answers

  • Hi Kirill

    this isn't something Word is designed to do

    The best thing would probably be to open the file in question, check the number of pages and reduce the font size until it fits on one page. Then bring that into the "target" document.

    To get the inserted information after-the-fact, you can use an IncludeText field to link it in from the source document. That way you can easily assign the field.Result to a Range object, delete the field (remove the link to the source file leaving the text), then reduce the Range.Font.Size until you get the desired result


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by KirillMedvedev Monday, December 27, 2010 12:50 PM
    Monday, December 27, 2010 10:37 AM
    Moderator
  • Thank you! I've used your idea, but modified it a little. My implementation is:

    private void InsertTextInformation(dynamic oWord, dynamic oWordDoc, dynamic fld, string summaryFileName)
    {
        //Getting initial pages count
        int initialPagesCount = GetDocumentPagesCount(oWordDoc);

        //Selecting a field to replace it's data
        fld.Select();

        //The start symbol number (before field begins)
        int start = oWord.Selection.Range.Start;

        //Replacing selected field with information  from file
        oWord.Selection.InsertFile(summaryFileName);

        //The start symbol number (where inserted text ends)
        int end = oWord.Selection.Range.Start;

        //Selecting inserted text
        oWord.Selection.SetRange(start, end);

        //Reducing it's size while inserted text will fit one page
        while(GetDocumentPagesCount(oWordDoc) > initialPagesCount)
        {
            oWord.Selection.Font.Shrink();
        }
    }

    private int GetDocumentPagesCount(dynamic oWordDoc)
    {
        return oWordDoc.ComputeStatistics(2); //wdStatisticPages = 2
    }
    • Edited by KirillMedvedev Monday, December 27, 2010 12:57 PM source code formatting
    • Marked as answer by KirillMedvedev Monday, December 27, 2010 2:19 PM
    Monday, December 27, 2010 12:54 PM

All replies

  • Hi Kirill

    this isn't something Word is designed to do

    The best thing would probably be to open the file in question, check the number of pages and reduce the font size until it fits on one page. Then bring that into the "target" document.

    To get the inserted information after-the-fact, you can use an IncludeText field to link it in from the source document. That way you can easily assign the field.Result to a Range object, delete the field (remove the link to the source file leaving the text), then reduce the Range.Font.Size until you get the desired result


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by KirillMedvedev Monday, December 27, 2010 12:50 PM
    Monday, December 27, 2010 10:37 AM
    Moderator
  • Thank you! I've used your idea, but modified it a little. My implementation is:

    private void InsertTextInformation(dynamic oWord, dynamic oWordDoc, dynamic fld, string summaryFileName)
    {
        //Getting initial pages count
        int initialPagesCount = GetDocumentPagesCount(oWordDoc);

        //Selecting a field to replace it's data
        fld.Select();

        //The start symbol number (before field begins)
        int start = oWord.Selection.Range.Start;

        //Replacing selected field with information  from file
        oWord.Selection.InsertFile(summaryFileName);

        //The start symbol number (where inserted text ends)
        int end = oWord.Selection.Range.Start;

        //Selecting inserted text
        oWord.Selection.SetRange(start, end);

        //Reducing it's size while inserted text will fit one page
        while(GetDocumentPagesCount(oWordDoc) > initialPagesCount)
        {
            oWord.Selection.Font.Shrink();
        }
    }

    private int GetDocumentPagesCount(dynamic oWordDoc)
    {
        return oWordDoc.ComputeStatistics(2); //wdStatisticPages = 2
    }
    • Edited by KirillMedvedev Monday, December 27, 2010 12:57 PM source code formatting
    • Marked as answer by KirillMedvedev Monday, December 27, 2010 2:19 PM
    Monday, December 27, 2010 12:54 PM