PDF处理控件Aspose.PDF功能演示:在C ++中以编程方式旋转 PDF 页面、文本或图像

  • 使用 C++ 旋转 PDF 页面
  • 使用 C++ 在 PDF 页面上旋转文本
  • 使用 C++ 在 PDF 页面上旋转图像


使用 C++ 旋转 PDF 页面

使用 Aspose.PDF for C++ 旋转页面轻而易举。只需几行代码,您就可以旋转 PDF 文档的所有页面。以下是使用 C++ 旋转 PDF 页面的步骤。

  • 使用Document 类加载源 PDF 文档。
  • 循环浏览文档页面。如果您只想旋转选定的页面,可以在此循环中定义该逻辑。
  • 使用Page->set_Rotate(Rotation value)方法旋转每个页面。
  • 使用Document->Save(System::String outputFileName, SaveFormat format) 方法保存输出 PDF 文件 。

以下示例代码显示了如何使用 C++ 旋转 PDF 页面。

// Open the source PDF documentauto pdfDocument = MakeObject(u"SourceDirectory\Sample 1.pdf");// Loop through the pagesfor (System::SharedPtrpage : pdfDocument->get_Pages()){// Rotate pagepage->set_Rotate(Aspose::Pdf::Rotation::on180);}// Save the PDF filepdfDocument->Save(u"OutputDirectory\Rotated_out.pdf", SaveFormat::Pdf);

使用 C++ 在 PDF 页面上旋转文本

在向 PDF 文档添加文本时,您可以更改许多文本属性,包括其角度。在以下示例中,我们将向 PDF 页面添加文本并将其旋转 45 度角。以下是实现这一目标的步骤。

  • 创建一个Document 类的实例来表示一个新的 PDF 文档。
  • 向 PDF 文档添加一个新页面。
  • 使用要添加的文本创建TextFragment类的实例。
  • 设置文本的位置、角度和其他属性。
  • 使用TextBuilder->AppendText(System::SharedPtrtextFragment)方法向 PDF 页面添加文本。
  • 使用Document->Save(System::String outputFileName, SaveFormat format) 方法保存输出 PDF 文件 。

以下示例代码演示了如何使用 C++ 旋转 PDF 页面上的文本。

// Create an instance of the Document classauto pdfDocument = MakeObject();// Add new pageSystem::SharedPtrpage = pdfDocument->get_Pages()->Add();// Create TextFragment objectSystem::SharedPtrtextFragment = MakeObject(u"Rotated Text");// Set the position of the texttextFragment->set_Position(MakeObject(200, 600));// Set text propertiestextFragment->get_TextState()->set_FontSize(12);textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));textFragment->get_TextState()->set_BackgroundColor(Aspose::Pdf::Color::get_LightGray());textFragment->get_TextState()->set_ForegroundColor(Aspose::Pdf::Color::get_Red());textFragment->get_TextState()->set_Rotation(45);textFragment->get_TextState()->set_Underline(true);// Create TextBuilder objectSystem::SharedPtrtextBuilder = MakeObject(page);// Add texc to the pagetextBuilder->AppendText(textFragment);// Save the PDF filepdfDocument->Save(u"OutputDirectory\Rotated_text_out.pdf", SaveFormat::Pdf);

使用 C++ 在 PDF 页面上旋转图像

在向 PDF 文档添加图像时,您可以设置许多属性,如高度、宽度、不透明度、旋转度等。在本示例中,我们将向 PDF 页面添加图像并将其旋转 90 度。以下是在 PDF 页面上旋转图像的步骤。

  • 使用Document 类加载源 PDF 文档。
  • 使用要添加的图像创建ImageStamp类的实例。
  • 设置图像的旋转和其他属性。
  • 使用Document->get_Pages()->idx_get(1)->AddStamp(System::SharedPtrstamp)方法将图像添加到 PDF 页面。
  • 使用Document->Save(System::String outputFileName, SaveFormat format) 方法保存输出 PDF 文件 。

以下示例代码显示了如何使用 C++ 旋转 PDF 页面上的图像。

// Open the source PDF documentauto pdfDocument = MakeObject(u"SourceDirectory\Sample 1.pdf");// Create ImageStamp objectSystem::SharedPtrimageStamp = MakeObject(u"SourceDirectory\aspose.png");// Set ImageStamp propertiesimageStamp->set_XIndent(250);imageStamp->set_YIndent(350);imageStamp->set_Height(100);imageStamp->set_Width(300);imageStamp->set_Rotate(Rotation::on90);imageStamp->set_Opacity(0.5);// Add ImageStamp to the pagepdfDocument->get_Pages()->idx_get(1)->AddStamp(imageStamp);// Save the PDF filepdfDocument->Save(u"OutputDirectory\Rotated_image_out.pdf", SaveFormat::Pdf);

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年5月1日
下一篇 2021年5月1日

相关推荐

发表回复

登录后才能评论