调整单词c#中的图像大小

时间:2016-09-27 09:07:57

标签: c# ms-word

我将我的Docx从" Letter格式" 转换为" A4" ,但现在布局选项已关闭。我想通过代码

来改变它
  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]

我尝试使用PageSetup高度和宽度,是的,值是在"点"将是30和21厘米。但这并不是正确的事情。以下是我希望通过code

更改的选项

1 个答案:

答案 0 :(得分:1)

修改

好的,这个人做了一些修补。形状问题是它们在剪切和粘贴它们时会保留它们的相对页面位置,因此从页面底部剪切并粘贴到下一页将粘贴在页面底部而不是顶部。

为了解决这个问题,我在切割之前将形状位置重新设置为左上角:

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;
}