Skip to content

Commit

Permalink
Updated api examples of correct porting to Python.
Browse files Browse the repository at this point in the history
  • Loading branch information
KotovDA committed Dec 12, 2024
1 parent 3c471ed commit 8c65f24
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 44 deletions.
18 changes: 11 additions & 7 deletions Examples/ApiExamples/ApiExamples/ExAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,32 @@ public void AiSummarize()

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

Document oneDocumentSummary = model.Summarize(firstDoc, new SummarizeOptions() { SummaryLength = SummaryLength.Short });
SummarizeOptions options = new SummarizeOptions();

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

Document multiDocumentSummary = model.Summarize(new Document[] { firstDoc, secondDoc }, new SummarizeOptions() { SummaryLength = SummaryLength.Long });
options.SummaryLength = SummaryLength.Long;
Document multiDocumentSummary = model.Summarize(new Document[] { firstDoc, secondDoc }, options);
multiDocumentSummary.Save(ArtifactsDir + "AI.AiSummarize.Multi.docx");
//ExEnd:AiSummarize
}

[Test, Ignore("This test should be run manually to manage API requests amount")]
public void AiTranslate()
{
{
//ExStart:AiTranslate
//GistId:695136dbbe4f541a8a0a17b3d3468689
//ExFor:IAiModelText.Translate(Document, AI.Language)
//ExSummary:Shows how to translate text using Google models.
//ExFor:IAiModelText.Translate(Document, AI.Language)
//ExSummary:Shows how to translate text using Google models.
Document doc = new Document(MyDir + "Document.docx");

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

Document translatedDoc = model.Translate(doc, Language.Arabic);
translatedDoc.Save(ArtifactsDir + "AI.AiTranslate.docx");
Expand Down
25 changes: 18 additions & 7 deletions Examples/ApiExamples/ApiExamples/ExCharts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2422,11 +2422,12 @@ public void DoughnutChartLabelPosition()
double totalValue = 0;
string[] categories = new string[dataLength];
double[] values = new double[dataLength];

for (int i = 0; i < dataLength; i++)
{
categories[i] = string.Format("Category {0}", i);
categories[i] = $"Category {i}";
values[i] = dataLength - i;
totalValue += values[i];
totalValue = totalValue + values[i];
}

ChartSeries series = seriesColl.Add("Series 1", categories, values);
Expand Down Expand Up @@ -2459,8 +2460,12 @@ public void DoughnutChartLabelPosition()
{
ChartDataLabel dataLabel = dataLabels[i];

double value = series.YValues[i].DoubleValue;
double labelWidth = (value < 10) ? oneCharLabelWidth : twoCharLabelWidth;
double value = series.YValues[i].DoubleValue;
double labelWidth;
if (value < 10)
labelWidth = oneCharLabelWidth;
else
labelWidth = twoCharLabelWidth;
double labelSegmentAngle = value / totalValue * 2 * System.Math.PI;
double labelAngle = labelSegmentAngle / 2 + totalAngle;
double labelCenterX = labelCircleRadius * System.Math.Cos(labelAngle) + doughnutCenterX;
Expand All @@ -2474,16 +2479,22 @@ public void DoughnutChartLabelPosition()
(System.Math.Abs(previousLabel.Left - labelLeft) < labelWidth))
{
// Move right on the top, left on the bottom.
bool isOnTop = (totalAngle < 0) || (totalAngle >= System.Math.PI);
labelLeft = previousLabel.Left + labelWidth * (isOnTop ? 1 : -1) + labelMargin;
bool isOnTop = (totalAngle < 0) || (totalAngle >= System.Math.PI);
int factor;
if (isOnTop)
factor = 1;
else
factor = -1;

labelLeft = previousLabel.Left + labelWidth * factor + labelMargin;
}

dataLabel.Left = labelLeft;
dataLabel.LeftMode = ChartDataLabelLocationMode.Absolute;
dataLabel.Top = labelTop;
dataLabel.TopMode = ChartDataLabelLocationMode.Absolute;

totalAngle += labelSegmentAngle;
totalAngle = totalAngle + labelSegmentAngle;
previousLabel = dataLabel;
}

Expand Down
92 changes: 62 additions & 30 deletions Examples/ApiExamples/ApiExamples/ExLowCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// "as is", without warranty of any kind, either expressed or implied.
//////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
Expand Down Expand Up @@ -354,7 +354,7 @@ private static void AssertXpsText(XpsElement element)
public void CompareDocuments()
{
//ExStart:CompareDocuments
//GistId:695136dbbe4f541a8a0a17b3d3468689
//GistId:695136dbbe4f541a8a0a17b3d3468689
//ExFor:Comparer.Compare(String, String, String, String, DateTime)
//ExFor:Comparer.Compare(String, String, String, SaveFormat, String, DateTime)
//ExFor:Comparer.Compare(String, String, String, String, DateTime, CompareOptions)
Expand All @@ -366,32 +366,39 @@ public void CompareDocuments()

Comparer.Compare(firstDoc, secondDoc, ArtifactsDir + "LowCode.CompareDocuments.1.docx", "Author", new DateTime());
Comparer.Compare(firstDoc, secondDoc, ArtifactsDir + "LowCode.CompareDocuments.2.docx", SaveFormat.Docx, "Author", new DateTime());
Comparer.Compare(firstDoc, secondDoc, ArtifactsDir + "LowCode.CompareDocuments.3.docx", "Author", new DateTime(), new CompareOptions() { IgnoreCaseChanges = true });
Comparer.Compare(firstDoc, secondDoc, ArtifactsDir + "LowCode.CompareDocuments.4.docx", SaveFormat.Docx, "Author", new DateTime(), new CompareOptions() { IgnoreCaseChanges = true });

CompareOptions compareOptions = new CompareOptions();
compareOptions.IgnoreCaseChanges =true;
Comparer.Compare(firstDoc, secondDoc, ArtifactsDir + "LowCode.CompareDocuments.3.docx", "Author", new DateTime(), compareOptions);
Comparer.Compare(firstDoc, secondDoc, ArtifactsDir + "LowCode.CompareDocuments.4.docx", SaveFormat.Docx, "Author", new DateTime(), compareOptions);
//ExEnd:CompareDocuments
}

[Test]
public void CompareStreamDocuments()
{
//ExStart:CompareStreamDocuments
//GistId:695136dbbe4f541a8a0a17b3d3468689
//GistId:695136dbbe4f541a8a0a17b3d3468689
//ExFor:Comparer.Compare(Stream, Stream, Stream, SaveFormat, String, DateTime)
//ExFor:Comparer.Compare(Stream, Stream, Stream, SaveFormat, String, DateTime, CompareOptions)
//ExSummary:Shows how to compare documents from the stream.
// There is a several ways to compare documents from the stream:
using (FileStream firstStreamIn = new FileStream(MyDir + "Table column bookmarks.docx", FileMode.Open, FileAccess.Read))
{
using (FileStream secondStreamIn = new FileStream(MyDir + "Table column bookmarks.doc", FileMode.Open, FileAccess.Read))
{
using (FileStream streamOut = new FileStream(ArtifactsDir + "LowCode.CompareStreamDocuments.1.docx", FileMode.Create, FileAccess.ReadWrite))
Comparer.Compare(firstStreamIn, secondStreamIn, streamOut, SaveFormat.Docx, "Author", new DateTime());

using (FileStream streamOut = new FileStream(ArtifactsDir + "LowCode.CompareStreamDocuments.2.docx", FileMode.Create, FileAccess.ReadWrite))
Comparer.Compare(firstStreamIn, secondStreamIn, streamOut, SaveFormat.Docx, "Author", new DateTime(), new CompareOptions() { IgnoreCaseChanges = true });
}
using (FileStream firstStreamIn = new FileStream(MyDir + "Table column bookmarks.docx", FileMode.Open, FileAccess.Read))
{
using (FileStream secondStreamIn = new FileStream(MyDir + "Table column bookmarks.doc", FileMode.Open, FileAccess.Read))
{
using (FileStream streamOut = new FileStream(ArtifactsDir + "LowCode.CompareStreamDocuments.1.docx", FileMode.Create, FileAccess.ReadWrite))
Comparer.Compare(firstStreamIn, secondStreamIn, streamOut, SaveFormat.Docx, "Author", new DateTime());

using (FileStream streamOut = new FileStream(ArtifactsDir + "LowCode.CompareStreamDocuments.2.docx", FileMode.Create, FileAccess.ReadWrite))
{
CompareOptions compareOptions = new CompareOptions();
compareOptions.IgnoreCaseChanges = true;
Comparer.Compare(firstStreamIn, secondStreamIn, streamOut, SaveFormat.Docx, "Author", new DateTime(), compareOptions);
}
}
//ExEnd:CompareStreamDocuments
}
//ExEnd:CompareStreamDocuments
}

[Test]
Expand All @@ -411,7 +418,9 @@ public void MailMerge()

MailMerger.Execute(doc, ArtifactsDir + "LowCode.MailMerge.1.docx", fieldNames, fieldValues);
MailMerger.Execute(doc, ArtifactsDir + "LowCode.MailMerge.2.docx", SaveFormat.Docx, fieldNames, fieldValues);
MailMerger.Execute(doc, ArtifactsDir + "LowCode.MailMerge.3.docx", SaveFormat.Docx, new MailMergeOptions() { TrimWhitespaces = true }, fieldNames, fieldValues);
MailMergeOptions mailMergeOptions = new MailMergeOptions();
mailMergeOptions.TrimWhitespaces = true;
MailMerger.Execute(doc, ArtifactsDir + "LowCode.MailMerge.3.docx", SaveFormat.Docx, mailMergeOptions, fieldNames, fieldValues);
//ExEnd:MailMerge
}

Expand All @@ -433,7 +442,11 @@ public void MailMergeStream()
MailMerger.Execute(streamIn, streamOut, SaveFormat.Docx, fieldNames, fieldValues);

using (FileStream streamOut = new FileStream(ArtifactsDir + "LowCode.MailMergeStream.2.docx", FileMode.Create, FileAccess.ReadWrite))
MailMerger.Execute(streamIn, streamOut, SaveFormat.Docx, new MailMergeOptions() { TrimWhitespaces = true }, fieldNames, fieldValues);
{
MailMergeOptions mailMergeOptions = new MailMergeOptions();
mailMergeOptions.TrimWhitespaces = true;
MailMerger.Execute(streamIn, streamOut, SaveFormat.Docx, mailMergeOptions, fieldNames, fieldValues);
}
}
//ExEnd:MailMergeStream
}
Expand Down Expand Up @@ -684,9 +697,11 @@ public void Replace()
string pattern = "(C)2006 Aspose Pty Ltd.";
string replacement = "Copyright (C) 2024 by Aspose Pty Ltd.";

