如何在ASP.NET MVC中生成PDF:IronPDF与iTextSharp比较?
两个布局问题在文档工作流程中不断出现。 宽的内容,如财务表格、仪表板和项目时间线,在被迫纵向显示时会被剪切或换行。 扫描或导入的页面通常是侧着或倒过来的。 IronPDF在C#中解决了这两个问题,在PDF渲染时设置方向并旋转已存在的文档页面。
业务问题
一个报告系统输出的类似电子表格的表溢出了纵向页面。 文档接收过程中收到的扫描件方向错误。 从多个来源组合而成的最终报告页面角度不一致。 每种情况都让用户必须在查看器中眯眼看、横向滚动或手动旋转页面。 目标是自动生成正确且一致的布局。
在渲染时设置方向
通过PaperOrientation属性在从其他格式生成PDF时选择方向。 横向为宽的内容提供了所需的水平空间。
using IronPdf;
using IronPdf.Rendering;
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;
PdfDocument pdf = renderer.RenderHtmlAsPdf(reportHtml);
pdf.SaveAs("report-landscape.pdf");
using IronPdf;
using IronPdf.Rendering;
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;
PdfDocument pdf = renderer.RenderHtmlAsPdf(reportHtml);
pdf.SaveAs("report-landscape.pdf");
Imports IronPdf
Imports IronPdf.Rendering
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(reportHtml)
pdf.SaveAs("report-landscape.pdf")
旋转现有文档的页面
旋转适用于已创建的文档,使其成为纠正扫描或规范化合并文件的工具。 IronPDF通过PdfPageRotation枚举提供四个固定角度,并为一个页面、选定集或所有页面提供方法。
PdfDocument pdf = PdfDocument.FromFile("scanned.pdf");
// Correct every page in a uniformly misrotated batch
pdf.SetAllPageRotations(PdfPageRotation.Clockwise90);
// Fix a single page
pdf.SetPageRotation(1, PdfPageRotation.Clockwise180);
// Fix a selected set of pages
pdf.SetPageRotations(new List<int> { 0, 3 }, PdfPageRotation.Clockwise270);
pdf.SaveAs("corrected.pdf");
PdfDocument pdf = PdfDocument.FromFile("scanned.pdf");
// Correct every page in a uniformly misrotated batch
pdf.SetAllPageRotations(PdfPageRotation.Clockwise90);
// Fix a single page
pdf.SetPageRotation(1, PdfPageRotation.Clockwise180);
// Fix a selected set of pages
pdf.SetPageRotations(new List<int> { 0, 3 }, PdfPageRotation.Clockwise270);
pdf.SaveAs("corrected.pdf");
Imports System.Collections.Generic
Dim pdf As PdfDocument = PdfDocument.FromFile("scanned.pdf")
' Correct every page in a uniformly misrotated batch
pdf.SetAllPageRotations(PdfPageRotation.Clockwise90)
' Fix a single page
pdf.SetPageRotation(1, PdfPageRotation.Clockwise180)
' Fix a selected set of pages
pdf.SetPageRotations(New List(Of Integer) From {0, 3}, PdfPageRotation.Clockwise270)
pdf.SaveAs("corrected.pdf")
GetPageRotation读取任何页面的当前角度,这有助于规范化从多个来源合并的文档。
PdfPageRotation rotation = pdf.GetPageRotation(1);
PdfPageRotation rotation = pdf.GetPageRotation(1);
Dim rotation As PdfPageRotation = pdf.GetPageRotation(1)
需要计划的要点
- 渲染时间与现有文件:
PaperOrientation仅在从其他格式生成PDF时适用。 旋转适用于已经存在的PDF。保持两者分开可以避免最常见的错误。 - 整页旋转:旋转将文本、图像和注释一起移动,这不同于旋转单个文本元素。
- 零基索引:
SetPageRotation(1, ...)定位第二页。 - 保留原件:将旋转后的输出保存为新文件名,并使用
SetPageRotations针对多页以提高性能。
结果
在渲染时设置方向并随后应用旋转,团队可以生成适合其内容和输入管道的报告,并存储方向正确的文档,无需手动清理。完整的方法细节在页面方向和旋转指南中。
常见问题解答
如何使用IronPDF将PDF的方向设置为横向?
您可以通过使用`ChromePdfRenderer`类的`PaperOrientation`属性在渲染过程中将PDF的方向设置为横向。简单地在渲染文档之前将`renderer.RenderingOptions.PaperOrientation`设置为`PdfPaperOrientation.Landscape`。
文档工作流中PDF方向常见问题有哪些?
常见问题包括像金融表格这样的宽内容在纵向模式中被剪切或换行,以及扫描或导入的页面侧面或颠倒到达。 IronPDF可以通过在渲染时设定正确的方向以及旋转现有文档中的页面来解决这些问题。
IronPDF能否旋转已存在PDF文档中的页面?
可以,IronPDF能旋转现有PDF文档中的页面。它提供的方法可以旋转一页、选定的几页或所有页面,使用`PdfPageRotation`枚举,该枚举类包括固定角度,例如Clockwise90、Clockwise180和Clockwise270。
IronPDF中`SetAllPageRotations`方法的作用是什么?
`SetAllPageRotations` 方法用于对PDF文档中的每一页应用统一的旋转角度。这对于纠正一批以相同方式错位的页面特别有用。
使用IronPDF时如何确保原始PDF文件被保留?
为了保留原始PDF文件,请使用`SaveAs`方法将旋转后的结果保存为新的文件名。这可以避免覆盖原始文档。
IronPDF中基于零的索引有什么意义?
IronPDF采用基于零的索引进行页面编号,这意味着第一页的索引为0。例如,`SetPageRotation(1, ...)`会针对文档中的第二页。
IronPDF如何处理整页旋转?
IronPDF旋转整个页面,包括文本、图像和注释在内。这种方法与仅旋转单个元素不同,确保整页内容保持其相对位置不变。
IronPDF提供了什么方法读取当前页面的旋转角度?
IronPDF提供了`GetPageRotation`方法来读取PDF文档中任何页面的当前旋转角度,这对于规范化来自多个来源合并的文档很有用。
为什么在IronPDF中区分渲染时的方向与旋转很重要?
区分渲染时的方向与旋转很重要,因为`PaperOrientation`仅在从其他格式生成PDF时适用,而旋转则应用于现有PDF。将这些概念分开可以避免文档处理中的常见错误。
在渲染时设置PDF方向解决了什么问题?
在渲染时设置PDF方向解决了宽内容在被迫进入纵向模式时被剪切或换行的问题。通过选择横向方向,金融表格和仪表板等内容获得所需的水平空间以正确显示。

