Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 15, 2024 08:30
Show Gist options
  • Save aspose-com-gists/6f0d8b5420af1af6af2d064587dd6803 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/6f0d8b5420af1af6af2d064587dd6803 to your computer and use it in GitHub Desktop.
Gists for Aspose.Page for .NET
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_GettingStarted();
// Initialize license object
License license = new License();
// Set license
license.SetLicense("D:\\Aspose.Total.NET.lic");
Console.WriteLine("License set successfully.");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_GettingStarted();
// Initialize license object
Aspose.Page.License license = new Aspose.Page.License();
// Load license in FileStream
FileStream myStream = new FileStream("Aspose.Total.NET.lic", FileMode.Open);
// Set license
license.SetLicense(myStream);
Console.WriteLine("License set successfully.");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
using (Stream zip = new SecureLicense().GetType().Assembly.GetManifestResourceStream("Aspose.Total.NET.lic.zip"))
{
using (ZipFile zf = ZipFile.Read(zip))
{
MemoryStream ms = new MemoryStream();
ZipEntry e = zf["Aspose.Total.NET.lic"];
e.ExtractWithPassword(ms, "test");
ms.Position = 0;
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_GettingStarted();
// Initialize license object
Aspose.Page.License license = new Aspose.Page.License();
// Set license
license.SetLicense("MergedAPI.Aspose.Total.NET.lic");
// Set the value to indicate that license will be embedded in the application
license.Embedded = true;
Console.WriteLine("License set successfully.");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize PDF output stream
System.Drawing.Imaging.ImageFormat imageFormat = System.Drawing.Imaging.ImageFormat.Png;
// Initialize PsDocument with PostScript file
PsDocument document = new PsDocument(dataDir + "inputForImage.ps");
// If you want to convert Postscript file despite of minor errors set this flag
bool suppressErrors = true;
//Initialize options object with necessary parameters.
// Default image format is PNG and it is not mandatory to set it in ImageSaveOptions
// Default image size is 595x842 and it is not mandatory to set it in ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(suppressErrors);
// But if you need to specify size and image format use constructor with parameters
//ImageSaveOptions options = new ImageSaveOptions(new System.Drawing.Size(595, 842), ImageFormat.Jpeg, suppressErrors);
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };
// Save PS document as images bytes arrays, one bytes array for one page.
byte [][] imagesBytes = document.Save(device, options);
int i = 0;
foreach (byte[] imageBytes in imagesBytes)
{
string imagePath = Path.GetFullPath("out_image" + i.ToString() +"." + imageFormat.ToString().ToLower());
using (FileStream fs = new FileStream(imagePath, FileMode.Create, FileAccess.Write))
{
fs.Write(imageBytes, 0, imageBytes.Length);
}
i++;
}
//Review errors
if (suppressErrors)
{
foreach (PsConverterException ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize PsDocument with PostScript file
PsDocument document = new PsDocument(dataDir + "input.ps");
// If you want to convert Postscript file despite of minor errors set this flag
bool suppressErrors = true;
//Initialize options object with necessary parameters.
// Default page size is 595x842 and it is not mandatory to set it in PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions(suppressErrors);
// But if you need to specify size and image format use following line
// PdfSaveOptions options = new PdfSaveOptions(suppressErrors, new System.Drawing.Size(595, 842));
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };
// Save PS document as PDF
document.SaveAsPdf(dataDir + "outputPDF_out.pdf", options);
//Review errors
if (suppressErrors)
{
foreach (PsConverterException ex in options.Exceptions)
{
Console.WriteLine(ex.Message);
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Input file
string inputFileName = dataDir + "input.xps";
//Outut file
string outputFileName = dataDir + "XPStoImage_out.bmp";
// Initialize XPS input stream
using (Stream xpsStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
{
// Load XPS document form the stream
XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize options object with necessary parameters.
BmpSaveOptions options = new BmpSaveOptions()
{
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality,
Resolution = 300,
PageNumbers = new int[] { 1, 2, 6 }
};
// Save XPS document to the images byte arrays. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.SaveAsImage(options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Input file
string inputFileName = dataDir + "input.xps";
//Outut file
string outputFileName = dataDir + "XPStoImage_out.jpeg";
// Initialize XPS input stream
using (Stream xpsStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
{
// Load XPS document form the stream
XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize options object with necessary parameters.
JpegSaveOptions options = new JpegSaveOptions()
{
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality,
Resolution = 300,
PageNumbers = new int[] { 1, 2, 6 }
};
// Save XPS document to the images byte arrays. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.SaveAsImage(options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Initialize XPS input stream
using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
// Load XPS document form the stream
XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize options object with necessary parameters.
Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions()
{
JpegQualityLevel = 100,
ImageCompression = Aspose.Page.XPS.Presentation.Pdf.PdfImageCompression.Jpeg,
TextCompression = Aspose.Page.XPS.Presentation.Pdf.PdfTextCompression.Flate,
PageNumbers = new int[] { 1, 2, 6 }
};
// Save XPS document as PDF
document.Save(dataDir + "XPStoPDF.pdf", options);
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Input file
string inputFileName = dataDir + "input.xps";
//Outut file
string outputFileName = dataDir + "XPStoImage_out.png";
// Initialize XPS input stream
using (Stream xpsStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
{
// Load XPS document form the stream
XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize options object with necessary parameters.
PngSaveOptions options = new PngSaveOptions()
{
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality,
Resolution = 300,
PageNumbers = new int[] { 1, 2, 6 }
};
// Save XPS document to the images byte arrays. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.SaveAsImage(options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion();
// Input file
string inputFileName = dataDir + "input.xps";
//Outut file
string outputFileName = dataDir + "XPStoImage_out.tif";
// Initialize XPS input stream
using (Stream xpsStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
{
// Load XPS document form the stream
XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize options object with necessary parameters.
TiffSaveOptions options = new TiffSaveOptions()
{
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality,
Resolution = 300,
PageNumbers = new int[] { 1, 2, 6 }
};
// Save XPS document to the images byte arrays. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.SaveAsImage(options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithGradient();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Initialize List of XpsGradentStop
List<XpsGradientStop> stops = new List<XpsGradientStop>();
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 244, 253, 225), 0.0673828f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 251, 240, 23), 0.314453f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 252, 209, 0), 0.482422f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 241, 254, 161), 0.634766f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 53, 253, 255), 0.915039f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 12, 91, 248), 1f));
// Create new path by defining geometery in abbreviation form
XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,210 L 228,210 228,300 10,300"));
path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 0f), new PointF(228f, 0f));
((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);
// Save resultant XPS document
doc.Save(dataDir + "AddHorizontalGradient_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithGradient();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Initialize List of XpsGradentStop
List<XpsGradientStop> stops = new List<XpsGradientStop>();
// Add Colors to Gradient
stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 142, 4), 0f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 202, 0), 0.144531f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 250, 0), 0.264648f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 0, 0), 0.414063f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(233, 0, 255), 0.544922f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(107, 27, 190), 0.694336f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(63, 0, 255), 0.844727f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 199, 80), 1f));
// Create new path by defining geometery in abbreviation form
XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,10 L 228,10 228,100 10,100"));
path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 10f), new PointF(228f, 100f));
((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);
// Save resultant XPS document
doc.Save(dataDir + "AddLinearGradient_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithGradient();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Initialize List of XpsGradentStop
List<XpsGradientStop> stops = new List<XpsGradientStop>();
stops.Add(doc.CreateGradientStop(doc.CreateColor(253, 255, 12, 0), 0f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(252, 255, 154, 0), 0.359375f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(252, 255, 56, 0), 0.424805f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(253, 255, 229, 0), 0.879883f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(252, 255, 255, 234), 1f));
// Create new path by defining geometery in abbreviation form
XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,110 L 228,110 228,200 10,200"));
path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 110f), new PointF(10f, 200f));
((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);
// Save resultant XPS document
doc.Save(dataDir + "AddVerticalGradient_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithImages();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Add Image
XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
//Creating a matrix is optional, it can be used for proper positioning
path.RenderTransform = doc.CreateMatrix(0.7f, 0f, 0f, 0.7f, 0f, 20f);
//Create Image Brush
path.Fill = doc.CreateImageBrush(dataDir + "QL_logo_color.tif", new RectangleF(0f, 0f, 258.24f, 56.64f), new RectangleF(50f, 20f, 193.68f, 42.48f));
// Save resultant XPS document
doc.Save(dataDir + "AddImage_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithImages();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Tile image
// ImageBrush filled rectangle in the right top bellow
XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,160 L 228,160 228,305 10,305"));
path.Fill = doc.CreateImageBrush(dataDir + "R08LN_NN.jpg", new RectangleF(0f, 0f, 128f, 96f), new RectangleF(0f, 0f, 64f, 48f));
((XpsImageBrush)path.Fill).TileMode = XpsTileMode.Tile;
path.Fill.Opacity = 0.5f;
// Save resultant XPS document
doc.Save(dataDir + "AddTiledImage_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithImages();
// Create new XPS Document
using (XpsDocument doc = new XpsDocument())
{
// Set first page's size.
doc.Page.Width = 540f;
doc.Page.Height = 220f;
// Draw the image box.
RectangleF imageBox = new RectangleF(10f, 10f, 200f, 200f);
XpsPath path = doc.AddPath(doc.Utils.CreateRectangle(imageBox));
path.Stroke = doc.CreateSolidColorBrush(Color.Black);
// Add an image to fit width.
path = doc.Utils.CreateImage(dataDir + "R08LN_NN.jpg", imageBox, ImageMode.FitToWidth);
// Prevent tiling.
((XpsImageBrush)path.Fill).TileMode = XpsTileMode.None;
doc.Add(path);
// Add an image to fit width.
doc.Add(doc.Utils.CreateImage(dataDir + "R08LN_NN.jpg", new RectangleF(220f, 10f, 200f, 100f), ImageMode.FitToHeight));
// Add an image to fit width.
doc.Add(doc.Utils.CreateImage(dataDir + "R08LN_NN.jpg", new RectangleF(430f, 10f, 100f, 200f), ImageMode.FitToBox));
// Save resultant XPS document
doc.Save(dataDir + "UseImageUtilsXPS.xps");
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithPages();
// Create new XPS Document
XpsDocument doc = new XpsDocument(dataDir + "Sample1.xps");
// Insert an empty page at beginning of pages list
doc.InsertPage(1, true);
// Save resultant XPS document
doc.Save(dataDir + "AddPages_out.xps");
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithPages();
// Open an XPS document
using (XpsDocument doc = new XpsDocument(dataDir + "Sample3.xps"))
// Create a font
using (Stream fontStream = File.OpenRead(dataDir + "arialbd.ttf"))
{
// Create options for conversion to PDF
PdfSaveOptions options = new PdfSaveOptions();
// Set the filter for the pages that need conversion
options.PageNumbers = new int[] { 2, 6, 7, 13 };
// Add the event handler that will execute right before the conversion of each page
options.BeforePageSavingEventHandlers.Add(new NavigationInjector(doc.CreateFont(fontStream), options.PageNumbers));
// Save resultant XPS document
doc.SaveAsPdf(dataDir + "ModifyPageOnConversion_out.pdf", options);
}
/// <summary>
/// The class to handle the before-page event while converting an XPS document.
/// </summary>
public class NavigationInjector : BeforePageSavingEventHandler
{
// The font in which navigation hyperlinks and page numbers will be displayed.
private readonly XpsFont _font;
// The page numbers to convert.
private readonly SortedList<int, int> _pageNumbers;
public NavigationInjector(XpsFont font, int[] pageNumbers)
{
_font = font;
if (pageNumbers == null)
return;
// Turn the page number array into a sorted collection of unique values.
_pageNumbers = new SortedList<int, int>();
foreach (int pn in pageNumbers)
_pageNumbers[pn] = 0;
}
public override void Handle(BeforeSavingEventArgs<PageAPI> args)
{
PageAPI api = args.ElementAPI;
XpsGlyphs glyphs;
// For all pages in the output PDF except the first one...
if (args.OutputPageNumber > 1)
{
// ...insert a hyperlink to the first page...
glyphs = api.CreateGlyphs(_font, 15f, 5f, api.Height - 10f, "[First]");
glyphs.Fill = api.CreateSolidColorBrush(Color.Blue);
glyphs.HyperlinkTarget = new XpsPageLinkTarget(_pageNumbers == null ? 1 : _pageNumbers.Keys[0]);
api.Add(glyphs);
// ...and to the previous page.
glyphs = api.CreateGlyphs(_font, 15f, 60f, api.Height - 10f, "[Prev]");
glyphs.Fill = api.CreateSolidColorBrush(Color.Blue);
glyphs.HyperlinkTarget = new XpsPageLinkTarget(
_pageNumbers == null ? args.AbsolutePageNumber - 1 : _pageNumbers.Keys[args.OutputPageNumber - 2]);
api.Add(glyphs);
}
// For all pages in the output PDF except the last one...
if ((_pageNumbers != null && args.OutputPageNumber < _pageNumbers.Count) ||
(_pageNumbers == null && args.OutputPageNumber < api.TotalPageCount))
{
// ...insert a hyperlink to the next page...
glyphs = api.CreateGlyphs(_font, 15f, 110f, api.Height - 10f, "[Next]");
glyphs.Fill = api.CreateSolidColorBrush(Color.Blue);
glyphs.HyperlinkTarget = new XpsPageLinkTarget(
_pageNumbers == null ? args.AbsolutePageNumber + 1 : _pageNumbers.Keys[args.OutputPageNumber]);
api.Add(glyphs);
// ...and to the last page.
glyphs = api.CreateGlyphs(_font, 15f, 160f, api.Height - 10f, "[Last]");
glyphs.Fill = api.CreateSolidColorBrush(Color.Blue);
glyphs.HyperlinkTarget = new XpsPageLinkTarget(
_pageNumbers == null ? api.TotalPageCount : _pageNumbers.Keys[_pageNumbers.Keys.Count - 1]);
api.Add(glyphs);
}
// Insert a page number in the bottom-right corner.
glyphs = api.CreateGlyphs(_font, 15f, api.Width - 20f, api.Height - 10f, args.OutputPageNumber.ToString());
glyphs.Fill = api.CreateSolidColorBrush(Color.Black);
api.Add(glyphs);
// Add an outline entry to display the links to the converted pages in the navigation pane of a PDF viewer.
api.AddOutlineEntry(string.Format("Page {0}", args.OutputPageNumber), 1, args.AbsolutePageNumber);
}
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dir = RunExamples.GetDataDir_WorkingWithPrintTickets();
// Create new XPS document
using (XpsDocument document = new XpsDocument())
{
// Set a custom job-level print ticket
document.JobPrintTicket = new JobPrintTicket(
// Specify input bin.
new JobInputBin(InputBin.InputBinOption.Manual.Clone().Add(
InputBin.FeedFace.FaceDown, InputBin.FeedDirection.LongEdgeFirst, new InputBin.MediaSheetCapacity(100))),
// Specify output bin.
new JobOutputBin(new OutputBin.OutputBinOption(OutputBin.BinType.Sorter),
new OutputBin.OutputBinOption(OutputBin.BinType.Stacker, new OutputBin.MediaSheetCapacity(100))),
// Specify page orientation.
new PageOrientation(PageOrientation.PageOrientationOption.Landscape),
// Specify duplex mode fof the output.
new JobDuplexAllDocumentsContiguously(Duplex.DuplexOption.TwoSidedLongEdge(Duplex.DuplexMode.Automatic)),
// Specify the color settings for the output.
new PageOutputColor(PageOutputColor.PageOutputColorOption.Grayscale(0, 8)));
// Save the document with the custom job-level print ticket.
document.Save(dir + "output1.xps");
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithShapes();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Radial gradient stroked ellipse in the lower left
List<XpsGradientStop> stops = new List<XpsGradientStop>();
stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 0, 255), 0f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 0, 0), .25f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(0, 255, 0), .5f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 255, 0), .75f));
stops.Add(doc.CreateGradientStop(doc.CreateColor(255, 0, 0), 1f));
XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 20,250 A 100,50 0 1 1 220,250 100,50 0 1 1 20,250"));
path.Stroke = doc.CreateRadialGradientBrush(new PointF(575f, 125f), new PointF(575f, 100f), 75f, 50f);
((XpsGradientBrush)path.Stroke).SpreadMethod = XpsSpreadMethod.Reflect;
((XpsGradientBrush)path.Stroke).GradientStops.AddRange(stops);
stops.Clear();
path.StrokeThickness = 12f;
// Save resultant XPS document
doc.Save(dataDir + "AddEllipse_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithShapes();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// CMYK (blue) solid color stroked rectangle in the lower left
XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 20,10 L 220,10 220,100 20,100 Z"));
path.Stroke = doc.CreateSolidColorBrush(
doc.CreateColor(dataDir + "uswebuncoated.icc", 1.0f, 1.000f, 0.000f, 0.000f, 0.000f));
path.StrokeThickness = 12f;
// Save resultant XPS document
doc.Save(dataDir + "AddRectangle_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithShapes();
// Create new XPS Document
using (XpsDocument doc = new XpsDocument())
{
// Set first page's size.
doc.Page.Width = 650f;
doc.Page.Height = 240f;
// Draw a circle with center (120, 120) and radius 100.
XpsPath path = doc.CreatePath(doc.Utils.CreateCircle(new PointF(120f, 120f), 100f));
path.Fill = doc.CreateSolidColorBrush(Color.Green);
doc.Add(path);
// Inscribe a regular pentagon in the circle.
path = doc.CreatePath(doc.Utils.CreateRegularInscribedNGon(5, new PointF(120f, 120f), 100f));
path.Fill = doc.CreateSolidColorBrush(Color.Red);
doc.Add(path);
// Circumscribe a regular hexagon around the circle.
path = doc.CreatePath(doc.Utils.CreateRegularCircumscribedNGon(6, new PointF(120f, 120f), 100f));
path.Stroke = doc.CreateSolidColorBrush(Color.Magenta);
path.StrokeThickness = 3f;
doc.Add(path);
// Draw a sector of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
path = doc.CreatePath(doc.Utils.CreatePieSlice(new PointF(340f, 120f), 100f, -45f, 45f));
path.Stroke = doc.CreateSolidColorBrush(Color.Red);
path.StrokeThickness = 5f;
doc.Add(path);
// Draw a segment of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
path = doc.CreatePath(doc.Utils.CreateCircularSegment(new PointF(340f, 120f), 100f, -45f, 45f));
path.Fill = doc.CreateSolidColorBrush(Color.Black);
doc.Add(path);
// Draw a rectangle with the top left vertex (530, 20), width 100 units and height 200 units.
path = doc.CreatePath(doc.Utils.CreateRectangle(new RectangleF(530f, 20f, 100f, 200f)));
path.Stroke = doc.CreateSolidColorBrush(Color.Red);
doc.Add(path);
// Draw an ellipse with center (580, 120) and radii 50 and 100.
path = doc.CreatePath(doc.Utils.CreateEllipse(new PointF(580f, 120f), 50f, 100f));
path.Fill = doc.CreateSolidColorBrush(Color.Yellow);
doc.Add(path);
doc.Save(dataDir + "UseShapeUtilsXPS_out.xps");
}
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithText();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
//Create a brush
XpsSolidColorBrush textFill = doc.CreateSolidColorBrush(Color.Black);
//Add glyph to the document
XpsGlyphs glyphs = doc.AddGlyphs("Arial", 12, FontStyle.Regular, 300f, 450f, "Hello World!");
glyphs.Fill = textFill;
// Save resultant XPS document
doc.Save(dataDir + "AddText_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithText();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Add Text
XpsSolidColorBrush textFill = doc.CreateSolidColorBrush(Color.Black);
XpsGlyphs glyphs = doc.AddGlyphs("Arial", 20, FontStyle.Regular, 400f, 200f, "TEN. rof SPX.esopsA");
glyphs.BidiLevel = 1;
glyphs.Fill = textFill;
// Save resultant XPS document
doc.Save(dataDir + "AddText_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithTransparency();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
// Just to demonstrate transparency
doc.AddPath(doc.CreatePathGeometry("M120,0 H400 v1000 H120")).Fill = doc.CreateSolidColorBrush(Color.Gray);
doc.AddPath(doc.CreatePathGeometry("M300,120 h600 V420 h-600")).Fill = doc.CreateSolidColorBrush(Color.Gray);
// Create path with closed rectangle geometry
XpsPath path1 = doc.CreatePath(doc.CreatePathGeometry("M20,20 h200 v200 h-200 z"));
// Set blue solid brush to fill path1
path1.Fill = doc.CreateSolidColorBrush(Color.Blue);
// Add it to the current page
XpsPath path2 = doc.Add(path1);
// path1 and path2 are the same as soon as path1 hasn't been placed inside any other element
// (which means that path1 had no parent element).
// Because of that rectangle's color on the page effectively turns to green
path2.Fill = doc.CreateSolidColorBrush(Color.Green);
// Now add path2 once again. Now path2 has parent. So path3 won't be the same as path2.
// Thus a new rectangle is painted on the page ...
XpsPath path3 = doc.Add(path2);
// ... and we shift it 300 units lower ...
path3.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 300);
// ... and set red solid brush to fill it
path3.Fill = doc.CreateSolidColorBrush(Color.Red);
// Create new path4 with path2's geometry ...
XpsPath path4 = doc.AddPath(path2.Data);
// ... shift it 300 units to the right ...
path4.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 300, 0);
// ... and set blue solid fill
path4.Fill = doc.CreateSolidColorBrush(Color.Blue);
// Add path4 once again.
XpsPath path5 = doc.Add(path4);
// path4 and path5 are not the same again ...
// (move path5 300 units lower)
path5.RenderTransform = path5.RenderTransform.Clone(); // to disconnect RenderTransform value from path4 (see next comment about Fill property)
path5.RenderTransform.Translate(0, 300);
// ... but if we set the opacity of Fill property, it will take effect on both path5 and path4
// because brush is a complex property value which remains the same for path5 and path4
path5.Fill.Opacity = 0.8f;
// Create new path6 with path2's geometry ...
XpsPath path6 = doc.AddPath(path2.Data);
// ... shift it 600 units to the right ...
path6.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 600, 0);
// ... and set yellow solid fill
path6.Fill = doc.CreateSolidColorBrush(Color.Yellow);
// Now add path6's clone ...
XpsPath path7 = doc.Add(path6.Clone());
// (move path5 300 units lower)
path7.RenderTransform = path7.RenderTransform.Clone();
path7.RenderTransform.Translate(0, 300);
// ... and set opacity for path7
path7.Fill.Opacity = 0.8f;
// Now opacity effects independantly as soon as property values are cloned along with the element
// The following code block is equivalent to the previous one.
// Add path6 itself. path6 and path7 are not the same. Although their Fill property values are the same
//XpsPath path7 = doc.Add(path6);
//path7.RenderTransform = path7.RenderTransform.Clone();
//path7.RenderTransform.Translate(0, 300);
// To "disconnect" path7's Fill property from path6's Fill property reassign it to its clone (or path6's Fill clone)
//path7.Fill = ((XpsSolidColorBrush)path7.Fill).Clone();
//path7.Fill.Opacity = 0.8f;
// Save resultant XPS document
doc.Save(dataDir + "WorkingWithTransparency_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithTransparency();
// Create new XPS Document
XpsDocument doc = new XpsDocument();
//Add Canvas to XpsDocument instance
XpsCanvas canvas = doc.AddCanvas();
// Rectangle with opacity masked by ImageBrush
XpsPath path = canvas.AddPath(doc.CreatePathGeometry("M 10,180 L 228,180 228,285 10,285"));
path.Fill = doc.CreateSolidColorBrush(doc.CreateColor(1.0f, 0.0f, 0.0f));
path.OpacityMask = doc.CreateImageBrush(dataDir + "R08SY_NN.tif", new RectangleF(0f, 0f, 128f, 192f),
new RectangleF(0f, 0f, 64f, 96f));
((XpsImageBrush)path.OpacityMask).TileMode = XpsTileMode.Tile;
// Save resultant XPS document
doc.Save(dataDir + "OpacityMask_out.xps");
// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithVisualBrush();
XpsDocument doc = new XpsDocument();
// Geometry for magenta grid VisualBrush
XpsPathGeometry pathGeometry = doc.CreatePathGeometry();
pathGeometry.AddSegment(doc.CreatePolyLineSegment(
new PointF[] { new PointF(240f, 5f), new PointF(240f, 310f), new PointF(0f, 310f) }));
pathGeometry[0].StartPoint = new PointF(0f, 5f);
// Canvas for magenta grid VisualBrush
XpsCanvas visualCanvas = doc.CreateCanvas();
XpsPath visualPath = visualCanvas.AddPath(
doc.CreatePathGeometry("M 0,4 L 4,4 4,0 6,0 6,4 10,4 10,6 6,6 6,10 4,10 4,6 0,6 Z"));
visualPath.Fill = doc.CreateSolidColorBrush(doc.CreateColor(1f, .61f, 0.1f, 0.61f));
XpsPath gridPath = doc.CreatePath(pathGeometry);
//Create Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
gridPath.Fill = doc.CreateVisualBrush(visualCanvas,
new RectangleF(0f, 0f, 10f, 10f), new RectangleF(0f, 0f, 10f, 10f));
((XpsVisualBrush)gridPath.Fill).TileMode = XpsTileMode.Tile;
// New canvas
XpsCanvas canvas = doc.AddCanvas();
canvas.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 268f, 70f);
// Add grid
canvas.AddPath(gridPath);
// Red transparent rectangle in the middle top
XpsPath path = canvas.AddPath(doc.CreatePathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
path = canvas.AddPath(doc.CreatePathGeometry("M 10,10 L 228,10 228,100 10,100"));
path.Fill = doc.CreateSolidColorBrush(doc.CreateColor(1.0f, 0.0f, 0.0f));
path.Opacity = 0.7f;
// Save resultant XPS document
doc.Save(dataDir + "AddGrid_out.xps");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment