کار با تصاویر

Aspose.Words به کاربران اجازه می دهد تا با تصاویر به روشی بسیار انعطاف پذیر کار کنند. در این مقاله می توانید تنها برخی از امکانات کار با تصاویر را بررسی کنید.

نحوه درج یک تصویر

DocumentBuilder چندین بار اضافه بار از روش InsertImage را ارائه می دهد که به شما امکان می دهد یک تصویر درون خطی یا شناور را وارد کنید. اگر تصویر یک متافیل EMF یا WMF باشد، در قالب متافایل در سند درج می شود. تمام تصاویر دیگر در فرمت PNG ذخیره خواهند شد. روش InsertImage می تواند از تصاویر منابع مختلف استفاده کند:

  • از یک فایل یا URL با عبور یک پارامتر String InsertImage
  • از یک جریان با عبور پارامتر Stream InsertImage
  • از یک شی Image با ارسال پارامتر Image InsertImage
  • از یک آرایه بایت با ارسال پارامتر آرایه بایت InsertImage

برای هر یک از روش های InsertImage، اضافه بارهای بیشتری وجود دارد که به شما امکان می دهد تصویری را با گزینه های زیر وارد کنید:

  • درون خطی یا شناور در یک موقعیت خاص، به عنوان مثال، InsertImage
  • مقیاس درصد یا اندازه سفارشی، به عنوان مثال، InsertImage. علاوه بر این، روش InsertImage یک شی Shape را که به تازگی ایجاد و درج شده است برمی گرداند تا بتوانید ویژگی های 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);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
private static void InsertBarcodeIntoFooter(DocumentBuilder builder, Section section, int pageId, HeaderFooterType footerType)
{
// Move to the footer type in the specific section.
builder.MoveToSection(section.Document.IndexOf(section));
builder.MoveToHeaderFooter(footerType);
// Insert the barcode, then move to the next line and insert the ID along with the page number.
// Use pageId if you need to insert a different barcode on each page. 0 = First page, 1 = Second page etc.
builder.InsertImage(System.Drawing.Image.FromFile( RunExamples.GetDataDir_WorkingWithImages() + "Barcode1.png"));
builder.Writeln();
builder.Write("1234567890");
builder.InsertField("PAGE");
// Create a right aligned tab at the right margin.
double tabPos = section.PageSetup.PageWidth - section.PageSetup.RightMargin - section.PageSetup.LeftMargin;
builder.CurrentParagraph.ParagraphFormat.TabStops.Add(new TabStop(tabPos, TabAlignment.Right, TabLeader.None));
// Move to the right hand side of the page and insert the page and page total.
builder.Write(ControlChar.Tab);
builder.InsertField("PAGE");
builder.Write(" of ");
builder.InsertField("NUMPAGES");
}

قفل نسبت ابعاد تصویر

نسبت ابعاد یک شکل هندسی، نسبت اندازه های آن در ابعاد مختلف است. با استفاده از 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 {#save-images-as-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);