Arbeiten Sie mit Leinwänden

Schneiden und transformieren Sie Leinwände von XPS-Dateien

 

Das Verwalten von Canvases in den Dokumenten ist eine der Funktionen, die Aspose.Page für .NET bietet. Dies ist eine Lösung für die Arbeit mit verschiedenen Seitenbeschreibungssprachen, insbesondere XPS XPS. Im folgenden Beispiel erfahren Sie, wie Sie:

  • Erstellen Sie eine Leinwand in einer XPS-Datei.

  • Machen Sie linke und obere Offsets im Hauptleinwand.

  • Fügen Sie der Hauptleinwand eine neue Leinwand mit einer übersetzten Transformation hinzu.

  • Fügen Sie der Hauptleinwand eine neue Leinwand mit einer Drehung um eine Punkttransformation hinzu.

  • Leinwand zuschneiden. Und viele weitere Manipulationen mit Leinwänden in XPS-Dateien.

Um Leinwände einer XPS-Datei umzuwandeln, folgen Sie der nächsten Anleitung:

  1. Erstellen Sie eine XPS-Datei mit der XpsDocument Class .
  2. Erstellen Sie mit der Methode AddCanvas() die Hauptleinwand, die für alle Seitenelemente gilt.
  3. Erstellen Sie mit der Methode CreateMatrix() linke und obere Offsets im Haupt-Canvas.
  4. Erstellen Sie rechteckige Pfadgeometrie mit der Methode CreatePathGeometry() .
  5. Erstellen Sie mithilfe der Klasse XpsBrush eine Füllung für Rechtecke.
  6. Um ein Rechteck in Leinwand 2 zu erstellen und es zu füllen, verwenden Sie die Klasse XpsPath .
  7. Verwenden Sie die CreateMatrix() -Methode, um Canvas 3 so zu übersetzen, dass ein neues Rechteck unter dem vorherigen Rechteck positioniert wird.
  8. Um diese Leinwand auf die rechte Seite der Seite zu übersetzen, verwenden Sie die Methode Translate() .
  9. Um die Leinwand 4 zu skalieren, rufen Sie die Methode Scale() auf.
  10. Um die Leinwand 5 um einen Punkt von 45 Grad zu drehen, wird die Methode RotateAround() verwendet praktisch.
  11. Speichern Sie das geänderte XPS-Dokument mit der Methode XPsDocument.Save() .

XPS-Datei Canvas Transformation C#-Code

    using Aspose.Page.Xps;
    using Aspose.Page.Xps.XpsModel;
    using System.Drawing;
    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();
            
    // Create a new XPS Document
    XpsDocument doc = new XpsDocument();

    // Create main canvas, common for all page elemnts
    XpsCanvas canvas1 = doc.AddCanvas();

    // Make left and top offsets in the main canvas
    canvas1.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 20, 10);

    // Create the rectangle path geometry
    XpsPathGeometry rectGeom = doc.CreatePathGeometry("M 0,0 L 200,0 200,100 0,100 Z");

    // Create a fill for rectangles
    XpsBrush fill = doc.CreateSolidColorBrush(doc.CreateColor(12, 15, 159));
            
    // Add new canvas without any transformations to the main canvas
    XpsCanvas canvas2 = canvas1.AddCanvas();
    // Create rectangle in this canvas and fill it
    XpsPath rect = canvas2.AddPath(rectGeom);
    rect.Fill = fill;

    // Add new canvas with translate transformation to the main canvas
    XpsCanvas canvas3 = canvas1.AddCanvas();
    // Translate this canvas to position new rectangle below the previous rectnagle
    canvas3.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 200);
    // Translate this canvas to the right side of the page
    canvas3.RenderTransform.Translate(500, 0);
    // Create the rectangle in this canvas and fill it
    rect = canvas3.AddPath(rectGeom);
    rect.Fill = fill;

    // Add new canvas with the double scale transformation to the main canvas
    XpsCanvas canvas4 = canvas1.AddCanvas();
    // Translate this canvas to position new rectangle below the previous rectnagle
    canvas4.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 400);
    // Scale this canvas
    canvas4.RenderTransform.Scale(2, 2);
    // Create a rectangle in this canvas and fill it
    rect = canvas4.AddPath(rectGeom);
    rect.Fill = fill;

    // Add new canvas with rotation around a point transformation to the main canvas
    XpsCanvas canvas5 = canvas1.AddCanvas();
    // Translate this canvas to position new rectangle below the previous rectnagle
    canvas5.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 800);
    // Rotate this canvas aroud a point on 45 degrees
    canvas5.RenderTransform.RotateAround(45, new PointF(100, 50));
    rect = canvas5.AddPath(rectGeom);
    rect.Fill = fill;
            
    // Save the resultant XPS document
    doc.Save(dataDir + "output1.xps");
