PDFsharp编辑pdf文件 [英] PDFsharp edit a pdf file

查看:2901
本文介绍了PDFsharp编辑pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Environment - PDFsharp Library, Visual Studio 2012 and C# as the language.

我试图:


  1. 阅读Test1.pdf(宽度= 17英寸,高度 - 11英寸)1页

  2. 向其中添加一些文字

  3. 将它另存为另一个文件(Test2.pdf)

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

I am able to do all the following. But when I open the file Test2.pdf the size of the page is getting reduced to Width = 11 inches, Height – 11 inches. These PDF files that I am using are Product Specification Sheets that I have downloaded from the internet. I believe this is happening on only certain types of file and I am not sure how to differentiate these files.

代码如下:

//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 Team的建议,代码应如下所示:

As suggested by the PDFsharp Team the code should be as follows:

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");


推荐答案

而不是修改文档,请创建一个新文档

Instead of modifying the document, please create a new document and copy the pages from the old document to the new document.

在PDFsharp论坛上的此帖中可以找到示例代码:

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

Sample code can be found in this post on the PDFsharp forum:
http://forum.pdfsharp.net/viewtopic.php?p=2637#p2637

这篇关于PDFsharp编辑pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