การทำงานกับการตัดและเส้นทางการตัดในเอกสาร PostScript (PS) ด้วย Java
กำหนด ใช้ และจัดการเส้นทางการตัดแบบเวกเตอร์ในเอกสาร PostScript (PS/EPS) โดยใช้ Aspose.Page for Java จำกัดการดำเนินการวาด การเรนเดอร์ข้อความ และการแสดงรูปภาพให้อยู่ภายในขอบเขตเรขาคณิตแบบปิดที่กำหนดเอง จัดการพื้นที่การตัดทั้งแบบเฉพาะที่และแบบส่วนกลางอย่างมีประสิทธิภาพโดยใช้สแต็กสถานะกราฟิกของ PostScript (writeGraphicsSave/writeGraphicsRestore) เพื่อสร้างเอฟเฟกต์ภาพแบบมาสก์ที่ซับซ้อน
Aspose.Page for Java ใช้ primitive java.awt.Shape ของ Java เพื่อแก้ไขเส้นทางการตัดปัจจุบันภายในสถานะการเรนเดอร์ของ PsDocument
- หากต้องการใช้การตัดที่ทำให้เส้นทางการตัดปัจจุบันตัดกับขอบเขตของรูปร่างเรขาคณิตที่กำหนด ให้ใช้เมธอด
PsDocument.writeGraphicsSave() - หากต้องการพุชสถานะกราฟิกปัจจุบัน ซึ่งรวมถึงขอบเขตการตัดที่ใช้งานอยู่ ลงในสแต็กสถานะ ให้ใช้เมธอด
PsDocument.writeGraphicsSave() - หากต้องการคืนค่าสถานะกราฟิก ซึ่งจะนำสถานะกราฟิกด้านบนสุดออกจากสแต็กและรีเซ็ตพื้นที่การตัดกลับไปเป็นสถานะก่อนหน้า ให้ใช้เมธอด
PsDocument.writeGraphicsRestore() - หากต้องการตัดรูปร่างโดยใช้เส้นทางเวกเตอร์แบบปิดมาตรฐานหรือซับซ้อนใด ๆ เป็นพื้นที่การตัด ให้ใช้
Rectangle2D,Ellipse2D,Path2D
ใช้การตัดด้วยรูปร่างอย่างง่าย (สี่เหลี่ยมผืนผ้าหรือวงรี)
จำกัดการดำเนินการวาดข้อความหรือเวกเตอร์ให้อยู่ภายในรูปร่างเรขาคณิตพื้นฐานอย่างเคร่งครัด
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
public class BasicClippingPS {
public static void main(String[] args) throws Exception {
// Initialize output stream and PS document
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/basic_clipping.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage(null);
// Define a clipping shape (Circle boundary)
Ellipse2D clipShape = new Ellipse2D.Float(100, 100, 200, 200);
// Save state before clipping
document.writeGraphicsSave();
// Apply clip shape
document.clip(clipShape);
// Draw a large rectangle that extends past the clip boundary
document.setPaint(Color.BLUE);
document.fill(new Rectangle2D.Float(50, 50, 300, 300)); // Only the circle area gets rendered
// Restore state to release clip
document.writeGraphicsRestore();
document.closePage();
document.save();
outStream.close();
System.out.println("Basic clip applied successfully to PostScript document.");
}
}จัดการพื้นที่การตัดแบบซ้อนกันด้วยสถานะกราฟิก
แยกขอบเขตการตัดเฉพาะที่ออกจากกัน และป้องกันไม่ให้การตัดคงอยู่ระหว่างองค์ประกอบต่าง ๆ ของหน้า โดยครอบการดำเนินการตัดไว้ภายในบล็อก writeGraphicsSave() และ writeGraphicsRestore()
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
public class NestedClippingPS {
public static void main(String[] args) throws Exception {
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/nested_clipping.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage(null);
// Outer graphic state
document.writeGraphicsSave();
// Intersect First Clip (Outer Box)
document.clip(new Rectangle2D.Float(100, 100, 200, 200));
// Inner graphic state
document.writeGraphicsSave();
// Intersect Second Clip (Inner Box - creates combined intersection)
document.clip(new Rectangle2D.Float(150, 150, 200, 200));
// Fill combined intersection region
document.setPaint(Color.RED);
document.fill(new Rectangle2D.Float(0, 0, 500, 500));
// Restore back to single outer box clip
document.writeGraphicsRestore();
// Restore back to unclipped original page state
document.writeGraphicsRestore();
document.closePage();
document.save();
outStream.close();
System.out.println("Nested clip states processed successfully.");
}
}ตัดข้อความที่ซับซ้อนและเส้นทางเวกเตอร์
ใช้รูปร่างแบบกำหนดเองที่สร้างด้วย Path2D เป็นมาสก์แบบไดนามิกสำหรับพื้นหลังที่มีลวดลายหรือหลายสี
import com.aspose.page.eps.PsDocument;
import com.aspose.page.eps.device.PsSaveOptions;
import java.awt.Color;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
public class ComplexPathClippingPS {
public static void main(String[] args) throws Exception {
FileOutputStream outStream = new FileOutputStream("C:/PSProject/Output/complex_clip.ps");
PsSaveOptions options = new PsSaveOptions();
PsDocument document = new PsDocument(outStream, options, 1);
document.openPage(null);
// Construct a custom triangle path for clipping
Path2D triangleClip = new Path2D.Float();
triangleClip.moveTo(250, 100);
triangleClip.lineTo(400, 350);
triangleClip.lineTo(100, 350);
triangleClip.closePath();
document.writeGraphicsSave();
// Apply custom path clip
document.clip(triangleClip);
// Render overlapping colored stripes inside the triangle boundary
Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW };
for (int i = 0; i < colors.length; i++) {
document.setPaint(colors[i]);
document.fill(new Rectangle2D.Float(100, 100 + (i * 60), 300, 50));
}
document.writeGraphicsRestore();
document.closePage();
document.save();
outStream.close();
System.out.println("Complex vector path clip applied successfully.");
}
}การติดตั้งและการตั้งค่า
เพิ่ม Aspose.Page for Java ลงใน Maven ของคุณ:
Package Manager Console Command
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-page</artifactId>
<version>Latest</version>
</dependency>
FAQ
1. PsDocument.clip(Shape) เปลี่ยนแปลงพื้นที่คลิปปัจจุบันอย่างไร?
การเรียกใช้ clip(Shape) จะทำการตัดกันระหว่างรูปร่างที่ระบุกับเส้นทางคลิปที่มีอยู่ในบริบทการทำงานของ PostScript โดยจะไม่แทนที่เส้นทางคลิปทั้งหมด เว้นแต่จะกู้คืนสถานะก่อน
2. ฉันจะลบหรือรีเซ็ตเส้นทางคลิปใน PostScript ได้อย่างไร?
เนื่องจาก PostScript สะสมการคลิปโดยใช้การตัดกัน จึงไม่สามารถ ‘ลบ’ เส้นทางคลิปได้โดยตรง แต่ให้บันทึกสถานะด้วย document.writeGraphicsSave() ก่อนใช้คลิป และกู้คืนสถานะด้วย document.writeGraphicsRestore() เมื่อเสร็จสิ้น
3. ฉันสามารถใช้รูปร่างใด ๆ เช่น เส้นขอบข้อความหรือเส้นทางโค้งสำหรับการคลิปได้หรือไม่?
ได้ การใช้งาน Java java.awt.Shape ใด ๆ รวมถึง Path2D, รูปร่าง Bézier แบบโค้ง และเส้นขอบเวกเตอร์ของข้อความที่แปลงแล้ว สามารถส่งไปยัง document.clip() ได้โดยตรง