การดำเนินการข้ามแพ็กเกจภายใน XPS Package

จัดการหน้า สี และ Glyph ภายใน XPS Package ผ่าน C#

 

Aspose.Page API Solution สำหรับ .NET ท่ามกลางรูปแบบอื่นๆ ประกอบด้วย XPS Package เป็นไลบรารีแยกต่างหากสำหรับทำงานกับไฟล์ XPS ฟังก์ชันการทำงานที่หลากหลายประกอบด้วยคุณสมบัติที่มีประโยชน์และเป็นที่นิยมมากมาย เช่น การรวมไฟล์ การแปลง การทำงานกับกราฟิก ฯลฯ

XPS สามารถเก็บไฟล์ได้หลายไฟล์ในเอกสารเดียว ดังนั้น XPS Package ใดๆ ควรมีฟังก์ชันการทำงานเพื่อจัดการไฟล์เหล่านั้นและหน้าของไฟล์เหล่านั้นภายในเอกสารและระหว่างเอกสาร XPS ที่แตกต่างกัน การจัดการดังกล่าวเรียกว่าการดำเนินการข้ามแพ็กเกจ ซึ่งควรจะอธิบายแยกกัน

ที่นี่คุณจะพบตัวอย่างการดำเนินการข้ามแพ็กเกจ เช่น การจัดการหน้า และการเพิ่ม Glyph และสี

ขั้นตอนในการจัดการหน้าภายใน XPS Package ด้วย C#

  1. ตั้งค่าเส้นทางไปยังไดเรกทอรีเอกสาร
  2. สร้างไฟล์ XPS โดยใช้ XpsDocument Class
  3. หากต้องการแทรกหน้าที่ใช้งานอยู่จากเอกสารหนึ่งไปยังจุดเริ่มต้นของอีกเอกสารหนึ่ง ให้ใช้เมธอด InsertPage()
  4. หากต้องการแทรกหน้าที่ใช้งานอยู่จากเอกสารหนึ่งไปยังส่วนท้ายของอีกเอกสารหนึ่ง ให้ใช้เมธอด AddPage()
  5. หากต้องการลบหน้าที่ว่างเปล่า ให้ใช้เมธอด RemovePage()
  6. หากต้องการย้ายหน้าจากเอกสารหนึ่งไปยังอีกเอกสารหนึ่ง ให้ใช้เมธอด InsertPage() และ SelectActivePage()
  7. บันทึกเอกสาร XPS ที่เปลี่ยนแปลงโดยใช้ XPsDocument.Save

โค้ด C# สำหรับการจัดการหน้าข้ามแพ็กเกจ

    using Aspose.Page.XPS;
    using Aspose.Page.XPS.XpsModel;
    using System.Drawing;
    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCrossPackageOperations();

    // Create the first XPS Document
    XpsDocument doc1 = new XpsDocument(dataDir + "input1.xps");

    // Create the second XPS Document
    XpsDocument doc2 = new XpsDocument(dataDir + "input2.xps");

    // Create the third XPS Document
    XpsDocument doc3 = new XpsDocument(dataDir + "input3.xps");

    // Create the fourth XPS Document
    XpsDocument doc4 = new XpsDocument();

    // Insert the active page (1 in this case) from the second document to the beginning of the fourth document
    doc4.InsertPage(1, doc2.Page, false);

    // Insert the active page (1 in this case) from the third document to the end of the fourth document
    doc4.AddPage(doc3.Page, false);

    // Remove page 2 from the fourth document. This is an empty page that was created when the document had been created.
    doc4.RemovePageAt(2);

    // Insert page 3 from the first document to the second postion of the fourth document
    doc4.InsertPage(2, doc1.SelectActivePage(3), false);

    // Save the fourth XPS document
    doc4.Save(dataDir + "out.xps");

ขั้นตอนในการเพิ่ม Glyph Clone ภายใน XPS Package ด้วย C#

  1. ตั้งค่าเส้นทางไปยังไดเรกทอรีเอกสาร
  2. เปิดสตรีมของไฟล์ XPS
  3. สร้างไฟล์ XPS โดยใช้ XpsDocument Class
  4. เพิ่ม Glyph ลงในเอกสารโดยใช้เมธอด AddGlyphs()
  5. สร้างไฟล์ XPS ไฟล์ที่สองโดยใช้ XpsDocument Class
  6. หากต้องการโคลน Glyph จากไฟล์แรกไปยังไฟล์ที่สอง ให้ใช้เมธอด Add() และ Clone()
  7. บันทึกเอกสาร XPS ทั้งสองฉบับโดยใช้เมธอด XPsDocument.Save()

