与其他文档合并时从 PDFsharp 页面合并中删除页码
Remove page numbers from PDFsharp page merge when merging with another document
我使用 PDFsharp 合并了 2 个 PDF 文件。我的代码将页码添加到每页的底部。现在我需要以同样的方式将创建的文档与另一个 PDF 合并。我得到的问题是从上一个文档创建的部分的页码没问题。从第一次合并创建的文档将新页码添加到第一组页面的顶部,因此在第一组页面中现在有两个页码彼此重叠。所以在一个地方,我将这两个 "Page 1 of 40" 和页面“1 of 110”放在一起。
这是我用来合并 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);
}
}
}
一个干净的解决方案:只将页码添加到最终文档中。保留不带页码的中间文件副本或根据需要创建文件两次。
技巧:在新页码下方绘制一个白色矩形以隐藏该区域中已有的任何内容。 PDF 将具有两个页码,但只有最近和当前的页码可见。
删除页码有点复杂。
我使用 PDFsharp 合并了 2 个 PDF 文件。我的代码将页码添加到每页的底部。现在我需要以同样的方式将创建的文档与另一个 PDF 合并。我得到的问题是从上一个文档创建的部分的页码没问题。从第一次合并创建的文档将新页码添加到第一组页面的顶部,因此在第一组页面中现在有两个页码彼此重叠。所以在一个地方,我将这两个 "Page 1 of 40" 和页面“1 of 110”放在一起。
这是我用来合并 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);
}
}
}
一个干净的解决方案:只将页码添加到最终文档中。保留不带页码的中间文件副本或根据需要创建文件两次。
技巧:在新页码下方绘制一个白色矩形以隐藏该区域中已有的任何内容。 PDF 将具有两个页码,但只有最近和当前的页码可见。
删除页码有点复杂。