Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将页面中的数字转换为PDF格式_C#_Pdf_Itext - Fatal编程技术网

C# 如何将页面中的数字转换为PDF格式

C# 如何将页面中的数字转换为PDF格式,c#,pdf,itext,C#,Pdf,Itext,我是一名编程新手,我尝试用iTextSharp库制作一个应用程序,该库获取一个pdf文件,将页码放在上面,然后创建一个新的应用程序 我试图在互联网上用一个例子制作一个WinForm应用程序 以下代码应将页码放入给定的pdf文件: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq

我是一名编程新手,我尝试用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)
        {

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
名称空间NummerierePDF
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
byte[]bytes=File.ReadAllBytes(@“C:\Test.pdf”);
Font-blackFont=FontFactory.GetFont(“Arial”,12,Font.NORMAL,BaseColor.BLACK);
使用(MemoryStream stream=new MemoryStream())
{
PdfReader reader=新PdfReader(字节);
使用(PdfStamper压模=新PdfStamper(读卡器,流))
{
int pages=reader.NumberOfPages;

对于(int i=1;i在声明局部变量时,必须指定完整的类型名
iTextSharp.text.Font
,因为有不同的类具有名称
Font
,编译器不知道使用哪个类

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

另请参见

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

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

另请参见

我仅更改了1行以删除编译错误

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

由于名称空间相同,System.Drawing.Font和iTextSharp.text.Font之间容易混淆。我只是添加了正确的名称空间


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

我只更改了一行以删除编译错误

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

由于名称空间相同,System.Drawing.Font和iTextSharp.text.Font之间容易混淆。我只是添加了正确的名称空间


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

我仍然看到:错误1“System.Drawing.Font”不包含“NORMAL”的定义,并且找不到接受“System.Drawing.Font”类型的第一个参数的扩展方法“NORMAL”(是否缺少using指令或程序集引用?)Z:\programmierung\NummerierePDF\NummerierePDF\Form1.cs 25 84NummerierePDF@TravisTD同样的问题。需要完整的类型名称。我更新了答案。我仍然得到以下结果:错误1“System.Drawing.Font”不包含“NORMAL”的定义,并且找不到接受类型为“System.Drawing.Font”的第一个参数的扩展方法“NORMAL”(是否缺少using指令或程序集引用?)NummerierePDF@TravisTD同样的问题。需要完整的类型名称。我更新了答案。它现在可以工作,但在一些PDF文件上它不会显示,我想是因为“stamper.getUndernetent(I)”无法工作,因为整个页面已被填充。让我检查一下。你能在不起作用的页面上附加一个页面吗。我可以调试。好的。我将创建这样一个页面,页面已被填充。当你指页面已被填充时。这是指页面编号已经存在还是该页面没有页脚?这是一个扫描的纸张站点。也许是这样吗?好的..我在测试时注意到了这个问题..首先我尝试了扫描页面,但它不起作用..那是因为页面不可编辑..让我尝试扫描页面它现在起作用,但在一些PDF文件上它不会显示,我想是因为“stamper.getUndernetent(I)”无法工作,因为整个页面已被填充。让我检查一下。你能在不起作用的页面上附加一个页面吗。我可以调试。好的。我将创建这样一个页面,页面已被填充。当你指页面已被填充时。这是指页面编号已经存在还是该页面没有页脚?这是一个扫描的纸张站点。也许是这样吗?好的。我在测试时注意到了这个问题。首先,我尝试了扫描页面,但它不起作用。那是因为页面不可编辑。让我试试扫描页面