โค้ด C# เพื่อคัดลอก Glyph ภายใน XPS Package

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCrossPackageOperations();

    // Create the first XPS Document
    XpsDocument doc1 = new XpsDocument();

    // Add glyphs to the first document
    XpsGlyphs glyphs = doc1.AddGlyphs("Times New Roman", 200, FontStyle.Bold, 50, 250, "Test");

    // Fill glyphs in the first document with one color
    glyphs.Fill = doc1.CreateSolidColorBrush(Color.Green);

    // Create the second XPS Document
    XpsDocument doc2 = new XpsDocument();

    // Add glyphs cloned from the one's from the first document
    glyphs = doc2.Add(glyphs.Clone());

    // Fill glyphs in the second document with another color
    ((XpsSolidColorBrush)glyphs.Fill).Color = doc2.CreateColor(Color.Red);

    // Save the first XPS document
    doc1.Save(dataDir + "out1.xps");

    // Save the second XPS document
    doc2.Save(dataDir + "out2.xps");

ขั้นตอนในการเพิ่ม Glyph ที่เติมด้วยรูปภาพด้วย C#

  1. ตั้งค่าเส้นทางไปยังไดเรกทอรีเอกสาร
  2. เปิดสตรีมของไฟล์ XPS
  3. สร้างไฟล์ XPS โดยใช้ XpsDocument Class
  4. เพิ่ม Glyph ลงในเอกสารโดยใช้เมธอด AddGlyphs()
  5. หากต้องการเติม Glyph ด้วย Image Brush ให้ใช้เมธอด CreateImageBrush()
  6. สร้างไฟล์ XPS ไฟล์ที่สองโดยใช้ XpsDocument Class
  7. เพิ่ม Glyph พร้อมฟอนต์จากเอกสารแรกไปยังเอกสารที่สองโดยใช้เมธอด AddGlyphs()
  8. สร้าง Image Brush จากการเติมของเอกสารแรกและเติม Glyph ในเอกสารที่สองโดยใช้เมธอด CreateImageBrush()
  9. บันทึกเอกสาร XPS ทั้งสองฉบับโดยใช้เมธอด XPsDocument.Save()

โค้ด C# เพื่อสร้าง Glyph ที่เติมด้วยรูปภาพภายใน XPS Package

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCrossPackageOperations();

    // Create the first XPS Document
    XpsDocument doc1 = new XpsDocument();

    // Add glyphs to the first document
    XpsGlyphs glyphs1 = doc1.AddGlyphs("Times New Roman", 200, FontStyle.Bold, 50, 250, "Test");

    // Fill the glyphs with an image brush
    glyphs1.Fill = doc1.CreateImageBrush(dataDir + "R08SY_NN.tif", new RectangleF(0f, 0f, 128f, 192f),
        new RectangleF(0f, 0f, 64f, 96f));
    ((XpsImageBrush)glyphs1.Fill).TileMode = XpsTileMode.Tile;

    // Create the second XPS Document
    XpsDocument doc2 = new XpsDocument();

    // Add glyphs with the font from the first document to the second document
    XpsGlyphs glyphs2 = doc2.AddGlyphs(glyphs1.Font, 200, 50, 250, "Test");

    // Create an image brush from the fill of the the first document and fill glyphs in the second document
    glyphs2.Fill = doc2.CreateImageBrush(((XpsImageBrush)glyphs1.Fill).Image, new RectangleF(0f, 0f, 128f, 192f),
        new RectangleF(0f, 0f, 128f, 192f));
    ((XpsImageBrush)glyphs2.Fill).TileMode = XpsTileMode.Tile;

    // Save the first XPS document
    doc1.Save(dataDir + "out1.xps");

    // Save the second XPS document
    doc2.Save(dataDir + "out2.xps");



คำถามที่พบบ่อย

1. แพ็คเกจ XPS คืออะไร?

XPS Package เป็นไลบรารีแยกต่างหากสำหรับจัดการไฟล์ XPS ใช้เพื่อสร้างตัวแปลง โปรแกรมอ่าน หรือแอปอื่นๆ ของคุณเองเพื่อแก้ไข XPS

2. ฉันจะรับแพ็คเกจ XPS ได้อย่างไร

แพ็คเกจ XPS รวมอยู่ในโซลูชัน Aspose.Page

3. มีการดำเนินการข้ามแพ็คเกจอะไรบ้าง?

การใช้ Aspose XPS Package ทำให้คุณสามารถถ่ายโอนหน้าจากเอกสารหนึ่งไปยังอีกเอกสารหนึ่ง โคลนออบเจ็กต์ เช่น สัญลักษณ์ สไตล์ หรือการตั้งค่า

4. วิธีจัดการหน้าระหว่างเอกสาร XPS

หากต้องการถ่ายโอนไฟล์ด้วยแพ็คเกจ XPS นี้ ให้ใช้เมธอด InsertPage(), AddPage(), RemovePage() และ SelectActivePage() ของคลาส XpsDocument

XPS XPS รูปแบบไฟล์คืออะไร

XPS (XML Paper Specification) เป็นรูปแบบทางเลือกของ Microsoft ต่อ PDF พื้นฐานบน XML/HTML รักษาเลย์เอาต์ข้ามแพลตฟอร์มและไม่ขึ้นกับระบบปฏิบัติการ