如何替换字节

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

我做了一个应用程序,它允许一个特定的用户选择从自己的设备中的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;
        }
    }
}

我试图字节改为如int一个其他的方法。但是,正如你猜对了,它不会工作。

c# pdf itext
1个回答
3
投票

为了扩大在先前的评论 - 基本上,停止尝试在内存中连续数组来保存整个文件 - 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);
    // ...
© www.soinside.com 2019 - 2024. All rights reserved.