3分鐘學會使用Aspose.Page在C++上創建編輯PostScript和XPS文檔!


XPS是工作中常用的一種微軟的文檔保存和查看格式,

針對XPS、EPS格式的管理控件Aspose.Page已經推出C++版,將能夠在基於C ++的應用程序中以編程方式創建,讀取,編輯,保存和轉換XPS文檔,該API還允許您處理XPS文檔中的頁面和元素,例如畫布和字形。此外,它支持將文檔轉換為PDF和光柵圖像。你可以點擊文末“瞭解更多”下載最新版測試體驗。與此同時,.NET版和Java版Aspose.Page已更新至v20.3最新版,修復將圖像添加到XPS文件時發生的異常。

本文演示瞭如何使用Aspose.Page for C ++執行與PostScript和XPS文檔有關的以下操作。

  • 用C ++創建一個新的XPS文檔
  • 在C ++中編輯現有的XPS文檔
  • 將頁面或文檔添加到C ++中的XPS文檔中

用C ++創建XPS文檔

以下是使用Aspose.Page for C ++創建包含文本和圖像的XPS文檔的簡單步驟。

  • 創建XpsDocument類的對象。
  • 分別使用XpsGlyphs和XpsPath對象添加文本和圖像。
  • 使用保存方法保存文檔。

下面的代碼示例演示如何使用C ++創建XPS文檔。

<code>// Create a new XpsDocument object
auto doc = System::MakeObject<xpsdocument>();

// Add Image
System::SharedPtr<xpspath> path = doc->AddPath(doc->CreatePathGeometry(u"M 30,20 l 450.24,0 0,150.64 -350.24,0 Z"));
// Create a matrix that can be used for proper positioning.
path->set_RenderTransform(doc->CreateMatrix(0.7f, 0.f, 0.f, 0.7f, 0.f, 20.f));
// Create Image Brush
path->set_Fill(doc->CreateImageBrush(u"QL_logo_color.tif", System::Drawing::RectangleF(0.f, 0.f, 450.24f, 150.64f), System::Drawing::RectangleF(50.f, 20.f, 450.68f, 150.48f)));

// Text as footer
System::SharedPtr<xpssolidcolorbrush> textFill = doc->CreateSolidColorBrush(System::Drawing::Color::get_Black());
System::SharedPtr<xpsglyphs> glyphs = doc->AddGlyphs(u"Arial", 18.0f, System::Drawing::FontStyle::Regular, 40.f, 1015.f, u"Copyright © 2006 QualityLogic, Inc.");
glyphs->set_Fill(textFill);
glyphs = doc->AddGlyphs(u"Arial", 15.0f, System::Drawing::FontStyle::Regular, 475.f, 1003.f, u"For information on QualityLogic XPS test products,");
glyphs->set_Fill(textFill);

// Save as XPS
doc->Save(u"Create-XPS.xps");/<xpsglyphs>/<xpssolidcolorbrush>/<xpspath>/<xpsdocument>/<code>

輸出結果

3分鐘學會使用Aspose.Page在C++上創建編輯PostScript和XPS文檔!

  • 創建XpsDocument類的對象,並使用XPS文檔的路徑對其進行初始化。
  • 使用XpsDocument對象訪問文檔的元素。
  • 使用保存方法保存更新的文檔。

下面的代碼示例演示如何使用C ++編輯現有的XPS文檔。

<code>// Load XPS Document
auto doc = System::MakeObject(u"Created-XPS.xps");
// Add empty page at end of pages list
doc->AddPage();
// Insert an empty page at beginning of pages list
doc->InsertPage(1, true);
// Save XPS file
doc->Save(u"Updated-XPS.xps");/<code>

在C ++中將頁面和文檔添加到XPS

Aspose.Page for C ++還允許您在XPS文檔中添加頁面以及多個文檔。以下是創建新XPS文檔並添加其他頁面和文檔的步驟。

  • 創建XpsDocument類的對象。
  • 使用AddPage和AddDocument方法分別添加頁面和文檔。
  • 使用SelectActiveDocument方法選擇要處理的活動文檔。
  • 在文檔中添加文本或圖像。
  • 保存文檔。

下面的代碼示例演示如何使用C ++將其他頁面和文檔添加到XPS。

<code>// New document (1 fixed document with 1 default size page)
auto doc = System::MakeObject();
// Add 2nd page on 1st document and set active
doc->AddPage();
// Add 2nd document with 1 page (3rd page in file)
doc->AddDocument(false);
// 1st document's 2nd page is still active
System::SharedPtrtextFill = doc->CreateSolidColorBrush(System::Drawing::Color::get_Black());
System::SharedPtrglyphs = doc->AddGlyphs(u"Arial", 12.0f, System::Drawing::FontStyle::Regular, 200.f, 500.f, u"Text on Page 2 (Document 1),");
glyphs->set_Fill(textFill);
// Activate 2nd document
doc->SelectActiveDocument(2);
glyphs = doc->AddGlyphs(u"Arial", 12.0f, System::Drawing::FontStyle::Regular, 200.f, 500.f, u"Text on Document 2 (Page #3 in file),");
glyphs->set_Fill(textFill);
// Save XPS file
doc->Save(u"Create-XPS.xps");/<code>

如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和諮詢。


分享到:


相關文章: