Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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# 在内存中操作PDF,然后将内容写入FileStream_C#_Itextsharp - Fatal编程技术网

C# 在内存中操作PDF,然后将内容写入FileStream

C# 在内存中操作PDF,然后将内容写入FileStream,c#,itextsharp,C#,Itextsharp,谁能告诉我哪里错了?我试图将不同的文件.pdf合并成一个唯一的文件,并为每个页面编写页面索引,但我总是有一个损坏的文件。我不习惯使用不同的流类型 public static bool MergeFiles(string destinationFile, List<string> sourceFiles) { try { int f = 0; // we create a read

谁能告诉我哪里错了?我试图将不同的文件.pdf合并成一个唯一的文件,并为每个页面编写页面索引,但我总是有一个损坏的文件。我不习惯使用不同的流类型

        public static bool MergeFiles(string destinationFile, List<string> sourceFiles)
    {
        try
        {

            int f = 0;
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(sourceFiles[f]);
            // we retrieve the total number of pages
            int n = reader.NumberOfPages;
            //Console.WriteLine("There are " + n + " pages in the original file.");
            // step 1: creation of a document-object
            Document document = new Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document

            MemoryStream ms = new MemoryStream();

            PdfWriter writer = PdfWriter.GetInstance(document, ms);

            // step 3: we open the document
            document.Open();

            PdfContentByte cb = writer.DirectContent;

            PdfImportedPage page;

            int rotation;
            // step 4: we add content

            while (f < sourceFiles.Count)
            {
                int i = 0;

                while (i < n)
                {
                    i++;
                    document.SetPageSize(reader.GetPageSizeWithRotation(i));
                    document.NewPage();
                    page = writer.GetImportedPage(reader, i);
                    rotation = reader.GetPageRotation(i);

                    if (rotation == 90 || rotation == 270)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                    }
                    else
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                }
                f++;
                if (f < sourceFiles.Count)
                {
                    reader = new PdfReader(sourceFiles[f]);
                    // we retrieve the total number of pages
                    n = reader.NumberOfPages;
                    //Console.WriteLine("There are " + n + " pages in the original file.");
                }
            }

            byte[] content = ms.ToArray();

            using (FileStream fs = File.Create(destinationFile))
            { 
                fs.Write(content, 0, (int)content.Length); 
            }
            PdfReader reader2 = new PdfReader(destinationFile);
            MemoryStream ms2 = new MemoryStream();

            PdfStamper stamper = new PdfStamper(reader, ms2);

            for (int i = 0; i < reader2.NumberOfPages; i++)
            {
                PdfContentByte canvas = stamper.GetOverContent(i);
                ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Page " + i + " of" + reader2.NumberOfPages), 10, 350, 90);
            }
            stamper.Close();

            MessageBox.Show("I file PDF sono stati uniti correttamente");
            return true;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);

            return false;
        }
    }
publicstaticboolmergefiles(字符串destinationFile,列表sourceFiles)
{
尝试
{
int f=0;
//我们为某个文档创建一个阅读器
PdfReader reader=新的PdfReader(sourceFiles[f]);
//我们检索总页数
int n=reader.NumberOfPages;
//WriteLine(“原始文件中有“+n+”个页面”);
//步骤1:创建文档对象
Document Document=新文档(reader.GetPageSizeWithRotation(1));
//步骤2:我们创建一个侦听文档的编写器
MemoryStream ms=新的MemoryStream();
PdfWriter writer=PdfWriter.GetInstance(文档,ms);
//步骤3:我们打开文档
document.Open();
PdfContentByte cb=writer.DirectContent;
PDF导入页面;
整数旋转;
//步骤4:我们添加内容
而(f
我自己解决了,希望它会有用

    public static bool MergeFiles2(string destinationFile, List<string> sourceFiles)
    {
        try
        {
            //Phase 1: merging multiple files
            int f = 0;
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(sourceFiles[f]);
            // we retrieve the total number of pages
            int n = reader.NumberOfPages;                 
            // step 1: creation of a document-object
            Document document = new Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a Memorystream for manipulating the files in memory
            MemoryStream ms = new MemoryStream();
            // step 3: we create a writer that listens to the document
            PdfWriter writer = PdfWriter.GetInstance(document, ms);       
            // step 4: we open the document
            document.Open();

            PdfContentByte cb = writer.DirectContent;                
            PdfImportedPage page;

            int rotation;

            // step 4: we add content
            while (f < sourceFiles.Count)
            {
                int i = 0;

                while (i < n)
                {
                    i++;
                    document.SetPageSize(reader.GetPageSizeWithRotation(i));
                    document.NewPage();
                    page = writer.GetImportedPage(reader, i);
                    rotation = reader.GetPageRotation(i);

                    if (rotation == 90 || rotation == 270)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                    }
                    else
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                }
                f++;
                if (f < sourceFiles.Count)
                {
                    reader = new PdfReader(sourceFiles[f]);
                    // we retrieve the total number of pages
                    n = reader.NumberOfPages;
                }
            }
            //step 5: we close the document
            document.Close();

            //step 6: we pass the stream into an array for passing all to an instance of PdfReader
            byte[] content = ms.ToArray();
            //Phase 2: Adding content to merged files

            //Step 1: we create an instance of PdfReader that reads the content of the last MemoryStream used
            PdfReader reader2 = new PdfReader(content);

            //Step2: we create a new MemoryStream
            MemoryStream ms2 = new MemoryStream();

            //Step3: we create a PdfStamper with the last PdfReader and the last MemoryStream created
            PdfStamper stamper = new PdfStamper(reader2, ms2);

            //Step4: for each page of the merged PDF we add the index of the page on the left and with orientation vertical
            for (int i = 1; i <= reader2.NumberOfPages; i++)
            {
                PdfContentByte canvas = stamper.GetOverContent(i);
                ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Page " + i + " of " + reader2.NumberOfPages), 10, 350, 90);
            }

            //Step5: we close the stamper
            stamper.Close();

            //Step6: we convert the MemoryStream to an array of bytes
            byte[] content2 = ms2.ToArray();

            //Step7: we finalize all creating the resultant file on the filesystem
            using (FileStream fs = File.Create(destinationFile))
            { 
                fs.Write(content2, 0, (int)content2.Length); 
            }

            MessageBox.Show("I file PDF sono stati uniti correttamente");
            return true;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);

            return false;
        }
    }
publicstaticboolmergefiles2(字符串destinationFile,列表sourceFiles)
{
尝试
{
//阶段1:合并多个文件
int f=0;
//我们为某个文档创建一个阅读器
PdfReader reader=新的PdfReader(sourceFiles[f]);
//我们检索总页数
int n=reader.NumberOfPages;
//步骤1:创建文档对象
Document Document=新文档(reader.GetPageSizeWithRotation(1));
//第2步:我们创建一个Memorystream来处理内存中的文件
MemoryStream ms=新的MemoryStream();
//步骤3:创建一个侦听文档的编写器
PdfWriter writer=PdfWriter.GetInstance(文档,ms);
//步骤4:我们打开文档
document.Open();
PdfContentByte cb=writer.DirectContent;
PDF导入页面;
整数旋转;
//步骤4:我们添加内容
而(f