簡體   English   中英

用單詞C#調整圖像大小

[英]Resize Image in a word c#

我將Docx從“字母格式”轉換“ 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 Height和Width,是的,“ Points”中的值將為30和21 cm。 但這並不是正確的選擇。 這是我想通過更改的文字選項 碼

編輯:

好的,這需要一些修補。 形狀的問題在於,它們在剪切和粘貼時會保留它們在頁面上的相對位置,因此從頁面底部剪切並粘貼到下一頁上會將它們粘貼到頁面底部而不是頁面頂部。

要解決此問題,我在切割之前將形狀位置重新設置為左上方:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM