Skip to content

Commit

Permalink
Updated API examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vderyushev committed Nov 7, 2024
1 parent 0e4a2ab commit 89c45e2
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 214 deletions.
5 changes: 3 additions & 2 deletions Examples/ApiExamples/ApiExamples/ApiExamples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,15 @@
<Content Include="ExWordML2003SaveOptions.cs" />
<Content Include="ExXlsxSaveOptions.cs" />
<Content Include="ExLowCode.cs" />
<Content Include="ExAI.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aspose.BarCode" Version="24.9.0" />
<PackageReference Include="Aspose.Page" Version="24.9.0" />
<PackageReference Include="Aspose.PDF" Version="24.10.0" />
<PackageReference Include="Aspose.Words" Version="24.10.0" />
<PackageReference Include="Aspose.Words.Shaping.HarfBuzz" Version="24.10.0" />
<PackageReference Include="Aspose.Words" Version="24.11.0" />
<PackageReference Include="Aspose.Words.Shaping.HarfBuzz" Version="24.11.0" />
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
Expand Down
52 changes: 52 additions & 0 deletions Examples/ApiExamples/ApiExamples/ExAI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2001-2024 Aspose Pty Ltd. All Rights Reserved.
//
// This file is part of Aspose.Words. The source code in this file
// is only intended as a supplement to the documentation, and is provided
// "as is", without warranty of any kind, either expressed or implied.
//////////////////////////////////////////////////////////////////////////

using System.Text;
using NUnit.Framework;
using Aspose.Words;
using System;
using Aspose.Words.AI;

namespace ApiExamples
{
[TestFixture]
public class ExAI : ApiExampleBase
{
[Test, Explicit("This test should be run manually to manage API requests amount")]
public void AiSummarize()
{
//ExStart:AiSummarize
//ReleaseVersion:24.11
//ExFor:GoogleAiModel
//ExFor:OpenAiModel
//ExFor:IAiModelText
//ExFor:IAiModelText.Summarize(Document, SummarizeOptions)
//ExFor:IAiModelText.Summarize(Document[], SummarizeOptions)
//ExFor:SummarizeOptions
//ExFor:SummarizeOptions.SummaryLength
//ExFor:SummaryLength
//ExFor:AiModel
//ExFor:AiModel.Create(AiModelType)
//ExFor:AiModel.WithApiKey(String)
//ExFor:AiModelType
//ExSummary:Shows how to summarize text using OpenAI and Google models.
Document firstDoc = new Document(MyDir + "Big document.docx");
Document secondDoc = new Document(MyDir + "Document.docx");

string apiKey = Environment.GetEnvironmentVariable("API_KEY");
// Use OpenAI or Google generative language models.
IAiModelText model = (IAiModelText)AiModel.Create(AiModelType.Gpt4OMini).WithApiKey(apiKey);

Document oneDocumentSummary = model.Summarize(firstDoc, new SummarizeOptions() { SummaryLength = SummaryLength.Short });
oneDocumentSummary.Save(ArtifactsDir + "AI.AiSummarize.One.docx");

Document multiDocumentSummary = model.Summarize(new Document[] { firstDoc, secondDoc }, new SummarizeOptions() { SummaryLength = SummaryLength.Long });
multiDocumentSummary.Save(ArtifactsDir + "AI.AiSummarize.Multi.docx");
//ExEnd:AiSummarize
}
}
}
52 changes: 52 additions & 0 deletions Examples/ApiExamples/ApiExamples/ExCharts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2298,5 +2298,57 @@ public void PieOfPieChart()
doc.Save(ArtifactsDir + "Charts.PieOfPieChart.docx");
//ExEnd:PieOfPieChart
}

