OpenXml entry --- insert images in word, openxml --- word

Source: Internet
Author: User

OpenXml entry --- insert images in word, openxml --- word

Next we will introduce how to insert images in word. By the way, the younger brother found that the MSDN official website has a complete OpenXML tutorial. Although it is full of English, it is still very helpful.

Tips, there is no template in the original excerpt code. We found that the size of the picture inserted in word is different from that in copy. We recommend that you insert an image in word by yourself, decompile the C # code through OpenXml Tools, change the image size, and decompile the code again.

Use byeond compare [http://www.scootersoftware.com/] To compare C # code, it will be found that because new DW. Extent () {Cx = 99213l, Cy = 792000L} is because of this set. In the future, we can use OpenXml Tools for decompilation and comparison in many places. View the attribute position of the Set style.

Http://msdn.microsoft.com/en-us/library/office/bb491088 (v = office.15). aspx

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using DocumentFormat. openXml; using DocumentFormat. openXml. packaging; using DocumentFormat. openXml. wordprocessing; using System. IO; using DW = DocumentFormat. openXml. drawing. wordprocessing; using PIC = DocumentFormat. openXml. drawing. pictures; using A = DocumentFormat. openXml. drawing; namespace Ad Dpicturew.word {public class Program {public static void Main (string [] args) {string picPath = "regular"; string filePath = "Test.docx"; addpicturew.word (filePath, picPath );} public static void addpicturew.word (string filePath, string picturePath) {using (WordprocessingDocument doc = WordprocessingDocument. open (filePath, true) {string picType = picturePath. Split ('. '). last (); ImagePartType imagePartType; ImagePart imagePart = null; // The image type is determined by the suffix. true indicates that the case-insensitive if (Enum. tryParse <ImagePartType> (picType, true, out imagePartType) {imagePart = doc. mainDocumentPart. addImagePart (imagePartType);} imagePart. feedData (File. open (picturePath, FileMode. open); // read the binary stream AddImageToBody (doc, doc. mainDocumentPart. getIdOfPart (imagePart);} // Excerpted from http://msdn.m Icrosoft.com/EN-US/library/office/bb497430 (v = office.15). aspx? Cs-save-lang = 1 & cs-lang = csharp # code-snippet-5
Private static void AddImageToBody (WordprocessingDocument wordDoc, string relationshipId) {// Define the reference of the image. var element = new Drawing (new DW. inline (new DW. extent () {Cx = 9910000l, Cy = 792000L}, // adjust the image size new DW. extends textent () {LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L}, new DW. docProperties () {Id = (UInt32Value) 1U, Name = "Picture 1"}, new DW. nonVisualGraphicFrameDrawingProperties (new. graphicFrameLocks () {NoChangeAspect = true}), new. graphic (new. graphicData (new PIC. picture (new PIC. nonVisualPictureProperties (new PIC. nonVisualDrawingProperties () {Id = (UInt32Value) 0U, Name = "New Bitmap Image.jpg"}, new PIC. nonVisualPictureDrawingProperties (), new PIC. blipFill (new. blip (new. blipExtensionList (new. blipExtension () {Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"}) {Embed = relationshipId, CompressionState =. blipCompressionValues. print}, new. stretch (new. fillRectangle (), new PIC. shapeProperties (new. transform2D (new. offset () {X = 0L, Y = 0L}, new. extents () {Cx = 9910000l, Cy = 792000L}), // align with the above new. presetGeometry (new. adjustValueList () {Preset =. shapeTypeValues. rectangle}) {Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"}) {DistanceFromTop = (UInt32Value) 0U, DistanceFromBottom = (UInt32Value) 0U, DistanceFromLeft = (UInt32Value) 0U, distanceFromRight = (UInt32Value) 0U, EditId = "50D07946"}); // Append the reference to body, the element shoshould be in a Run. wordDoc. mainDocumentPart. document. body. appendChild (new Paragraph (new Run (element )));}}}


Insert an image in WORD to show only a small part of the image below

Format-paragraph-line spacing changed to single-factor line spacing

Embedded images are processed as one character. If the line spacing is set to a fixed value, the image can only display a fixed height.

How can I insert an image into a word to minimize the space occupied by the image?

Select an image, right-click and choose "set object format"-"image" and select "compression"-apply: Select "All images in the document"; change resolution: Select "print "; option: Select "compressed image" and "delete image Cropping Area"-"OK"-"OK"
Or right-click in the blank area of the toolbar and check the image in the drop-down menu. Select the image, click the "compress image" button on the toolbar, and apply it to: Select "All images in the document" in the displayed dialog box; change resolution: Select "print"; options: select "compressed image" and "delete image Cropping Area"-"OK"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.