PDF處理控件Aspose.PDF功能演示:使用C?;騐B.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?;騐B.NET應用程序中旋轉PDF文檔的所有頁面。同樣,可能有成千上萬的相關用例需要旋轉PDF文件。您可以按照以下步驟旋轉PDF文件的所有頁面:
- 加載輸入的PDF文檔
- 遍歷每個頁面
- 使用Rotation屬性旋轉PDF頁面
- 保存輸出PDF文件
下面的代碼段顯示了如何使用C?;騐B.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?;騐B.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);
還想要更多嗎?您可以點擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。