[Test]
public void FormatCode()
{
//ExStart:FormatCode
//ReleaseVersion:24.11
//ExFor:ChartXValueCollection.FormatCode
//ExFor:ChartYValueCollection.FormatCode
//ExFor:BubbleSizeCollection.FormatCode
//ExSummary:Shows how to work with the format code of the chart data.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a Bubble chart.
Shape shape = builder.InsertChart(ChartType.Bubble, 432, 252);
Chart chart = shape.Chart;

// Delete default generated series.
chart.Series.Clear();

ChartSeries series = chart.Series.Add(
"Series1",
new double[] { 1, 1.9, 2.45, 3 },
new double[] { 1, -0.9, 1.82, 0 },
new double[] { 2, 1.1, 2.95, 2 });

// Show data labels.
series.HasDataLabels = true;
series.DataLabels.ShowCategoryName = true;
series.DataLabels.ShowValue = true;
series.DataLabels.ShowBubbleSize = true;

// Set data format codes.
series.XValues.FormatCode = "#,##0.0#";
series.YValues.FormatCode = "#,##0.0#;[Red]\\-#,##0.0#";
series.BubbleSizes.FormatCode = "#,##0.0#";

doc.Save(ArtifactsDir + "Charts.FormatCode.docx");
//ExEnd:FormatCode

doc = new Document(ArtifactsDir + "Charts.FormatCode.docx");
shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
chart = shape.Chart;

ChartSeriesCollection seriesCollection = chart.Series;
foreach (ChartSeries seriesProperties in seriesCollection)
{
Assert.AreEqual("#,##0.0#", seriesProperties.XValues.FormatCode);
Assert.AreEqual("#,##0.0#;[Red]\\-#,##0.0#", seriesProperties.YValues.FormatCode);
Assert.AreEqual("#,##0.0#", seriesProperties.BubbleSizes.FormatCode);
}
}
}
}
159 changes: 0 additions & 159 deletions Examples/ApiExamples/ApiExamples/ExDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
using Aspose.Words.WebExtensions;
using NUnit.Framework;
using MemoryFontSource = Aspose.Words.Fonts.MemoryFontSource;
using Aspose.Words.Pdf2Word.FixedFormats;
using Aspose.Page.XPS;
using LoadOptions = Aspose.Words.Loading.LoadOptions;
using Aspose.Page.XPS.XpsModel;
using Aspose.Words.Settings;
using Aspose.Pdf.Text;
using Aspose.Words.Shaping.HarfBuzz;
Expand Down Expand Up @@ -267,162 +264,6 @@ public void OpenProtectedPdfDocument()
doc = new Document(ArtifactsDir + "Document.PdfDocumentEncrypted.pdf", loadOptions);
}

[TestCase("Protected pdf document.pdf", "PDF")]
[TestCase("Pdf Document.pdf", "HTML")]
[TestCase("Pdf Document.pdf", "XPS")]
[TestCase("Images.pdf", "JPEG")]
[TestCase("Images.pdf", "PNG")]
[TestCase("Images.pdf", "TIFF")]
[TestCase("Images.pdf", "BMP")]
public void PdfRenderer(string docName, string format)
{
var pdfRenderer = new PdfFixedRenderer();
var options = new PdfFixedOptions();


switch (format)
{
case "PDF":
options = new PdfFixedOptions() { Password = "{Asp0se}P@ssw0rd" };
SaveTo(pdfRenderer, docName, options, "pdf");
AssertResult("pdf");

break;

case "HTML":
options = new PdfFixedOptions() { PageIndex = 0, PageCount = 1 };
SaveTo(pdfRenderer, docName, options, "html");
AssertResult("html");

break;

case "XPS":
SaveTo(pdfRenderer, docName, options, "xps");
AssertResult("xps");

break;

case "JPEG":
options = new PdfFixedOptions() { JpegQuality = 10, ImageFormat = FixedImageFormat.Jpeg };
SaveTo(pdfRenderer, docName, options, "jpeg");
AssertResult("jpeg");

break;

case "PNG":
options = new PdfFixedOptions()
{
PageIndex = 0,
PageCount = 2,
JpegQuality = 50,
ImageFormat = FixedImageFormat.Png
};
SaveTo(pdfRenderer, docName, options, "png");
AssertResult("png");

break;

case "TIFF":
options = new PdfFixedOptions() { JpegQuality = 100, ImageFormat = FixedImageFormat.Tiff };
SaveTo(pdfRenderer, docName, options, "tiff");
AssertResult("tiff");

break;

case "BMP":
options = new PdfFixedOptions() { ImageFormat = FixedImageFormat.Bmp };
SaveTo(pdfRenderer, docName, options, "bmp");
AssertResult("bmp");

break;
}
}

private void SaveTo(PdfFixedRenderer pdfRenderer, string docName, PdfFixedOptions fixedOptions, string fileExt)
{
using (var pdfDoc = File.OpenRead(MyDir + docName))
{
Stream stream = new MemoryStream();
IReadOnlyList<Stream> imagesStream = new List<Stream>();

if (fileExt == "pdf")
{
stream = pdfRenderer.SavePdfAsPdf(pdfDoc, fixedOptions);
}
else if (fileExt == "html")
{
stream = pdfRenderer.SavePdfAsHtml(pdfDoc, fixedOptions);
}
else if (fileExt == "xps")
{
stream = pdfRenderer.SavePdfAsXps(pdfDoc, fixedOptions);
}
else if (fileExt == "jpeg" || fileExt == "png" || fileExt == "tiff" || fileExt == "bmp")
{
imagesStream = pdfRenderer.SavePdfAsImages(pdfDoc, fixedOptions);
}

if (imagesStream.Count != 0)
{
for (int i = 0; i < imagesStream.Count; i++)
{
using (FileStream resultDoc = new FileStream(ArtifactsDir + $"PdfRenderer_{i}.{fileExt}", FileMode.Create))
imagesStream[i].CopyTo(resultDoc);
}
}
else
{
using (FileStream resultDoc = new FileStream(ArtifactsDir + $"PdfRenderer.{fileExt}", FileMode.Create))
stream.CopyTo(resultDoc);
}
}
}

