c#iTextSharp设置pdf页面大小
时间: 2024-11-03 17:16:10 浏览: 73
在iTextSharp库中,用于设置PDF页面大小通常是在创建PdfDocument或PdfStamper时进行的。如果你想要更改已存在的PDF页面尺寸,可以使用PdfCopy对象来复制文档并调整页面大小。以下是一个简单的示例:
```csharp
using iTextSharp.text;
using iTextSharp.text.pdf;
// 创建一个新的PdfDocument,并设置A4纸张大小作为默认
PdfDocument document = new PdfDocument();
document.SetPageSize(iTextSharp.text.PageSize.A4);
// 或者对于已有文档,假设sourcePdf是你要操作的源PDF
PdfReader reader = new PdfReader(sourcePdf);
PdfCopy copy = new PdfCopy(document, new System.IO.StreamWriter("output.pdf")); // 输出流
copy.OpenDocuments(reader);
copy.CopyPage(0); // 复制第一页
// 现在你可以修改页面大小,例如改为Letter尺寸
PdfImportedPage page = copy.GetImportedPage(reader, 0);
PdfStamper stamper = new PdfStamper(copy, new FileStream("output.pdf", FileMode.Append));
stamper.NewPage(iTextSharp.text.PageSize.Letter);
stamper.Close();
// 关闭文档和资源
reader.Close();
document.Close();
```
在这个例子中,我们首先创建了一个新的PDF,然后选择A4页面。如果需要改变现有PDF的某一页大小,就先复制该页,然后创建一个新的页面并设置其大小。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)