العمل مع الصور
يتيح Aspose.Words للمستخدمين العمل مع الصور بطريقة مرنة للغاية. في هذه المقالة، يمكنك استكشاف بعض إمكانيات العمل مع الصور فقط.
كيفية إدراج صورة
يوفر DocumentBuilder العديد من التحميلات الزائدة لطريقة InsertImage التي تسمح لك بإدراج صورة مضمّنة أو عائمة. إذا كانت الصورة عبارة عن ملف تعريف EMF أو WMF، فسيتم إدراجها في المستند بتنسيق ملف تعريف. سيتم تخزين جميع الصور الأخرى بتنسيق PNG. يمكن لطريقة InsertImage استخدام صور من مصادر مختلفة:
- من ملف أو
URL
عن طريق تمرير معلمةString
InsertImage - من الدفق عن طريق تمرير معلمة
Stream
InsertImage - من كائن صورة عن طريق تمرير معلمة الصورة InsertImage
- من صفيف بايت عن طريق تمرير معلمة صفيف بايت InsertImage
لكل طريقة من طرق InsertImage، هناك المزيد من التحميلات الزائدة التي تسمح لك بإدراج صورة بالخيارات التالية:
- مضمنة أو عائمة في موضع محدد، على سبيل المثال، InsertImage
- مقياس النسبة المئوية أو الحجم المخصص، على سبيل المثال، InsertImage؛ علاوة على ذلك، تقوم طريقة InsertImage بإرجاع كائن Shape الذي تم إنشاؤه وإدراجه للتو حتى تتمكن من تعديل خصائص الشكل بشكل أكبر
كيفية إدراج صورة مضمنة
قم بتمرير سلسلة واحدة تمثل ملفًا يحتوي على الصورة إلى InsertImage لإدراج الصورة في المستند كرسم مضمّن
يوضح مثال التعليمات البرمجية التالي كيفية إدراج صورة سطرية في موضع المؤشر في مستند:
// 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); |
كيفية إدراج صورة عائمة
يوضح مثال التعليمات البرمجية التالي كيفية إدراج صورة عائمة من ملف أو 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.
- التكرار من خلال مجموعات العقدة الناتجة.
- التحقق من خاصية 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++; | |
} | |
} |
كيفية إدراج الباركود في كل صفحة مستند
يوضح لك هذا المثال كيفية إضافة نفس الرموز الشريطية أو رموز مختلفة على كل الصفحات أو صفحات معينة من مستند 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); |
قفل نسبة العرض إلى الارتفاع للصورة
نسبة العرض إلى الارتفاع للشكل الهندسي هي نسبة أحجامه في أبعاد مختلفة. يمكنك قفل نسبة العرض إلى الارتفاع للصورة باستخدام 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); |
قص الصور
يشير اقتصاص الصورة عادةً إلى إزالة الأجزاء الخارجية غير المرغوب فيها من الصورة للمساعدة في تحسين الإطار. يتم استخدامه أيضًا لإزالة بعض أجزاء الصورة لزيادة التركيز على منطقة معينة.
يوضح مثال التعليمات البرمجية التالي كيفية تحقيق ذلك باستخدام 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 وظيفة لحفظ جميع الصور المتوفرة في مستند إلى تنسيق ومف أثناء تحويل 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); |