Práce s obrázky

Aspose.Words umožňuje uživatelům pracovat s obrazy velmi flexibilním způsobem. V tomto článku můžete prozkoumat pouze některé možnosti práce s obrázky.

Jak vložit obrázek

DocumentBuilder poskytuje několik přetížení InsertImage metoda, která umožňuje vložit inline nebo plovoucí obraz. Pokud je obraz EMF nebo WMF metafile, bude vložen do dokumentu ve formátu metafile. Všechny ostatní obrázky budou uloženy ve formátu PNG. • InsertImage metoda může použít obrázky z různých zdrojů:

  • Ze složky nebo URL a String parametr InsertImage
  • Z potoka průchodem Stream parametr InsertImage
  • Z Obrazového objektu zadáním parametru Obrázek InsertImage
  • Z pole byte zadáním parametru byte pole InsertImage

Pro každý z InsertImage metody, existují další přetížení, které vám umožní vložit obrázek s těmito možnostmi:

  • Inline nebo plovoucí na konkrétní pozici, například, InsertImage
  • Procentní nebo vlastní velikost, například, InsertImage; InsertImage metoda vrací a Shape objekt, který byl právě vytvořen a vložen, takže můžete dále upravovat vlastnosti Tvaru

Jak vložit inline obrázek

Pošlete jeden řetězec představující soubor, který obsahuje obrázek do InsertImage vložit obrázek do dokumentu jako inline grafiku

Následující příklad kódu ukazuje, jak vložit inline obrázek na pozici kurzoru do dokumentu:

// 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);

Jak vložit plovoucí obrázek

Následující příklad kódu ukazuje, jak vložit plovoucí obrázek ze souboru nebo URL ve stanovené poloze a velikosti:

// 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);

Jak extrahovat obrázky z dokumentu

Všechny obrázky jsou uloženy uvnitř Shape a Document. Chcete-li získat všechny obrázky nebo obrázky, které mají specifický typ z dokumentu, postupujte podle těchto kroků:

  • Použij GetChildNodes metoda výběru všech Shape uzly.
  • Proniká skrz výsledné kolekce uzlů.
  • Zkontroluj to. HasImage Booleanský majetek.
  • Extrahování obrazových dat pomocí ImageData majetek.
  • Uložit obrazová data do souboru.

Následující příklad kódu ukazuje, jak extrahovat obrázky z dokumentu a uložit je jako soubory:

// 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++;
}
}

Jak vložit čárový kód na každou stránku dokumentu

Tento příklad ukazuje, že můžete přidat stejné nebo různé čárové kódy na všechny nebo konkrétní stránky dokumentu Word. Neexistuje žádný přímý způsob, jak přidat čárových kódů na všech stránkách dokumentu, ale můžete použít MoveToSection, MoveToHeaderFooter a InsertImage metody pro přesun do libovolné sekce nebo hlaviček/noha a vložte obrázky čárového kódu, jak je vidět v následujícím kódu.

Následující příklad kódu ukazuje, jak vložit obrázek čárového kódu na každou stránku dokumentu:

// 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");
}

Zamknout Aspect poměr obrázku

Poměr stran geometrického tvaru je poměr jeho velikostí v různých rozměrech. Můžete zamknout poměr stran obrazu pomocí AspectRatioLocked. Výchozí hodnota poměru stran tvaru závisí na ShapeType. Je. true místo ShapeType.Image a false pro jiné typy tvarů.

Následující příklad kódu ukazuje, jak pracovat s poměrem stran:

// 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);

Jak získat skutečné hranice tvaru v bodech

Pokud chcete skutečný ohraničovací box tvaru vykreslený na stránce, můžete toho dosáhnout pomocí BoundsInPoints majetek.

Následující příklad kódu ukazuje, jak používat tuto vlastnost:

// 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);

Obrázky plodin

Ořezání obrazu se obvykle týká odstranění nežádoucích vnějších částí obrazu, které pomohou zlepšit rámování. Používá se také k odstranění některých částí obrazu pro zvýšení zaměření na určitou oblast.

Následující příklad kódu ukazuje, jak toho dosáhnout pomocí 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));
}

Uložit obrázky jako WMF

Aspose.Words poskytuje funkce pro uložení všech dostupných obrázků v dokumentu do WMFformát při převodu DOCX na RTF.

Následující příklad kódu ukazuje, jak uložit obrázky jako WMF s možností RTF uložení:

// 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);