کار با تصاویر

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

نحوه استخراج تصاویر از یک سند

همه تصاویر در داخل گره های Shape در یک سند ذخیره می شوند. برای استخراج تمام تصاویر یا تصاویر با نوع خاصی از سند، این مراحل را دنبال کنید:

  • از روش getChildNodes برای انتخاب تمام گره های شکل استفاده کنید.
  • از طریق مجموعه گره های حاصل تکرار کنید.
  • ویژگی boolean hasImage را بررسی کنید.
  • استخراج اطلاعات تصویر با استفاده از خاصیت ImageData.
  • داده های تصویر را در یک فایل ذخیره کنید.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ExtractImagesToFiles.class);
Document doc = new Document(dataDir + "Image.SampleImages.doc");
NodeCollection<Shape> shapes = (NodeCollection<Shape>) doc.getChildNodes(NodeType.SHAPE, true);
int imageIndex = 0;
for (Shape shape : shapes
) {
if (shape.hasImage()) {
String imageFileName = String.format(
"Image.ExportImages.{0}_out_{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
shape.getImageData().save(dataDir + imageFileName);
imageIndex++;
}
}

چگونه بارکد را در هر صفحه سند وارد کنیم

این مثال به شما اجازه می دهد بارکد های یکسان یا متفاوت را در تمام یا صفحات خاص یک سند ورد اضافه کنید. هیچ راهی برای اضافه کردن بارکد در تمام صفحات یک سند وجود ندارد اما شما می توانید از روش های moveToSection، moveToHeaderFooter و insertImage برای حرکت به هر بخش یا سرصفحه/پای صفحه استفاده کنید و تصاویر بارکد را همانطور که در کد زیر می بینید وارد کنید.

مثال کد زیر نشان می دهد که چگونه یک تصویر بارکد را در هر صفحه از یک سند وارد کنید:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(InsertBarcodeImage.class);
Document doc = new Document(dataDir + "Image.SampleImages.doc");
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.getFirstSection(), 1, HeaderFooterType.FOOTER_PRIMARY);
InsertBarcodeIntoFooter(builder, doc.getFirstSection(), 1, HeaderFooterType.FOOTER_PRIMARY);
for (int i = 1; i < numPages; i++) {
// Clone the first section and add it into the end of the document.
Section cloneSection = (Section) doc.getFirstSection().deepClone(false);
// cloneSection.getPageSetup().getSectionStart() = SectionStart.NEW_PAGE;
doc.appendChild(cloneSection);
// Insert the barcode and other information into the footer of the section.
InsertBarcodeIntoFooter(builder, cloneSection, i, HeaderFooterType.FOOTER_PRIMARY);
}
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-Java
private static void InsertBarcodeIntoFooter(DocumentBuilder builder, Section section, int pageId, int footerType) {
// Move to the footer type in the specific section.
try {
builder.moveToSection(section.getDocument().indexOf(section));
builder.moveToHeaderFooter(footerType);
String dataDir = Utils.getDataDir(InsertBarcodeImage.class);
// 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(dataDir + "Barcode1.png");
builder.writeln();
builder.write("1234567890");
builder.insertField("PAGE");
// Create a right aligned tab at the right margin.
double tabPos = section.getPageSetup().getPageWidth() - section.getPageSetup().getRightMargin() - section.getPageSetup().getLeftMargin();
builder.getCurrentParagraph().getParagraphFormat().getTabStops().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");
} catch (Exception x) {
}
}

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

نسبت ابعاد یک شکل هندسی نسبت اندازه های آن در ابعاد مختلف است. شما می توانید نسبت ابعاد یک تصویر را با استفاده از AspectRatioLocked قفل کنید. مقدار پیش فرض نسبت ابعاد شکل به ShapeType بستگی دارد. این برای ShapeType.Image درست است و برای سایر انواع شکل غلط است.

مثال کد زیر نشان می دهد که چگونه با نسبت ابعاد کار کنیم:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(DocumentBuilderSetImageAspectRatioLocked.class);
// Open the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(dataDir + "Test.png");
shape.setAspectRatioLocked(false);
doc.save(dataDir + "output.doc");

چگونه مرزهای واقعی شکل را در نقاط بدست آوریم

اگر می خواهید جعبه محدود کننده واقعی شکل را همانطور که در صفحه ارائه شده است، می توانید با استفاده از ویژگی BoundsInPoints به این هدف برسید.

مثال کد زیر نشان می دهد که چگونه از این ویژگی استفاده کنید:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(dataDir + "Test.png");
shape.setAspectRatioLocked(false);
System.out.print("\nGets the actual bounds of the shape in points. ");
System.out.println(shape.getShapeRenderer().getBoundsInPoints());

تصاویر محصول

برش یک تصویر معمولا به حذف قسمت های بیرونی ناخواسته یک تصویر برای کمک به بهبود قاب اشاره دارد. همچنین برای حذف برخی از بخش های یک تصویر برای افزایش تمرکز بر یک منطقه خاص استفاده می شود.

مثال کد زیر نشان می دهد که چگونه با استفاده از Aspose.Words APIبه این هدف برسیم:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(CropImages.class);
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-Java
public static void cropImage(String inPath, String outPath, int left, int top, int width, int height) throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
BufferedImage img = ImageIO.read(new File(inPath));
int effectiveWidth = img.getWidth() - width;
int effectiveHeight = img.getHeight() - height;
Shape croppedImage = builder.insertImage(img,
ConvertUtil.pixelToPoint(img.getWidth() - effectiveWidth),
ConvertUtil.pixelToPoint(img.getHeight() - effectiveHeight));
double widthRatio = croppedImage.getWidth() / ConvertUtil.pixelToPoint(img.getWidth());
double heightRatio = croppedImage.getHeight() / ConvertUtil.pixelToPoint(img.getHeight());
if (widthRatio < 1)
croppedImage.getImageData().setCropRight(1 - widthRatio);
if (heightRatio < 1)
croppedImage.getImageData().setCropBottom(1 - heightRatio);
float leftToWidth = (float) left / img.getWidth();
float topToHeight = (float) top / img.getHeight();
croppedImage.getImageData().setCropLeft(leftToWidth);
croppedImage.getImageData().setCropRight(croppedImage.getImageData().getCropRight() - leftToWidth);
croppedImage.getImageData().setCropTop(topToHeight);
croppedImage.getImageData().setCropBottom(croppedImage.getImageData().getCropBottom() - 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-Java
String fileName = "TestFile.doc";
Document doc = new Document(dataDir + fileName);
RtfSaveOptions saveOpts = new RtfSaveOptions();
saveOpts.setSaveImagesAsWmf(true);
doc.save(dataDir + "output.rtf", saveOpts);