简体   繁体   English

与另一个文档合并时从 PDFsharp 页面合并中删除页码

[英]Remove page numbers from PDFsharp page merge when merging with another document

I used PDFsharp to merge 2 PDF files.我使用 PDFsharp 合并了 2 个 PDF 文件。 My code adds page numbering to the bottom of each page.我的代码将页码添加到每页的底部。 Now I need to merge the created document with another PDF the same way.现在我需要以同样的方式将创建的文档与另一个 PDF 合并。 The issue I get is the page numbers on the part created from the last document are fine.我得到的问题是从最后一个文档创建的部分的页码很好。 The document created from the first merge has the new page number added over the top of the first set so there are now two page numbers on top of each other in the first set of pages.从第一次合并创建的文档在第一组的顶部添加了新的页码,因此在第一组页面中现在有两个相互重叠的页码。 So in one spot I have both of these "Page 1 of 40" and Page "1 of 110" on top of each other.所以在一个地方,我将这些“第 1 页,共 40 页”和“第 1 页,共 110 页”放在一起。

Here is the code I used to merge the PDFs这是我用来合并 PDF 的代码

using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace SomeProject.Helpers
{
    public static class PDFHelper
    {
        public static void MergePDFFiles(string[] pdfFiles, string outputFilePath)
        {
            PdfDocument document = new PdfDocument();
            foreach (string pdfFile in pdfFiles)
            {
                PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
                document.Version = inputPDFDocument.Version;
                foreach (PdfPage page in inputPDFDocument.Pages)
                {
                    document.AddPage(page);
                }
            }

            // Set font for paging  
            XFont font = new XFont("Verdana", 9);
            XBrush brush = XBrushes.Black;
            // Create variable that store page count  
            string noPages = document.Pages.Count.ToString();
            // Set for loop of document page count and set page number using DrawString function of PdfSharp  
            for (int i = 0; i < document.Pages.Count; ++i)
            {
                PdfPage page = document.Pages[i];
                // Make a layout rectangle.  
                XRect layoutRectangle = new XRect(240 /*X*/ , page.Height - font.Height - 10 /*Y*/ , page.Width /*Width*/ , font.Height /*Height*/ );
                using (XGraphics gfx = XGraphics.FromPdfPage(page))
                {
                    gfx.DrawString("Page " + (i + 1).ToString() + " of " + noPages, font, brush, layoutRectangle, XStringFormats.Center);
                }
            }

            document.Save(outputFilePath);
        }
    }
}

A clean solution: only add the page numbers to the final documents.一个干净的解决方案:只将页码添加到最终文档中。 Keep copies of intermediate files without page numbers or create the files twice as needed.保留没有页码的中间文件副本,或根据需要创建文件两次。

A hack: draw a white rectangle below the new page numbers to hide anything that is already in that area.一个技巧:在新页码下方绘制一个白色矩形以隐藏该区域中已经存在的任何内容。 The PDF will have both page numbers, but only the most recent and current page number will be visible. PDF 将具有两个页码,但只有最近和当前的页码可见。

Removing page numbers is a bit complicated.删除页码有点复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM