C# 如何使用PDFsharp在pdf页面底部添加空白

C# 如何使用PDFsharp在pdf页面底部添加空白,c#,asp.net,pdfsharp,C#,Asp.net,Pdfsharp,我试图在每个PDF页面的底部打上文字戳。问题是,我不希望新文本覆盖PDF页面的现有内容。我试图增加页面的大小,这会导致调整所有内容的大小,从而导致相同的问题。以下是我的代码示例: class Program { static void Main(string[] args) { PdfSharp.Pdf.PdfDocument PDFDoc = PdfSharp.Pdf.IO.PdfReader.Open(@"C:

我试图在每个PDF页面的底部打上文字戳。问题是,我不希望新文本覆盖PDF页面的现有内容。我试图增加页面的大小,这会导致调整所有内容的大小,从而导致相同的问题。以下是我的代码示例:

    class Program
    {
        static void Main(string[] args)
        {
            PdfSharp.Pdf.PdfDocument PDFDoc = PdfSharp.Pdf.IO.PdfReader.Open(@"C:\Users\Desktop\name.pdf", PdfDocumentOpenMode.Import);

            PdfSharp.Pdf.PdfDocument PDFNewDoc = new PdfSharp.Pdf.PdfDocument();

            for (int Pg = 0; Pg < PDFDoc.Pages.Count; Pg++)
            {
                PdfSharp.Pdf.PdfPage pdfPage = PDFNewDoc.AddPage(PDFDoc.Pages[Pg]);
                pdfPage.Height += 100;
                // Add height at bottom of the page
                XRect rect = new XRect(0, 0, pdfPage.Width, pdfPage.Height + 100);
                pdfPage.MediaBox = new PdfSharp.Pdf.PdfRectangle(rect);
                //   pdfPage.Width = XUnit.FromInch(7.50);
                //pdfPage.Height = XUnit.FromInch(12);


                XGraphics gfx = XGraphics.FromPdfPage(PDFNewDoc.Pages[Pg]);
                XFont font = new XFont("Arial", 10, XFontStyle.Regular);
                //rect calls new size
                gfx.DrawString("something", font, XBrushes.Black, rect, XStringFormats.BottomCenter);
            

            }
             PDFNewDoc.Save(@"C:\Users\Desktop\EditedPdf.pdf");


        }

    }
类程序
{
静态void Main(字符串[]参数)
{
PdfSharp.Pdf.PdfDocument PDFDoc=PdfSharp.Pdf.IO.PdfReader.Open(@“C:\Users\Desktop\name.Pdf”,PdfDocumentOpenMode.Import);
PdfSharp.Pdf.PdfDocument PDFNewDoc=新的PdfSharp.Pdf.PdfDocument();
对于(int Pg=0;Pg
我对这个话题做了研究,但没有找到有用的解决办法。非常感谢。
另外,一项建议是缩小PDF页面的内容,这将在页面的所有侧面提供空白。然后我可以在底部添加我的文本。这不是一个最优的解决方案,但总比没有解决方案好。不幸的是,我也不知道如何做到这一点;非常感谢您的帮助。

您可以创建一个新文档,并在其中添加一个页面,页面大小与原始文档的大小以及所需的额外空间相同。然后使用
DrawImage
将原始文档的内容复制到新文档中,但请注意不要在创建的额外空间上绘制

在本例中,我添加了40个点,约为14.1 mm

PdfDocument outputDocument = new PdfDocument();
PdfPage outputPage = outputDocument.AddPage();
XPdfForm inputForm = XPdfForm.FromFile(inputFilePath);

outputPage.Width = inputForm.Page.Width;
outputPage.Height = inputForm.Page.Height + 40;

XGraphics gfx = XGraphics.FromPdfPage(outputPage);
XRect box = new XRect(0, 0, outputPage.Width, outputPage.Height - 40);
gfx.DrawImage(inputForm, box);

outputDocument.Save(outputFilePath);

您可以创建一个新文档,并使用原始文档的大小加上所需的额外空间向其中添加一个页面。然后使用
DrawImage
将原始文档的内容复制到新文档中,但请注意不要在创建的额外空间上绘制

在本例中,我添加了40个点,约为14.1 mm

PdfDocument outputDocument = new PdfDocument();
PdfPage outputPage = outputDocument.AddPage();
XPdfForm inputForm = XPdfForm.FromFile(inputFilePath);

outputPage.Width = inputForm.Page.Width;
outputPage.Height = inputForm.Page.Height + 40;

XGraphics gfx = XGraphics.FromPdfPage(outputPage);
XRect box = new XRect(0, 0, outputPage.Width, outputPage.Height - 40);
gfx.DrawImage(inputForm, box);

outputDocument.Save(outputFilePath);