FindReplaceOptions options = new FindReplaceOptions();
options.FindWholeWordsOnly = false;
Replacer.Replace(doc, ArtifactsDir + "LowCode.Replace.1.docx", pattern, replacement);
Replacer.Replace(doc, ArtifactsDir + "LowCode.Replace.2.docx", SaveFormat.Docx, pattern, replacement);
Replacer.Replace(doc, ArtifactsDir + "LowCode.Replace.3.docx", SaveFormat.Docx, pattern, replacement, new FindReplaceOptions() { FindWholeWordsOnly = false });
Replacer.Replace(doc, ArtifactsDir + "LowCode.Replace.3.docx", SaveFormat.Docx, pattern, replacement, options);
//ExEnd:Replace
}

Expand All @@ -708,7 +723,11 @@ public void ReplaceStream()
Replacer.Replace(streamIn, streamOut, SaveFormat.Docx, pattern, replacement);

using (FileStream streamOut = new FileStream(ArtifactsDir + "LowCode.ReplaceStream.2.docx", FileMode.Create, FileAccess.ReadWrite))
Replacer.Replace(streamIn, streamOut, SaveFormat.Docx, pattern, replacement, new FindReplaceOptions() { FindWholeWordsOnly = false });
{
FindReplaceOptions options = new FindReplaceOptions();
options.FindWholeWordsOnly = false;
Replacer.Replace(streamIn, streamOut, SaveFormat.Docx, pattern, replacement, options);
}
}
//ExEnd:ReplaceStream
}
Expand Down Expand Up @@ -939,8 +958,10 @@ public void SplitDocument()
//ExSummary:Shows how to split document by pages.
string doc = MyDir + "Big document.docx";

