2

複数の単一のpdfページを1つの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");
}
4

1 に答える 1

1

最善の方法はわかりませんが、確実に機能する 2 つの方法を知っています。

  1. PDFsharp サンプルの Two Pages on One のように実行します (One Pages on One にのみ変更されます)。
  2. ファイルを保存した後 (今と同じように)、ファイルを開いて変更し、ページにテキストを追加できます (PDFsharp に付属の透かしサンプルを参照してください)。

方法 2 では、ページがそのまま保持されます。方法 1 では、ソース ファイル内のさまざまなページ サイズに注意を払う必要がありますが、すべてのページを 1 つの形式 (DIN A4 など) にスケーリングすることもできます。これは、複数の異なるページを含むファイルよりも使いやすい可能性があります。フォーマット。

于 2012-06-13T09:22:23.753 に答える