【问题标题】:PdfSharp, combine several pdf pages and put text on top of combined pdfPdfSharp,合并几个 pdf 页面并将文本放在合并的 pdf 之上
【发布时间】:2012-06-16 01:38:15
【问题描述】:

我想将几个单独的pdf页面合并为一个pdf,同时在第一页的顶部放一个数字。某种水印,但左上对齐。

有人知道怎么解决吗?

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

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

【问题讨论】:

    标签: pdf pdfsharp


    【解决方案1】:

    我不确定最好的方法,但我知道两种肯定会奏效的方法:

    1. 像 PDFsharp 示例两页合一那样做(仅修改为一页合一)
    2. 保存文件后(就像您现在所做的那样),您可以打开它进行修改并将文本添加到页面(请参阅 PDFsharp 附带的水印示例)

    方法 2 保持页面不变。使用方法 1,您必须注意源文件中不同的页面大小 - 但您也有机会将所有页面缩放为单一格式(例如 DIN A4),这可能比具有多个不同页面的文件更易于用户使用格式。

    【讨论】:

    • 谢谢。在上面发布了解决方案。
    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2019-12-08
    相关资源
    最近更新 更多