עבודה עם תמונות
Aspose.Words מאפשר למשתמשים לעבוד עם תמונות בצורה גמישה מאוד. במאמר זה, אתה יכול לחקור רק כמה אפשרויות של עבודה עם תמונות.
כיצד להכניס תמונה
DocumentBuilder מספק מספר עומסים של InsertImage שיטה המאפשרת לך להוסיף תמונה פנימית או צף. אם התמונה היא metafile EMF או WMF, זה יוכנס לתוך המסמך בפורמט metafile. כל התמונות האחרות מאוחסנים בפורמט PNG. The The The InsertImage שיטה יכולה להשתמש בתמונות ממקורות שונים:
- מקובץ או
URL
על ידי עוברString
פרמטר InsertImage - מתוך זרם על ידי עובר
Stream
פרמטר InsertImage - מאובייקט תמונה על ידי העברת פרמטר תמונה InsertImage
- ממערך Byte על ידי העברת פרמטר מערך InsertImage
עבור כל אחד InsertImage שיטות, יש עומסים נוספים המאפשרים לך להוסיף תמונה עם האפשרויות הבאות:
- Inline או צף במיקום מסוים, למשל, InsertImage
- גודל בינוני או גודל מותאם אישית, למשל, InsertImageעוד יותר, InsertImage שיטה מחזירה Shape אובייקט שנוצר והוכנס כדי שתוכל לשנות את התכונות של הצורה
כיצד להכניס תמונה Inline
לעבור מחרוזת אחת המייצגת קובץ המכיל את התמונה InsertImage כדי להכניס את התמונה לתוך המסמך כגרפיקה פנימית
הדוגמה הבאה של הקוד מראה כיצד להכניס תמונה איליין בעמדה cursor לתוך מסמך:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertImage(dataDir + "Watermark.png"); | |
dataDir = dataDir + "DocumentBuilderInsertInlineImage_out.doc"; | |
doc.Save(dataDir); |
כיצד להכניס תמונה Floating
הדוגמה הבאה של הקוד מראה כיצד להוסיף תמונה צף מקובץ או URL
במיקום מסוים בגודל ובגודל:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertImage(dataDir + "Watermark.png", | |
RelativeHorizontalPosition.Margin, | |
100, | |
RelativeVerticalPosition.Margin, | |
100, | |
200, | |
100, | |
WrapType.Square); | |
dataDir = dataDir + "DocumentBuilderInsertFloatingImage_out.doc"; | |
doc.Save(dataDir); |
כיצד להפיק תמונות ממכתב
כל התמונות נשמרות בפנים Shape רעשים ב Document. כדי לחלץ את כל התמונות או התמונות שיש סוג מסוים של המסמך, בצע שלבים אלה:
- השתמש GetChildNodes שיטה לבחור את כל Shape צומת
- עקבו אחרי node אוספים
- בדוק את HasImage רכוש בוז.
- נתוני צילום באמצעות ImageData רכוש.
- שמור את נתוני התמונה לקובץ.
הדוגמה הבאה של הקוד מראה כיצד להפיק תמונות ממסמכים ולהציל אותם כקבצים:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithImages(); | |
Document doc = new Document(dataDir + "Image.SampleImages.doc"); | |
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); | |
int imageIndex = 0; | |
foreach (Shape shape in shapes) | |
{ | |
if (shape.HasImage) | |
{ | |
string imageFileName = string.Format( | |
"Image.ExportImages.{0}_out{1}", imageIndex, FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)); | |
shape.ImageData.Save(dataDir + imageFileName); | |
imageIndex++; | |
} | |
} |
כיצד להכניס Barcode לכל מסמך
דוגמה זו מדגימה אותך להוסיף את אותם ברקודים או שונים על כל העמודים הספציפיים של מסמך Word. אין דרך ישירה להוסיף קודים על כל דפי המסמך, אבל אתה יכול להשתמש MoveToSection, MoveToHeaderFooter ו InsertImage שיטות לעבור לכל חלק או כותרות / מ"ר ולהכניס את התמונות הברקוד כפי שאתה יכול לראות בקוד הבא.
הדוגמה הבאה של הקוד מראה כיצד להוסיף תמונת ברקוד על כל דף של מסמך:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithImages(); | |
// Create a blank documenet. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// The number of pages the document should have. | |
int numPages = 4; | |
// The document starts with one section, insert the barcode into this existing section. | |
InsertBarcodeIntoFooter(builder, doc.FirstSection, 1, HeaderFooterType.FooterPrimary); | |
for (int i = 1; i < numPages; i++) | |
{ | |
// Clone the first section and add it into the end of the document. | |
Section cloneSection = (Section)doc.FirstSection.Clone(false); | |
cloneSection.PageSetup.SectionStart = SectionStart.NewPage; | |
doc.AppendChild(cloneSection); | |
// Insert the barcode and other information into the footer of the section. | |
InsertBarcodeIntoFooter(builder, cloneSection, i, HeaderFooterType.FooterPrimary); | |
} | |
dataDir = dataDir + "Document_out.docx"; | |
// Save the document as a PDF to disk. You can also save this directly to a stream. | |
doc.Save(dataDir); |
Lock Aspect Ratio of Image
היחס ההיבט של צורה גיאומטרי הוא היחס של הגדלים שלו בממדים שונים. אתה יכול לנעול את יחס ההיבט של התמונה באמצעות AspectRatioLocked. ערך ברירת המחדל של יחס ההיבט של הצורה תלוי ShapeType. זה true עבור ShapeType.Image
ו false עבור סוגים אחרים של צורה.
דוגמה לקוד הבא מראה כיצד לעבוד עם יחס היבט:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
var shape = builder.InsertImage(dataDir + "Test.png"); | |
shape.AspectRatioLocked = false; | |
dataDir = dataDir + "Shape_AspectRatioLocked_out.doc"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
כיצד להפוך את פצעי הצורה בנקודות
אם אתה רוצה את הקופסה האמיתית של הצורה כפי שניתן על הדף, אתה יכול להשיג את זה על ידי שימוש BoundsInPoints רכוש.
דוגמה לקוד הבא מראה כיצד להשתמש בנכס זה:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
var shape = builder.InsertImage(dataDir + "Test.png"); | |
shape.AspectRatioLocked = false; | |
Console.Write("\nGets the actual bounds of the shape in points."); | |
Console.WriteLine(shape.GetShapeRenderer().BoundsInPoints); |
תמונות Crop
היבול של תמונה מתייחס בדרך כלל להסרת החלקים החיצוניים הלא רצויים של תמונה כדי לעזור לשפר את ההקפאה. הוא משמש גם להסרת חלק מהחלקים של תמונה כדי להגביר את המיקוד באזור מסוים.
דוגמה לקוד הבא מראה כיצד להשיג זאת באמצעות Aspose.Words API:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithImages(); | |
string inputPath = dataDir + "ch63_Fig0013.jpg"; | |
string outputPath = dataDir + "cropped-1.jpg"; | |
CropImage(inputPath,outputPath, 124, 90, 570, 571); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static void CropImage(string inPath, string outPath, int left, int top,int width, int height) | |
{ | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Image img = Image.FromFile(inPath); | |
int effectiveWidth = img.Width - width; | |
int effectiveHeight = img.Height - height; | |
Shape croppedImage = builder.InsertImage(img, | |
ConvertUtil.PixelToPoint(img.Width - effectiveWidth), | |
ConvertUtil.PixelToPoint(img.Height - effectiveHeight)); | |
double widthRatio = croppedImage.Width / ConvertUtil.PixelToPoint(img.Width); | |
double heightRatio = croppedImage.Height / ConvertUtil.PixelToPoint(img.Height); | |
if (widthRatio< 1) | |
croppedImage.ImageData.CropRight = 1 - widthRatio; | |
if (heightRatio< 1) | |
croppedImage.ImageData.CropBottom = 1 - heightRatio; | |
float leftToWidth = (float)left / img.Width; | |
float topToHeight = (float)top / img.Height; | |
croppedImage.ImageData.CropLeft = leftToWidth; | |
croppedImage.ImageData.CropRight = croppedImage.ImageData.CropRight - leftToWidth; | |
croppedImage.ImageData.CropTop = topToHeight; | |
croppedImage.ImageData.CropBottom = croppedImage.ImageData.CropBottom - topToHeight; | |
croppedImage.GetShapeRenderer().Save(outPath, new ImageSaveOptions(SaveFormat.Jpeg)); | |
} |
שמור תמונות כמו WMF
Aspose.Words מספק פונקציונליות כדי לשמור את כל התמונות הזמינות במסמך WMFפורמט תוך המרת DOCX ל- RTF.
הדוגמה הבאה של הקוד מראה כיצד לחסוך תמונות כמו WMF עם RTF אפשרויות:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
string fileName = "TestFile.doc"; | |
Document doc = new Document(dataDir + fileName); | |
RtfSaveOptions saveOpts = new RtfSaveOptions(); | |
saveOpts.SaveImagesAsWmf = true; | |
doc.Save(dataDir + "output.rtf", saveOpts); |