首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PDFsharp编辑pdf文件

PDFsharp编辑pdf文件
EN

Stack Overflow用户
提问于 2013-07-15 06:20:21
回答 1查看 44.1K关注 0票数 25

环境- PDFsharp库,Visual 2012和C#作为语言。

我试图:

  1. 阅读Test1.pdf (宽度= 17英寸,高度- 11英寸),1页
  2. 给它加上一些文字
  3. 保存为另一个文件(Test2.pdf)

我可以做以下所有的事情。但是当我打开Test2.pdf文件时,页面的大小正在缩小到宽度= 11英寸,高度- 11英寸。这些PDF文件,我正在使用的产品规格表,我已经从互联网下载。我相信这种情况只发生在某些类型的文件上,我不知道如何区分这些文件。

代码如下:

代码语言:javascript
复制
//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");

我已经把文件上传到这里了Test1.pdf

==================================================================================

正如PDFsharp团队建议的那样,代码应该如下所示:

代码语言:javascript
复制
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");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-15 07:59:28

不要修改文档,请创建一个新文档并将页面从旧文档复制到新文档。

示例代码可以在PDFsharp论坛上的这篇文章中找到:

http://forum.pdfsharp.net/viewtopic.php?p=2637#p2637

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17647872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档