PdfSharp,结合几个pdf页面并将文本放在组合pdf之上

时间:2012-06-13 06:04:55

标签: pdf pdfsharp

我想将几个单独的pdf页面组合成一个pdf,同时将一个数字放在FIRST页面的顶部。某种水印,但顶部对齐,左边。

任何人都知道如何解决这个问题?

现在我得到以下代码来合并文件:

        Console.WriteLine("Merging started.....");
    PdfDocument outputPDFDocument = new PdfDocument();
    foreach (string pdfFile in pdfFiles)
    {
        PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
        outputPDFDocument.Version = inputPDFDocument.Version;

        foreach (PdfPage page in inputPDFDocument.Pages)
        {
            outputPDFDocument.AddPage(page);
        }
    }
    outputPDFDocument.Save(outputFilePath);
    Console.WriteLine("Merging Completed");
            addOverlay(outputPDFDocument);

编辑: 我通过在保存后打开合并文档然后添加文本来使其工作。

    public static void addOverlay(PdfDocument combined_pdf)
{
    PdfDocument document = combined_pdf;

    // Select the first page
    PdfPage page = document.Pages[0];

    page.Orientation = PageOrientation.Portrait;

    XGraphics gfx = XGraphics.FromPdfPage(page, XPageDirection.Downwards);

    // Write on top of background with known colors
    gfx.DrawString("TEXT ON PDF PAGE", new XFont("Helvetica", 12, XFontStyle.Regular), XBrushes.White, 10, 0, XStringFormats.TopLeft);

    // Save the new modified document...
    document.Save("final_path");
}

1 个答案:

答案 0 :(得分:1)

我不确定最佳方式,但我知道两种方法肯定会有效:

  1. 就像PDFsharp样本上的两页一样(仅修改为一页上的一页)
  2. 保存文件后(就像现在一样),您可以打开它进行修改并将文本添加到页面中(参见PDFsharp附带的水印样本)
  3. 方法2保持页面不变。使用方法1,您必须注意源文件中的不同页面大小 - 但您也有机会将所有页面缩放为单一格式(例如DIN A4),这可能比具有多个不同页面的文件更加用户友好格式。

相关问题