C# 4.0 PDFsharp编辑pdf文件
环境-PDFsharp库、Visual Studio 2012和C#作为语言 我正在努力:C# 4.0 PDFsharp编辑pdf文件,c#-4.0,pdf,pdfsharp,C# 4.0,Pdf,Pdfsharp,环境-PDFsharp库、Visual Studio 2012和C#作为语言 我正在努力: 阅读Test1.pdf(宽=17英寸,高-11英寸),共1页 添加一些文本到它 另存为另一个文件(Test2.pdf) 我能做到以下所有事情。但是,当我打开文件Test2.pdf时,页面的大小会减小到宽度=11英寸,高度–11英寸。 我使用的这些PDF文件是我从互联网下载的产品规格表。我相信这只发生在某些类型的文件上,我不知道如何区分这些文件 代码如下: //File dimentions - Width
//File dimentions - Width = 17 inches, Height - 11 inches (Tabloid Format)
PdfDocument pdfDocument = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Modify);
PdfPage page = pdfDocument.Pages[0];
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
//When the file is saved dimentions change to - Width = 11 inches, Height - 11 inches
pdfDocument.Save(@"D:\Test2.pdf");
我已经把文件上传到这里了
==================================================================================
根据PDFsharp团队的建议,代码应如下所示:
PdfDocument PDFDoc = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Import);
PdfDocument PDFNewDoc = new PdfDocument();
for (int Pg = 0; Pg < PDFDoc.Pages.Count; Pg++)
{
PdfPage pp = PDFNewDoc.AddPage(PDFDoc.Pages[Pg]);
XGraphics gfx = XGraphics.FromPdfPage(pp);
XFont font = new XFont("Arial", 10, XFontStyle.Regular);
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, pp.Width, pp.Height), XStringFormats.BottomCenter);
}
PDFNewDoc.Save(@"D:\Test2.pdf");
PdfDocument PDFDoc=PdfReader.Open(@“D:\Test1.pdf”,PdfDocumentOpenMode.Import);
PdfDocument PDFNewDoc=新PdfDocument();
对于(int Pg=0;Pg
请创建新文档并将页面从旧文档复制到新文档,而不是修改文档
示例代码可以在PDFsharp论坛的这篇文章中找到:PDFsharp团队,感谢您的回复。我将尝试以这种方式重新构造我的代码,然后重新开始。PDFsharp团队,我已经重新构造了我的代码。我仍然面临一些问题。请看一看我上面发布的重组代码。按照所有说明,我能够解决我的问题。非常感谢PDFsharp团队。请尝试从PDFNewDoc.Pages(而不是PDFDoc.Pages)修改页面,或者获取AddPage()返回的页面。我尝试运行此代码。它说PdfReader在当前上下文中不存在?