【发布时间】: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)
{
}
}
}
我收到以下错误消息:
【问题讨论】: