PDF处理控件Aspose.PDF功能演示:使用C#或VB.NET旋转PDF页面,文本或图像

在本文中,让我们探讨与PDF文档旋转相关的场景,可以在基于.NET框架的应用程序中使用C#或VB.NET以编程方式旋转整个页面或PDF页面内容,包括文本或图像。

此外,本文将在简单而基本的PDF旋转功能示例的帮助下,浏览以下PDF页面,图像或文本旋转方案:

  • 使用C#旋转PDF文档的所有页面
  • 使用C#旋转PDF的特定页面
  • 使用C#旋转PDF文档上的文本
  • 使用C#在PDF上旋转图像

目前,.NET版Aspose.PDF升级到v20.10版,新增支持ZUGFeRD附件,优化添加签名功能吗,修复XPS到PDF转换异常等诸多Bug问题,感兴趣的朋友可点击下方按钮下载最新版。

使用C#旋转PDF文档的所有页面

让我们假设通过扫描某些文档(以特定角度扫描所有图像)创建的PDF文档。就像所有页面都是上下颠倒一样,您需要在C#或VB.NET应用程序中旋转PDF文档的所有页面。同样,可能有成千上万的相关用例需要旋转PDF文件。您可以按照以下步骤旋转PDF文件的所有页面:

  • 加载输入的PDF文档
  • 遍历每个页面
  • 使用Rotation属性旋转PDF页面
  • 保存输出PDF文件

下面的代码段显示了如何使用C#或VB.NET旋转PDF文件的所有页面:

// Load input PDF document
Document document = new Document(dataDir + "Rotate.pdf");

// Iterate through each page of PDF
foreach(Page page in document.Pages)
{
    // Rotate the PDF document on desired angle
    page.Rotate = Rotation.on180;
}

// Save output rotated PDF file
document.Save(dataDir + "Rotated.pdf");

使用C#旋转PDF的特定页面

PDF文档中的旋转应用于页面级别。因此,您还可以根据需要旋转PDF文件的特定页面。您只需要选择要应用旋转的页码即可。以下步骤说明了如何旋转PDF文件的某些页面:

  • 加载输入的PDF文档
  • 指定要轮换的页码
  • 遍历某些页码
  • 以特定角度旋转页面
  • 保存输出PDF文件

以下代码段阐述了如何使用C#或VB.NET旋转PDF文档中的特定页面或特定页面:

// Load input PDF document
Document document = new Document(dataDir + "Rotate.pdf");

// Specify the page numbers you want to apply rotation on
int[] pages = { 1, 3, 7 };

// Iterate through particular pages 
foreach (Page page in document.Pages)
{
    foreach (int match in pages)
    {
        if (page.Number == match)
        {
            // Rotate the page
            page.Rotate = Rotation.on90;
        }
    }
}

// Save rotated PDF document
document.Save(dataDir + "Rotated.pdf");

使用C#旋转PDF文档上的文本

在PDF文档中添加文本时,可以旋转不同角度的文本。在PDF文档中添加一些水印文本时,此文本旋转可能更相关。让我们在页面上的特定坐标处添加一些文本,然后将文本对角旋转45度。

  • 初始化Document类的对象
  • 在PDF文档中添加空白页
  • 创建新的TextFragment对象
  • 在页面的特定坐标处添加文本
  • 附加文字并保存输出的PDF文件

下面的代码段显示了如何使用C#或VB.NET旋转PDF文档中的文本:

// Initialize document
Document pdfDocument = new Document();
// Get particular page
Page pdfPage = pdfDocument.Pages.Add();

// Create text fragment
TextFragment tf = new TextFragment("Rotated text");

// Add text at specific loaction on the page
tf.Position = (new Position(200, 600));

// Set text properties
tf.TextState.FontSize = 12;
tf.TextState.Font = FontRepository.FindFont("TimesNewRoman");
tf.TextState.BackgroundColor = Aspose.Pdf.Color.LightGray;
tf.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
tf.TextState.Rotation = 45;
tf.TextState.Underline = true;

// Create TextBuilder object
TextBuilder textBuilder = new TextBuilder(pdfPage);
// Append the text fragment to the PDF page
textBuilder.AppendText(tf);
// Save document
pdfDocument.Save(dataDir + "Text_Rotated.pdf");

使用C#在PDF上旋转图像

可以在PDF文档中添加或插入图像时旋转PDF文档中的图像。当您要更新或更改图像方向时,这可能会很有帮助。您可以按照以下步骤在PDF页面上旋转图像:

  • 加载输入的PDF文档
  • 创建ImageStamp类的实例
  • 设置包括旋转在内的不同属性
  • 保存输出PDF文件

以下代码演示了如何使用C#或VB.NET以编程方式旋转PDF文档中的图像或图片:

// Open document
Document pdfDocument = new Document(dataDir + "Image.pdf");

// Create image stamp
ImageStamp imageStamp = new ImageStamp(dataDir + "Image.jpg");
imageStamp.XIndent = 100;
imageStamp.YIndent = 100;
imageStamp.Height = 300;
imageStamp.Width = 300;
imageStamp.Rotate = Rotation.on90;
imageStamp.Opacity = 0.5;
// Add stamp to particular page
pdfDocument.Pages[1].AddStamp(imageStamp);

dataDir = dataDir + "RotatedImage.pdf";
// Save output document
pdfDocument.Save(dataDir);

猜你喜欢

转载自blog.csdn.net/m0_67129275/article/details/132402521