2010-12-29 6 views
1

私は、図形の中に配置された "Picture Content Control"を持つ.docxテンプレートを持っています。ユーザーが選択したイメージのサイズが異なるため、ユーザーが選択したイメージをその特定の領域に挿入しました。「ピクチャコンテンツコントロール」とコンテナ(図形)の高さと幅のスケールをプログラムで調整するソリューションを探しています。私は私の.docxのテンプレートに画像を挿入するには、このソリューションを使用してい :これらの事については http://www.codeproject.com/KB/office/Word_2007_Images.aspxWord文書のイメージサイズをプログラムで変更する

答えて

0

、私は多くの場合、マクロの記録から来ることができる最も有用な答えを見つけること、そして、あなたがプログラムが何をしたいのかやってマクロによって生成されたコードを見直し、必要に応じて調整します。マクロは、Wordオブジェクトを使用して、何をしたいのかを示す方法を示します。

+0

Thnksランス、しかし私は "OPENXML"ソリューションを探しています – Damon

0

これはあまりにも私のトラブルの多くを引き起こしたが、私は解決策を見つけた:

//Get SdtElement (can be a block, run... so I use the base class) with corresponding Tag 
SdtElement block = doc.MainDocumentPart.Document.Body.Descendants<SdtElement>() 
    .FirstOrDefault(sdt => sdt.SdtProperties.GetFirstChild<Tag>()?.Val == contentControlTag) 

//Get First drawing Element and get the original sizes of placeholder SDT 
//I use SDT placeholder size as maximum size to calculate picture size with correct ratios 
Drawing sdtImage = block.Descendants<Drawing>().First(); 
double sdtWidth = sdtImage.Inline.Extent.Cx; 
double sdtHeight = sdtImage.Inline.Extent.Cy; 
double sdtRatio = sdtWidth/sdtHeight; 

*Calculate final width/height of image* 

//Resize picture placeholder 
sdtImage.Inline.Extent.Cx = finalWidth; 
sdtImage.Inline.Extent.Cy = finalHeight; 

//Change width/height of picture shapeproperties Transform 
//This will override above height/width until you manually drag image for example 
sdtImage.Inline.Graphic.GraphicData 
     .GetFirstChild<DocumentFormat.OpenXml.Drawing.Pictures.Picture>() 
     .ShapeProperties.Transform2D.Extents.Cx = finalWidth; 
sdtImage.Inline.Graphic.GraphicData 
     .GetFirstChild<DocumentFormat.OpenXml.Drawing.Pictures.Picture>() 
     .ShapeProperties.Transform2D.Extents.Cy = finalHeight; 

すべてこの後、私は、コンテンツコントロールを削除し、ちょうど私の中の実行としてsdtContentからコードを挿入することを選択します段落しかし、これはもちろんオプションです:)

関連する問題