iTextSharp.text.Image旋转图片
时间: 2024-12-23 19:20:19 浏览: 53
iTextSharp是一个用于.NET平台的PDF处理库,在这个库中,你可以方便地操作PDF文档,包括旋转图片。要使用iTextSharp旋转图片,首先你需要加载图片,然后创建一个`Image`对象,并指定旋转的角度。下面是一个简单的示例:
```csharp
using iTextSharp.text;
using iTextSharp.text.image;
// 加载图片
byte[] imageBytes = File.ReadAllBytes("path_to_your_image.jpg");
Image imageObject = Image.GetInstance(imageBytes);
// 设置旋转角度,例如顺时针90度
double rotationDegrees = 90; // 可以是0、90、180或270等
imageObject.RotateClockwise(rotationDegrees);
// 将旋转后的图片添加到PDF页面上
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream("rotated_pdf.pdf", FileMode.Create));
document.Open();
// 使用Image对象
document.Add(imageObject);
document.Close();
```
相关问题
iTextSharp.text.Image Image 旋转图片
iTextSharp是一个.NET库,用于处理PDF文档,包括图像操作。如果你想在iTextSharp中旋转图片,可以使用`Image`类提供的旋转功能。以下是基本步骤:
```csharp
using iTextSharp.text;
using iTextSharp.text.image;
// 加载图片
Image image = Image.GetInstance("path_to_your_image.jpg");
// 定义旋转角度(这里以90度为例)
float rotationAngle = 90f; // 逆时针旋转
// 创建一个矩形矩阵,用于保存旋转后的图像位置信息
RectangleF rotatedBox = new RectangleF(0, 0, image.Width, image.Height);
rotatedBox.Rotate(rotationAngle);
// 使用旋转后的矩形确定新图像的位置和大小
image.SetAbsolutePosition(rotatedBox.Left, rotatedBox.Top);
image.ScaleTo(rotatedBox.Right - rotatedBox.Left, rotatedBox.Bottom - rotatedBox.Top);
// 将旋转后的图片添加到PDF文档中
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileStream("output.pdf", FileMode.Create));
document.Open();
copy.AddImage(image);
document.Close();
```
完成上述代码后,原始图片将以指定的角度旋转并添加到生成的PDF文件中。
iTextSharp.text.Rectangle的用法
iTextSharp是一个用于.NET平台创建PDF文档的强大库,它的Rectangle类用于表示矩形区域,常用于设置页面尺寸、边距、图表边界等。以下是`iTextSharp.text.Rectangle`的基本用法:
```csharp
using iTextSharp.text;
using iTextSharp.text.geometry;
// 创建一个新的矩形对象
Rectangle rect = new Rectangle(10f, 20f); // x轴位置10mm, y轴位置20mm
// 如果你想指定厘米为单位
rect = new Rectangle(1.0f, 2.0f, Unit.Centimeter);
// 矩形还可以包含宽度和高度的默认值
Rectangle pageSize = PageSize.A4; // 使用预设的A4纸张大小
// 设置矩形的边距
rect.SetMargin(10f, 15f, 20f, 25f); // 上下左右各10mm、15mm、20mm、25mm
// 在PDF中应用这个矩形,比如作为页面大小或图像框
Document document = new Document(rect);
```
阅读全文
相关推荐
