private void AssertResult(string fileExt)
{
if (fileExt == "jpeg" || fileExt == "png" || fileExt == "tiff" || fileExt == "bmp")
{
Regex reg = new Regex("PdfRenderer_*");

var images = Directory.GetFiles(ArtifactsDir, $"*.{fileExt}")
.Where(path => reg.IsMatch(path))
.ToList();

if (fileExt == "png")
Assert.AreEqual(2, images.Count);
else
Assert.AreEqual(5, images.Count);
}
else
{
if (fileExt == "xps")
{
var doc = new XpsDocument(ArtifactsDir + $"PdfRenderer.{fileExt}");
AssertXpsText(doc);
}
else
{
var doc = new Document(ArtifactsDir + $"PdfRenderer.{fileExt}");
var content = doc.GetText().Replace("\r", " ");

Assert.True(content.Contains("Heading 1 Heading 1.1.1.1 Heading 1.1.1.2"));
}
}
}

private static void AssertXpsText(XpsDocument doc)
{
AssertXpsText(doc.SelectActivePage(1));
}

private static void AssertXpsText(XpsElement element)
{
for (int i = 0; i < element.Count; i++)
AssertXpsText(element[i]);
if (element is XpsGlyphs)
Assert.True(new[] { "Heading 1", "Head", "ing 1" }.Any(c => ((XpsGlyphs)element).UnicodeString.Contains(c)));
}

[Test]
public void OpenFromStreamWithBaseUri()
{
Expand Down
18 changes: 17 additions & 1 deletion Examples/ApiExamples/ApiExamples/ExDocumentProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void BuiltIn()
}
//ExEnd

Assert.AreEqual(28, doc.BuiltInDocumentProperties.Count);
Assert.AreEqual(31, doc.BuiltInDocumentProperties.Count);
}

[Test]
Expand Down Expand Up @@ -672,5 +672,21 @@ public void PropertyTypes()
Assert.AreEqual(123.45d, properties["Authorized Amount"].ToDouble());
//ExEnd
}

[Test]
public void ExtendedProperties()
{
//ExStart:ExtendedProperties
//ReleaseVersion:24.11
//ExFor:BuiltInDocumentProperties.ScaleCrop
//ExFor:BuiltInDocumentProperties.SharedDocument
//ExFor:BuiltInDocumentProperties.HyperlinksChanged
//ExSummary:Shows how to get extended properties.
Document doc = new Document(MyDir + "Extended properties.docx");
Assert.IsTrue(doc.BuiltInDocumentProperties.ScaleCrop);
Assert.IsTrue(doc.BuiltInDocumentProperties.SharedDocument);
Assert.IsTrue(doc.BuiltInDocumentProperties.HyperlinksChanged);
//ExEnd:ExtendedProperties
}
}
}
1 change: 0 additions & 1 deletion Examples/ApiExamples/ApiExamples/ExFieldOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;
using Aspose.Drawing;
using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Fields;
Expand Down
7 changes: 4 additions & 3 deletions Examples/ApiExamples/ApiExamples/ExHyphenation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ public void Dictionary()
[Test]
public void UsePdfDocumentForDictionary()
{
const string unicodeOptionalHyphen = "\xad";

Dictionary();

Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(ArtifactsDir + "Hyphenation.Dictionary.Registered.pdf");
TextAbsorber textAbsorber = new TextAbsorber();
textAbsorber.Visit(pdfDoc);

Assert.True(textAbsorber.Text.Replace(" ", " ").Contains($"La ob storen an deinen am sachen. Dop-{Environment.NewLine}" +
$"pelte um da am spateren verlogen ge-{Environment.NewLine}" +
Assert.True(textAbsorber.Text.Replace(" ", " ").Contains($"La ob storen an deinen am sachen. Dop{unicodeOptionalHyphen}{Environment.NewLine}" +
$"pelte um da am spateren verlogen ge{unicodeOptionalHyphen}{Environment.NewLine}" +
$"kommen achtzehn blaulich."));

pdfDoc = new Aspose.Pdf.Document(ArtifactsDir + "Hyphenation.Dictionary.Unregistered.pdf");
Expand Down
Loading

0 comments on commit 89c45e2

Please sign in to comment.