c# - 如何替换字节

标签 c# pdf itext

我制作了一个应用程序,允许特定用户从他的设备中选择一个 PDF 文件并在其上放置页码。 之后文件将在特定目录中显示为“PDF.pdf”

但我的问题是文件会占用大量内存,使应用程序崩溃。

错误信息: Error Alert

代码:

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
    {
        private string theFile = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(theFile) || !File.Exists(theFile))
                return;
            byte[] bytes = File.ReadAllBytes(theFile);
            iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.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.GetOverContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
                    }
                }
                bytes = stream.ToArray();
            }
            File.WriteAllBytes(@"C:\Users\user\Pictures\Camera Roll\PDF.pdf", bytes);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var FD = new System.Windows.Forms.OpenFileDialog();
            if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                theFile = FD.FileName;
        }
    }
}

我试图将 byte 更改为其他方法,如 int。 但如您所料,它不会起作用。

最佳答案

要扩展之前的评论 - 基本上,停止尝试将整个文件作为连续数组保存在内存中 - API 是基于 Stream 的,并且你有可用的 FileStream对你来说,所以:

iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 12,
    iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
using (Stream source = File.OpenRead(theFile))
using (Stream dest = File.Create(@"C:\Users\user\Pictures\Camera Roll\PDF.pdf"))
{
    PdfReader reader = new PdfReader(source);
    using (PdfStamper stamper = new PdfStamper(reader, dest))
    {
        int pages = reader.NumberOfPages;
        for (int i = 1; i <= pages; i++)
        {
            ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_RIGHT,
                new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
        }
    }
}

我无法从这里进行测试,但它看起来应该可以工作。

根据快速搜索,它可能还可以用作:

iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 12,
    iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
using (Stream dest = File.Create(@"C:\Users\user\Pictures\Camera Roll\PDF.pdf"))
{
    PdfReader reader = new PdfReader(theFile);
    // ...

关于c# - 如何替换字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54532224/

相关文章:

java - 如何使用密码保护已经存在的 PDF?

vb.net - 使用 iTextSharp 在 VB.NET 中读取 PDF 书签

java - org.bouncycaSTLe.asn1.ASN1ObjectIdentifier”的签名者信息与签名者信息不匹配

c# - Unity 中的 Mathf.clamp() 函数

c# - var转换为C#中的类类型?

c# - 在 C# 中将对象集合转换为 JavaScript 数组的创造性方法

r - 在 rmarkdown pdf 的标题页添加图像

java - Android - 强制关闭、PDF 不可用、iText

c# - 包含 HTML 显示原始文本的数据库字段

java - 从其他带有页码的 PDF 生成 PDF