【问题标题】:How to put numbers to pages into PDF如何将页面上的数字放入PDF
【发布时间】:2019-02-04 19:54:33
【问题描述】:

我是编程新手,我尝试使用 iTextSharp 库制作一个应用程序,该库采用 pdf 并在其上放置页码并创建一个新的。

我尝试用互联网上的示例制作一个 WinForm 应用程序。

以下代码应将页码放入给定的 pdf 文件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace NummerierePDF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] bytes = File.ReadAllBytes(@"C:\Test.pdf");
            Font blackFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
            using (MemoryStream stream = new MemoryStream())
            {
                PdfReader reader = new PdfReader(bytes);
                using (PdfStamper stamper = new PdfStamper(reader, stream))
                {
                    int pages = reader.NumberOfPages;
                    for (int i = 1; i <= pages; i++)
                    {
                        ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
                    }
                }
                bytes = stream.ToArray();
            }
            File.WriteAllBytes(@"C:\Test_1.pdf", bytes);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

我收到以下错误消息:

Erorr messages

【问题讨论】:

    标签: c# pdf itext


    【解决方案1】:

    在声明局部变量blackFont时,必须指定完整的类型名称iTextSharp.text.Font,因为有不同的类名称为Font,编译器不知道取哪一个。

    iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
    

    另见Resolving an ambiguous reference

    【讨论】:

    • 我仍然得到这个:错误 1 ​​'System.Drawing.Font' 不包含'NORMAL' 的定义,并且没有扩展方法'NORMAL' 接受'System.Drawing.Font 类型的第一个参数' 可以找到(您是否缺少 using 指令或程序集引用?) Z:\programmierung\NummerierePDF\NummerierePDF\Form1.cs 25 84 NummerierePDF
    • @TravisTD 同样的问题。需要完整的类型名称。我更新了答案。
    【解决方案2】:

    我只更改了 1 行以消除编译错误

    改变

    Font blackFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
    

    iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
    

    System.Drawing.Font 和 iTextSharp.text.Font 由于相同的命名空间而令人困惑。我刚刚添加了正确的命名空间

    我可以看到添加了页码的新 pdf。

    【讨论】:

    • 它现在可以工作,但在某些 PDF 文件上它不会显示,我认为因为“stamper.GetUnderConetent(I)”将无法工作,因为整个页面已经被填满。
    • 让我检查一下..你能附上它不起作用的页面之一..然后我可以调试
    • 啊..好的..我将创建这样一个页面已经填充的页面..当你的意思页面已经填充..这是否意味着页码已经存在或者该页面没有页脚?
    • 它是一个扫描的纸质网站。也许是这样?
    • 好的..我在测试时注意到了这个问题..首先我尝试扫描页面但它没有工作..那是因为页面不可编辑..让我尝试扫描页面
    猜你喜欢
    • 2019-09-29
    • 2014-04-03
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2011-01-21
    • 2020-04-04
    相关资源
    最近更新 更多