Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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# 如何替换字节_C#_Pdf_Itext - Fatal编程技术网

C# 如何替换字节

C# 如何替换字节,c#,pdf,itext,C#,Pdf,Itext,我制作了一个应用程序,允许特定用户从他的设备中选择一个PDF文件,并在上面输入页码。 之后,该文件将在特定目录中显示为“PDF.PDF” 但我的问题是,文件将占用大量内存,使应用程序崩溃 错误消息: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using Sys

我制作了一个应用程序,允许特定用户从他的设备中选择一个PDF文件,并在上面输入页码。 之后,该文件将在特定目录中显示为“PDF.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
    {
        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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
名称空间NummerierePDF
{
公共部分类Form1:Form
{
私有字符串theFile=“”;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
if(string.IsNullOrEmpty(theFile)| |!File.Exists(theFile))
返回;
byte[]bytes=File.ReadAllBytes(文件);
iTextSharp.text.Font-blackFont=FontFactory.GetFont(“Arial”,12,iTextSharp.text.Font.NORMAL,BaseColor.BLACK);
使用(MemoryStream stream=new MemoryStream())
{
PdfReader reader=新PdfReader(字节);
使用(PdfStamper压模=新PdfStamper(读卡器,流))
{
int pages=reader.NumberOfPages;

对于(int i=1;i要扩展前面的注释-基本上,停止尝试将整个文件作为连续数组保存在内存中-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);
        }
    }
}

你能不能先写一个
FileStream
,然后把
MemoryStream
放在外面?也就是说,
使用(var stream=File.Create(path)){…}
?读卡器也一样-这里不需要
ReadAllBytes
-它看起来像
PdfReader
接受
,所以给它
File.OpenRead(文件)
我的知识和英语水平不足以理解你的答案。如果可以的话,你能再多解释一点吗?那太好了。我会阅读你的答案并在互联网上查找你的想法。我仍然试图弄清楚你做了什么,但谢谢你。你是MVP!@Burak我不知道你熟悉哪些概念但是数组(
byte[]
)是一个单独的连续内存块,当大小变大时会变得很棘手;
Stream
,然而,它是一个允许增量IO访问某些资源(在本例中是一个文件)的API,这意味着:您不需要一次保存所有内存-您只需读取接下来的10、100、1000个字节(无论什么),处理它,然后重复。因此:
Stream
(或最近的“管道”)API更适合用于大型资源。我用
Stream
替换了数组用法,仅此而已
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);
    // ...