Das nächste Code-Snippet zeigt, wie Leinwände von XPS-Dateien innerhalb der Aspose.Page für .NET-API-Lösung zugeschnitten werden.

Um Leinwände einer XPS-Datei zuzuschneiden, befolgen Sie die folgende Anleitung:

  1. Erstellen oder öffnen Sie eine XPS-Datei mit XpsDocument Class.
  2. Erstellen Sie mit der Methode AddCanvas() die Hauptleinwand, die für alle Seitenelemente gilt.
  3. Erstellen Sie mit der Methode CreateMatrix() linke und obere Offsets im Haupt-Canvas.
  4. Erstellen Sie rechteckige Pfadgeometrie mit der Methode CreatePathGeometry() .
  5. Erstellen Sie mithilfe der Klasse XpsBrush eine Füllung für Rechtecke.
  6. Um eine weitere Leinwand mit einem Clip zur Hauptleinwand hinzuzufügen, rufen Sie die AddCanvas()-Methode erneut auf.
  7. Erstellen Sie Kreisgeometrie für den Clip mithilfe der Klasse XpsPathGeometry .
  8. Um ein Rechteck in dieser Leinwand zu erstellen und es zu füllen, verwenden Sie die Klasse XpsPath .
  9. Fügen Sie eine weitere Leinwand mit der AddCanvas()-Methode hinzu, erstellen Sie dann ein Rechteck in dieser Leinwand und streichen Sie es mit der XpsPathGeometry-Klasse.
  10. Speichern Sie das geänderte XPS-Dokument mit der Methode XPsDocument.Save().

XPS-Datei-Canvas-Clipping-C#-Code

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();
            
    // Create a new XPS Document
    XpsDocument doc = new XpsDocument();

    // Create main canvas, common for all page elemnts
    XpsCanvas canvas1 = doc.AddCanvas();

    // Make left and top offsets in the main canvas
    canvas1.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 20, 10);

    // Create the rectangle path geometry
    XpsPathGeometry rectGeom = doc.CreatePathGeometry("M 0,0 L 500,0 500,300 0,300 Z");

    // Create a fill for rectangles
    XpsBrush fill = doc.CreateSolidColorBrush(doc.CreateColor(12, 15, 159));
            
    // Add another canvas with the clip to the main canvas
    XpsCanvas canvas2 = canvas1.AddCanvas();

    // Create circle geometry for the clip
    XpsPathGeometry clipGeom = doc.CreatePathGeometry("M250,250 A100,100 0 1 1 250,50 100,100 0 1 1 250,250");
    canvas2.Clip = clipGeom;

    // Create rectangle in this canvas and fill it
    XpsPath rect = canvas2.AddPath(rectGeom);
    rect.Fill = fill;

    // Add the second canvas with stroked rectangle to the main canvas
    XpsCanvas canvas3 = canvas1.AddCanvas();

    // Create rectangle in this canvas and stroke it
    rect = canvas3.AddPath(rectGeom);
    rect.Stroke = fill;
    rect.StrokeThickness = 2;

    // Save resultant XPS document
    doc.Save(dataDir + "output2.xps");



FAQ

1. Wie kann ich einer XPS-Datei eine Leinwand hinzufügen?

Legen Sie den Pfad zum Dokumentenverzeichnis fest. Um Leinwände hinzuzufügen, verwenden Sie die Methode AddCanvas().

2. Wie manipuliert man Canvas-Offsets in einer XPS-Datei?

Legen Sie den Pfad zum Dokumentenverzeichnis fest und fügen Sie Leinwände hinzu. Verwenden Sie die Methode CreateMatrix(), um Offsets vorzunehmen.

3. Welche Vorgänge mit Leinwänden in der XPS-Datei werden unterstützt?

Sie können eine Leinwand erstellen, ausschneiden und hinzufügen sowie deren Versätze bearbeiten.

XPS Was ist XPS Dateiformat

Das XPS-Format ähnelt dem PDF-Format. Beides sind Formate der Seitenbeschreibungssprache (PDL). EPS basiert auf HTML und nicht auf der PostScript-Sprache. Die .eps-Datei kann ein Markup der Dokumentstruktur zusammen mit Informationen darüber enthalten, wie das Dokument aussehen würde. Es gibt auch Anweisungen zum Drucken und Rendern des Dokuments. Das Merkmal des Formats ist, dass es die Beschreibung des Dokuments festlegt, was bedeutet, dass es gleich aussieht, egal wer und von welchem ​​​​Betriebssystem es öffnet.