Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.Words מאפשר למשתמשים לעבוד עם תמונות בצורה גמישה מאוד. במאמר זה, אתה יכול לחקור רק כמה אפשרויות של עבודה עם תמונות.
DocumentBuilder מספק מספר עומסים של insert_image שיטה המאפשרת לך להוסיף תמונה פנימית או צף. אם התמונה היא metafile EMF או WMF, זה יוכנס לתוך המסמך בפורמט metafile. כל התמונות האחרות מאוחסנים בפורמט PNG. The The The המונחים: שיטה יכולה להשתמש בתמונות ממקורות שונים:
URL עובר פרמטר מיתרStream פרמטרעבור כל אחד המונחים: שיטות, יש עומסים נוספים המאפשרים לך להוסיף תמונה עם האפשרויות הבאות:
לעבור מחרוזת אחת המייצגת קובץ המכיל את התמונה insert_image כדי להכניס את התמונה לתוך המסמך כגרפיקה פנימית.
הדוגמה הבאה של הקוד מראה כיצד להכניס תמונה איליין בעמדה cursor לתוך מסמך:
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.insert_image(docs_base.images_dir + "Logo.jpg")
doc.save(docs_base.artifacts_dir+"WorkingWithImages.document_builder_insert_inline_image.doc")הדוגמה הבאה של הקוד מראה כיצד להוסיף תמונה צף מקובץ או URL במיקום מסוים בגודל ובגודל:
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.insert_image(docs_base.images_dir + "Logo.jpg",
aw.drawing.RelativeHorizontalPosition.MARGIN,
100,
aw.drawing.RelativeVerticalPosition.MARGIN,
100,
200,
100,
aw.drawing.WrapType.SQUARE)
doc.save(docs_base.artifacts_dir+"WorkingWithImages.document_builder_insert_floating_image.doc")כל התמונות נשמרות בפנים Shape רעשים ב Document. כדי לחלץ את כל התמונות או התמונות שיש סוג מסוים של המסמך, בצע שלבים אלה:
הדוגמה הבאה של הקוד מראה כיצד להפיק תמונות ממסמכים ולהציל אותם כקבצים:
ניתן להוריד את קובץ התבנית של דוגמה זו כאן.
דוגמה זו מדגימה אותך להוסיף את אותם ברקודים או שונים על כל העמודים הספציפיים של מסמך Word. אין דרך ישירה להוסיף קודים על כל דפי המסמך, אבל אתה יכול להשתמש move_to_section, move_to_header_footer ו insert_image שיטות לעבור לכל חלק או כותרות / מ"ר ולהכניס את התמונות הברקוד כפי שאתה יכול לראות בקוד הבא.
הדוגמה הקודית הבאה מציגה תמונת ברקוד על כל דף של מסמך.
היחס ההיבט של צורה גיאומטרי הוא היחס של הגדלים שלו בממדים שונים. אתה יכול לנעול את יחס ההיבט של התמונה באמצעות aspect_ratio_locked. ערך ברירת המחדל של יחס ההיבט של הצורה תלוי ShapeType. זה True עבור ShapeType.IMAGE ו False עבור סוגים אחרים של צורה.
דוגמה לקוד הבא מראה כיצד לעבוד עם יחס היבט:
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.insert_image(docs_base.images_dir + "Logo.jpg")
shape.aspect_ratio_locked = False
doc.save(docs_base.artifacts_dir+"WorkingWithImages.set_aspect_ratio_locked.doc")אם אתה רוצה את הקופסה האמיתית של הצורה כפי שניתן על הדף, אתה יכול להשיג את זה על ידי שימוש bounds_in_points רכוש.
דוגמה לקוד הבא מראה כיצד להשתמש בנכס זה:
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.insert_image(docs_base.images_dir + "Logo.jpg")
shape.aspect_ratio_locked = False
print("\nGets the actual bounds of the shape in points.")
rect = shape.get_shape_renderer().bounds_in_points
print(f"{rect.x}, {rect.y}, {rect.width}, {rect.height}")היבול של תמונה מתייחס בדרך כלל להסרת החלקים החיצוניים הלא רצויים של תמונה כדי לעזור לשפר את ההקפאה. הוא משמש גם להסרת חלק מהחלקים של תמונה כדי להגביר את המיקוד באזור מסוים.
דוגמה לקוד הבא מראה כיצד להשיג זאת באמצעות Aspose.Words API:
# The path to the documents directory.
inputPath = docs_base.images_dir + "Logo.jpg"
outputPath = docs_base.artifacts_dir + "cropped_logo.jpg"
self.crop_image(inputPath,outputPath, 100, 90, 200, 200)@staticmethod
def crop_image(inPath : str, outPath : str, left : int, top : int, width : int, height : int) :
doc = aw.Document();
builder = aw.DocumentBuilder(doc)
croppedImage = builder.insert_image(inPath)
src_width_points = croppedImage.width
src_height_points = croppedImage.height
croppedImage.width = aw.ConvertUtil.pixel_to_point(width)
croppedImage.height = aw.ConvertUtil.pixel_to_point(height)
widthRatio = croppedImage.width / src_width_points
heightRatio = croppedImage.height / src_height_points
if (widthRatio< 1) :
croppedImage.image_data.crop_right = 1 - widthRatio
if (heightRatio< 1) :
croppedImage.image_data.crop_bottom = 1 - heightRatio
leftToWidth = aw.ConvertUtil.pixel_to_point(left) / src_width_points
topToHeight = aw.ConvertUtil.pixel_to_point(top) / src_height_points
croppedImage.image_data.crop_left = leftToWidth
croppedImage.image_data.crop_right = croppedImage.image_data.crop_right - leftToWidth
croppedImage.image_data.crop_top = topToHeight
croppedImage.image_data.crop_bottom = croppedImage.image_data.crop_bottom - topToHeight
croppedImage.get_shape_renderer().save(outPath, aw.saving.ImageSaveOptions(aw.SaveFormat.JPEG))Aspose.Words מספק פונקציונליות כדי לשמור את כל התמונות הזמינות במסמך WMF פורמט תוך המרת DOCX ל- RTF.
הדוגמה הבאה של הקוד מראה כיצד לחסוך תמונות כמו WMF עם RTF אפשרויות:
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.