Splitter.Split(doc, ArtifactsDir + "LowCode.SplitDocument.1.docx", new SplitOptions() { SplitCriteria = SplitCriteria.Page });
Splitter.Split(doc, ArtifactsDir + "LowCode.SplitDocument.2.docx", SaveFormat.Docx, new SplitOptions() { SplitCriteria = SplitCriteria.Page });
SplitOptions options = new SplitOptions();
options.SplitCriteria = SplitCriteria.Page;
Splitter.Split(doc, ArtifactsDir + "LowCode.SplitDocument.1.docx", options);
Splitter.Split(doc, ArtifactsDir + "LowCode.SplitDocument.2.docx", SaveFormat.Docx, options);
//ExEnd:SplitDocument
}

Expand All @@ -953,7 +974,9 @@ public void SplitDocumentStream()
//ExSummary:Shows how to split document from the stream by pages.
using (FileStream streamIn = new FileStream(MyDir + "Big document.docx", FileMode.Open, FileAccess.Read))
{
Stream[] stream = Splitter.Split(streamIn, SaveFormat.Docx, new SplitOptions() { SplitCriteria = SplitCriteria.Page });
SplitOptions options = new SplitOptions();
options.SplitCriteria = SplitCriteria.Page;
Stream[] stream = Splitter.Split(streamIn, SaveFormat.Docx, options);
}
//ExEnd:SplitDocumentStream
}
Expand All @@ -973,8 +996,10 @@ public void WatermarkText()

