.NET 6中XPdfPage.CreateFromPage的替代
时间: 2024-02-24 14:56:28 浏览: 25
在.NET 6中,XPdfPage.CreateFromPage方法已经被移除了。如果你需要将一个已有的PdfPage对象转换为XPdfPage对象,可以使用PdfSharpCore库中的PdfSharpCore.Pdf.PageExtensions.ToPdfPage方法。此方法将PdfPage对象转换为PdfSharpCore.Pdf.PdfPage对象,然后你可以使用PdfSharpCore.Rendering.XGraphics.FromPdfPage方法来创建XGraphics对象。以下是一个示例代码:
```
using PdfSharpCore.Pdf;
using PdfSharpCore.Pdf.IO;
using PdfSharpCore.Drawing;
// 打开PDF文档
PdfDocument document = PdfReader.Open("input.pdf", PdfDocumentOpenMode.Modify);
// 获取第一页
PdfPage page = document.Pages[0];
// 将PdfPage对象转换为PdfSharpCore.Pdf.PdfPage对象
PdfSharpCore.Pdf.PdfPage pdfSharpPage = page.ToPdfPage();
// 使用PdfSharpCore.Rendering.XGraphics.FromPdfPage方法创建XGraphics对象
XGraphics gfx = XGraphics.FromPdfPage(pdfSharpPage);
// 在XGraphics对象上绘制内容
gfx.DrawString("Hello, world!", new XFont("Arial", 12), XBrushes.Black, new XRect(0, 0, pdfSharpPage.Width, pdfSharpPage.Height), XStringFormats.Center);
// 保存PDF文档
document.Save("output.pdf");
```
希望这个回答能够帮到你。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)