Denna handledning guidar dig steg-för-steg om hur du lägger till bild i Word-dokument med C#. Vi använder en kommandoradsapplikation i C# lägg till bild i word-dokument.
Steg för att lägga till bild i Word-dokument med C#
- Lägg till referens till System.Drawing montering i lösning
- Därefter måste Aspose.Words for .NET NuGet-paketreferens läggas till
- Lägg till med hjälp av direktiv för Aspose.Words och Aspose.Words.Drawing namnrymder
- Ring License.SetLicense-metoden
- Skapa dokumentobjekt för att ladda Word DOC från filsystemet eller minnesströmmen
- Skapa DocumentBuilder-klassobjekt för att skriva text, bilder, tabeller etc.
- Flytta markören till sidhuvud eller sidfot eller valfri position i Word DOC
- Använd DocumentBuilder.InsertImage för att lägga till bild från ström eller fil
- Använd Shape class för att ytterligare ställa in storlek, position, fyllning etc. för bilden
- Ring Document.Save-metoden för att spara Word DOC till disk eller stream
Du kan använda följande kodexempel i .NET-applikationen för att lägga till bild i Word-dokument med C#.
Kod för att lägga till bild i Word-dokument med C#
using Aspose.Words; | |
using Aspose.Words.Drawing; | |
namespace HowtoAddImageinWordDocumentUsingCsharp | |
{ | |
class AddImageToWordDOC | |
{ | |
static void Main(string[] args) | |
{ | |
// Set license prior to adding image in Word document using C# | |
License setupPriorAddingImages = new License(); | |
setupPriorAddingImages.SetLicense("path to license.lic"); | |
// Load Word DOC document that you want to add images to | |
Document AddImagesToWordDOC = new Document("input.doc"); | |
// Instantiate DocumentBuilder class object to write text, images, tables etc. | |
DocumentBuilder imageWriter = new DocumentBuilder(AddImagesToWordDOC); | |
// Move cursor to Primary Header in Word DOC | |
imageWriter.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); | |
// Insert image in word document header c# | |
Shape headerImage = imageWriter.InsertImage("C:\\Add Image in Word Header.jpg"); | |
// Set Image Size in Header | |
headerImage.Width = 1 * 72; // equals to one inch | |
headerImage.Height = 1 * 72; | |
// Now, move cursor to last Paragraph in Word Document | |
imageWriter.MoveTo(AddImagesToWordDOC.LastSection.Body.LastParagraph); | |
// Add Image to Word Document and Link to File | |
Shape imageAsLinkToFile = imageWriter.InsertImage("C:\\Add Image as Link to File.jpg"); | |
imageAsLinkToFile.ImageData.SourceFullName = "C:\\Add Image as Link to File.jpg"; | |
// Save As DOCX | |
AddImagesToWordDOC.Save("C:\\Word with Embeded and Linked Images.docx"); | |
} | |
} | |
} |
Så, ovanstående Visual Studio-applikation låter dig lägga till bild till Word-dokument C#. Den laddar en befintlig DOC-fil men du kan till och med programmatiskt skapa word-dokument i C#. Koden presenterar två sätt att lägga till bild till ordet DOC C# - den infogar först bild i word-dokumentets rubrik C# och sedan lägger den till bild till word som länkad bild, dvs bilden i detta fall är inte inbäddad utan infogas som länk till fil.