Watermarker.SetText(doc, ArtifactsDir + "LowCode.WatermarkText.1.docx", watermarkText);
Watermarker.SetText(doc, ArtifactsDir + "LowCode.WatermarkText.2.docx", SaveFormat.Docx, watermarkText);
Watermarker.SetText(doc, ArtifactsDir + "LowCode.WatermarkText.3.docx", watermarkText, new TextWatermarkOptions() { Color = Color.Red });
Watermarker.SetText(doc, ArtifactsDir + "LowCode.WatermarkText.4.docx", SaveFormat.Docx, watermarkText, new TextWatermarkOptions() { Color = Color.Red });
TextWatermarkOptions watermarkOptions = new TextWatermarkOptions();
watermarkOptions.Color = Color.Red;
Watermarker.SetText(doc, ArtifactsDir + "LowCode.WatermarkText.3.docx", watermarkText, watermarkOptions);
Watermarker.SetText(doc, ArtifactsDir + "LowCode.WatermarkText.4.docx", SaveFormat.Docx, watermarkText, watermarkOptions);
//ExEnd:WatermarkText
}

Expand All @@ -992,9 +1017,13 @@ public void WatermarkTextStream()
{
using (FileStream streamOut = new FileStream(ArtifactsDir + "LowCode.WatermarkTextStream.1.docx", FileMode.Create, FileAccess.ReadWrite))
Watermarker.SetText(streamIn, streamOut, SaveFormat.Docx, watermarkText);


using (FileStream streamOut = new FileStream(ArtifactsDir + "LowCode.WatermarkTextStream.2.docx", FileMode.Create, FileAccess.ReadWrite))
Watermarker.SetText(streamIn, streamOut, SaveFormat.Docx, watermarkText, new TextWatermarkOptions() { Color = Color.Red });
{
TextWatermarkOptions options = new TextWatermarkOptions();
options.Color = Color.Red;
Watermarker.SetText(streamIn, streamOut, SaveFormat.Docx, watermarkText, options);
}
}
//ExEnd:WatermarkTextStream
}
Expand All @@ -1014,8 +1043,11 @@ public void WatermarkImage()

Watermarker.SetImage(doc, ArtifactsDir + "LowCode.SetWatermarkImage.1.docx", watermarkImage);
Watermarker.SetImage(doc, ArtifactsDir + "LowCode.SetWatermarkText.2.docx", SaveFormat.Docx, watermarkImage);
Watermarker.SetImage(doc, ArtifactsDir + "LowCode.SetWatermarkText.3.docx", watermarkImage, new ImageWatermarkOptions() { Scale = 50 });
Watermarker.SetImage(doc, ArtifactsDir + "LowCode.SetWatermarkText.4.docx", SaveFormat.Docx, watermarkImage, new ImageWatermarkOptions() { Scale = 50 });

ImageWatermarkOptions options = new ImageWatermarkOptions();
options.Scale = 50;
Watermarker.SetImage(doc, ArtifactsDir + "LowCode.SetWatermarkText.3.docx", watermarkImage, options);
Watermarker.SetImage(doc, ArtifactsDir + "LowCode.SetWatermarkText.4.docx", SaveFormat.Docx, watermarkImage, options);
//ExEnd:WatermarkImage
}

Expand Down

0 comments on commit 8c65f24

Please sign in to comment.