下面代码的目的是将另外一个PDF文件(file2)内容追加到第一个PDF文件(file)尾,但合并的结果是第一个文件的内容被覆盖了。
System.IO.FileStream fs = new System.IO.FileStream(file.FullName,System.IO.FileMode.Append,System.IO.FileAccess.Write); System.IO.FileStream sr = new System.IO.FileStream(file2.FullName,System.IO.FileMode.Open); 
byte[] bData = new byte[sr.Length];
sr.Read(bData,0,bData.Length); 
fs.Write(bData,0,bData.Length);sr.Close(); 
fs.Flush();
fs.Close();请问怎么解决被覆盖的问题。
还有如果合并成功,如何保证追加的内容是另起一页。
谢谢! 

解决方案 »

  1.   

    你这样写是不成的pdf文件读写是有格式的
    internal void PopulateRelatedObjects(PdfFile PdfFile,  
      Hashtable container)
    {  
      Match m = Regex.Match(this.OriginalText, @"\d+ 0 R[^G]");
      while (m.Success)
      {
      int num=int.Parse(
      m.Value.Substring(0,m.Value.IndexOf(" ")));
      bool notparent = !Regex.IsMatch(this.OriginalText,  
      @"/Parent\s+"+num+" 0 R");  
      if (notparent &! container.Contains(num))
      {
      PdfFileObject pfo = PdfFile.LoadObject(num);
      if (pfo != null & !container.Contains(pfo.number))
      {
      container.Add(num,null);
      pfo.PopulateRelatedObjects(PdfFile, container);
      }
      }
      m = m.NextMatch();
      }
    }
    这地方写的很详细
    http://www.codeproject.com/KB/cs/giospdfsplittermerger.aspx?fid=247540&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=1
      

  2.   

    以你的写法,确实只有第二个文件。
    并不是你的程序有问题,而在于格式。
    pdf格式是从后向前看的。一般来讲,每段开头是定义本段的名称和数据等信息。
    而结尾才是定义本文章的内容排列顺序等总的信息。
    所以,你的程序处理后的那个文件,再次打开后,pdf阅读器从后向前查找定义,根本找不到第一个文件的那些说明,所以,就不显示。