【问题标题】:How to add white space at the bottom of a pdf page using PDFsharp如何使用 PDFsharp 在 pdf 页面底部添加空白
【发布时间】:2020-08-07 15:27:05
【问题描述】:

我正在尝试用文本标记每个 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");


        }

    }

我对此主题进行了研究,但找不到有用的解决方案。谢谢 此外,一个建议是缩小 PDF 页面的内容,这将在页面的所有侧面提供空白。然后我可以在底部添加我的文本。这不是最佳解决方案,但总比没有解决方案好。不幸的是,我也不知道如何实现。任何帮助表示赞赏。

【问题讨论】:

    标签: c# asp.net pdfsharp


    【解决方案1】:

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

    在此示例中,我添加了 40 个点,大约为 14.1 毫米。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多