XPS 내에서 캔버스(canvases)로 작업하기
C++ API를 사용하여 XPS 파일에서 캔버스를 클리핑(clip)하고 변환(transform)하기
XPS 파일에서 캔버스(canvas)는 텍스트, 이미지, 도형 같은 시각적 콘텐츠를 그릴 수 있는 페이지의 직사각형 영역입니다. 이 요소들의 컨테이너 역할을 하며 구성하고 레이어링(layering)하기 위한 프레임워크를 제공합니다. 캔버스는 문서의 다른 부분이나 여러 문서에서 재사용할 수 있어 XPS 문서 내에서 콘텐츠를 유연하게 구조화하고 구성할 수 있습니다. XPS 파일의 캔버스 주요 특성:
- 캔버스는 다른 캔버스 안에 중첩(nested)하여 복잡한 레이아웃(layouts)과 요소 그룹화를 가능하게 하는 계층 구조를 만들 수 있습니다.
- 캔버스는 이동(translation), 회전(rotation) 및 크기 조정(scaling)과 같은 다양한 작업을 사용하여 변환(transform)할 수 있습니다. 이를 통해 콘텐츠의 유연한 위치 지정 및 크기 조정이 가능합니다.
- 캔버스는 특정 영역으로 클리핑(clip)할 수 있어 콘텐츠의 표시 영역을 제한합니다. 이는 복잡한 도형 및 효과를 만드는 데 유용합니다.
- 캔버스에는 텍스트 런(text runs), 단락(paragraphs) 및 텍스트 상자(text boxes)를 포함한 다양한 시각적 요소가 포함될 수 있습니다. 이미지, 기하학적 도형 및 다른 캔버스도 포함될 수 있습니다.
문서에서 캔버스를 관리하는 것은 C++용 Aspose.Page에서 제공하는 기능 중 하나입니다. 이 솔루션은 다양한 페이지 설명 언어, 특히 XPS와 함께 작동합니다.
XPS 파일의 캔버스를 변환(transform)하려면 아래 가이드를 따르세요:
- XpsDocument 클래스를 사용하여 XPS 파일을 만듭니다.
- AddCanvas() 메서드를 사용하여 모든 페이지 요소의 공통 기본 캔버스를 만듭니다.
- CreateMatrix() 메서드를 사용하여 기본 캔버스에서 왼쪽 및 위쪽 오프셋을 만듭니다.
- CreatePathGeometry() 메서드로 직사각형 경로 지오메트리(rectangle path geometry)를 만듭니다.
- XpsBrush 클래스를 사용하여 직사각형에 대한 채우기(fill)를 만듭니다.
- 캔버스 2에 직사각형을 만들고 채우려면 XpsPath 클래스를 사용합니다.
- 캔버스 3을 이동하여 이전 직사각형 아래에 새 직사각형을 배치하려면 CreateMatrix() 메서드를 사용합니다.
- 이 캔버스를 페이지 오른쪽으로 이동하려면 Translate() 메서드를 사용합니다.
- 캔버스 4를 크기 조정(scale)하려면 Scale() 메서드를 호출합니다.
- 캔버스 5를 한 점을 중심으로 45도 회전하려면 RotateAround() 메서드가 유용합니다.
- XpsDocument.Save() 메서드를 사용하여 변경된 XPS 문서를 저장합니다.
캔버스 변환(canvas transformation)
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_WorkingWithCanvas();
// Create new XPS Document
System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
// Create main canvas, common for all page elemnts
System::SharedPtr<XpsCanvas> canvas1 = doc->AddCanvas();
// Make left and top offsets in the main canvas
canvas1->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 20.0f, 10.0f));
// Create rectangle path geometry
System::SharedPtr<XpsPathGeometry> rectGeom = doc->CreatePathGeometry(u"M 0,0 L 200,0 200,100 0,100 Z");
// Create a fill for rectangles
System::SharedPtr<XpsBrush> fill = doc->CreateSolidColorBrush(doc->CreateColor(12, 15, 159));
// Add new canvas without any transformations to the main canvas
System::SharedPtr<XpsCanvas> canvas2 = canvas1->AddCanvas();
// Create rectangle in this canvas and fill it
System::SharedPtr<XpsPath> rect = canvas2->AddPath(rectGeom);
rect->set_Fill(fill);
// Add new canvas with translate transformation to the main canvas
System::SharedPtr<XpsCanvas> canvas3 = canvas1->AddCanvas();
// Translate this canvas to position new rectangle below previous rectnagle
canvas3->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 200.0f));
// Translate this canvas to right side of page
canvas3->get_RenderTransform()->Translate(500.0f, 0.0f);
// Create rectangle in this canvas and fill it
rect = canvas3->AddPath(rectGeom);
rect->set_Fill(fill);
// Add new canvas with double scale transformation to the main canvas
System::SharedPtr<XpsCanvas> canvas4 = canvas1->AddCanvas();
// Translate this canvas to position new rectangle below previous rectnagle
canvas4->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 400.0f));
// Scale this canvas
canvas4->get_RenderTransform()->Scale(2.0f, 2.0f);
// Create rectangle in this canvas and fill it
rect = canvas4->AddPath(rectGeom);
rect->set_Fill(fill);
// Add new canvas with rotation around a point transformation to the main canvas
System::SharedPtr<XpsCanvas> canvas5 = canvas1->AddCanvas();
// Translate this canvas to position new rectangle below previous rectnagle
canvas5->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 800.0f));
// Rotate this canvas aroud a point on 45 degrees
canvas5->get_RenderTransform()->RotateAround(45.0f, System::Drawing::PointF(100.0f, 50.0f));
rect = canvas5->AddPath(rectGeom);
rect->set_Fill(fill);
// Save resultant XPS document
doc->Save(dataDir + u"output/" + u"output1.xps");XPS 파일의 캔버스를 클리핑(clip)하려면 아래 가이드를 따르세요:
- XpsDocument 클래스를 사용하여 XPS 파일을 만들거나 엽니다.
- AddCanvas() 메서드를 사용하여 모든 페이지 요소의 공통 기본 캔버스를 만듭니다.
- CreateMatrix() 메서드를 사용하여 기본 캔버스에서 왼쪽 및 위쪽 오프셋을 만듭니다.
- CreatePathGeometry() 메서드로 직사각형 경로 지오메트리를 만듭니다.
- XpsBrush 클래스를 사용하여 직사각형에 대한 채우기를 만듭니다.
- 기본 캔버스에 클립이 있는 다른 캔버스를 추가하려면 AddCanvas() 메서드를 다시 호출합니다.
- XpsPathGeometry 클래스를 사용하여 클립용 원 지오메트리를 만듭니다.
- 이 캔버스에 직사각형을 만들고 채우려면 XpsPath 클래스를 사용합니다.
- AddCanvas() 메서드로 다른 캔버스를 추가한 다음, 이 캔버스에 직사각형을 만들고 XpsPathGeometry 클래스로 스트로크(stroke)를 적용합니다.
- XpsDocument.Save() 메서드를 사용하여 변경된 XPS 문서를 저장합니다.
캔버스 클리핑(canvas clipping)
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_WorkingWithCanvas();
// Create new XPS Document
System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
// Create main canvas, common for all page elemnts
System::SharedPtr<XpsCanvas> canvas1 = doc->AddCanvas();
// Make left and top offsets in the main canvas
canvas1->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 20.0f, 10.0f));
// Create rectangle path geometry
System::SharedPtr<XpsPathGeometry> rectGeom = doc->CreatePathGeometry(u"M 0,0 L 500,0 500,300 0,300 Z");
// Create a fill for rectangles
System::SharedPtr<XpsBrush> fill = doc->CreateSolidColorBrush(doc->CreateColor(12, 15, 159));
// Add another canvas with clip to the main canvas
System::SharedPtr<XpsCanvas> canvas2 = canvas1->AddCanvas();
// Create circle geometry for clip
System::SharedPtr<XpsPathGeometry> clipGeom = doc->CreatePathGeometry(u"M250,250 A100,100 0 1 1 250,50 100,100 0 1 1 250,250");
canvas2->set_Clip(clipGeom);
// Create rectangle in this canvas and fill it
System::SharedPtr<XpsPath> rect = canvas2->AddPath(rectGeom);
rect->set_Fill(fill);
// Add the second canvas with stroked rectangle to the main canvas
System::SharedPtr<XpsCanvas> canvas3 = canvas1->AddCanvas();
// Create rectangle in this canvas and stroke it
rect = canvas3->AddPath(rectGeom);
rect->set_Stroke(fill);
rect->set_StrokeThickness(2.0f);
// Save resultant XPS document
doc->Save(dataDir + u"output/" + u"output2.xps");
XPS XPS 파일 형식이란 무엇입니까?
XPS (XML Paper Specification) は、Microsoft が提供する PDF の代替フォーマットです。XML/HTML ベースで、プラットフォームに依存せずレイアウトを保持します。