Niko.K
Niko.K

Reputation: 307

Resize Image in a word c#

I converted my Docx from the "Letter format" to "A4" but now the Layout options are off. And I would like to change that via code

  var wordApplication = new Application();
        var document = wordApplication.Documents.Open(@"C:\Users\test\Desktop\Test\test.docx");


        //Set paper Size
        document.PageSetup.PaperSize = WdPaperSize.wdPaperA4;

        //Set paper Format
        document.PageSetup.PageHeight = 855;
        document.PageSetup.PageWidth = 595;


        //Doc save
        document.Save();

        //Close word
        wordApplication.Quit();[![enter image description here][1]][1]

I tried to use the PageSetup Height and Width and yes the values are in "Points" would be 30 and 21 cm. But that does not seam to be the right thing. Here is option in word that I would like to change via code

Upvotes: 0

Views: 2437

Answers (1)

LocEngineer
LocEngineer

Reputation: 2917

Edit:

OK, this one took some tinkering. trouble with shapes is that they retain their relative page position when you cut and paste them, so cutting from page bottom and pasting on the next page would paste them at the page bottom, not at the top.

To fix this, I re-set the shape position to top left before cutting:

var maxHeight = doc.PageSetup.PageHeight - doc.PageSetup.BottomMargin;
foreach (Word.Shape shape in doc.Shapes)
{
    //scale to 97.2%
    shape.Width = (float)0.972*shape.Width;
    shape.Height = (float) 0.972*shape.Height;

    var pos = (float)shape.Anchor.Information[Word.WdInformation.wdVerticalPositionRelativeToPage];
    var curPage = (int)shape.Anchor.Information[Word.WdInformation.wdActiveEndPageNumber];
    if (pos > maxHeight)
    {
        //Re-set position of shape to top left before cut/paste:
        shape.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
        shape.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
        shape.Left = WdApp.InchesToPoints((float)0.889);
        shape.Top = WdApp.InchesToPoints((float)0.374);
        shape.Select();
        WdApp.Selection.Cut();
        WdApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage, curPage + 1);
        WdApp.Selection.Paste();
    }
}
foreach (Word.InlineShape inline in doc.InlineShapes)
{
    //scale to 97.2%
    inline.Width = (float)0.972 * inline.Width;
    inline.Height = (float)0.972 * inline.Height;
}

Upvotes: 1

Related Questions