环境-PDFsharp库,Visual Studio 2012和C#作为语言。
我在尝试着:
我能够做以下所有事情。但是,当我打开文件Test2.pdf时,页面的大小将减小为宽度= 11英寸,高度– 11英寸。
我正在使用的这些PDF文件是我从互联网上下载的产品规格表。我相信这仅在某些类型的文件上发生,并且我不确定如何区分这些文件。
代码如下:
//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小组的建议,代码应如下所示:
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");
最佳答案
无需修改文档,请创建新文档并将页面从旧文档复制到新文档。
可以在PDFsharp论坛上的此帖子中找到示例代码:
http://forum.pdfsharp.net/viewtopic.php?p=2637#p2637
关于c#-4.0 - PDFsharp编辑pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17647872/