如何把号码的网页转换成PDF

问题描述 投票:0回答:2

我是新进的编程,我试图让与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
2个回答
2
投票

当声明局部变量blackFont,您必须指定完整的类型名称iTextSharp.text.Font,因为有有名字Font不同的阶级和编译器不知道要采取的一个。

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

参见Resolving an ambiguous reference


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。

© www.soinside.com 2019 - 2024. All rights reserved.