2011-11-18 95 views
0

我正在创建PDF并希望添加现有的PDF和/或图像。我有以下代码适用于这些图像,但由于现有的pdf未显示在新PDF中,而图像没有问题,所以我遇到了PDF部分的问题。我找到了以下question,但我的代码看起来很相似。任何想法,我缺少什么?如何将现有pdf添加到创建的pdf中?

 using (MemoryStream ms = new MemoryStream()) 
     { 
      PdfWriter pWriter = PdfWriter.GetInstance(myDoc, ms); 

      myDoc.Open(); 

      int index = 0; 
      iTextSharp.text.Image img; 
      foreach (var buf in bufList) 
      { 
       if (uploadType[index] == 0) 
       { 
        PdfContentByte pdfContentByte = pWriter.DirectContent; 

        PdfReader reader = new PdfReader(buf); 
        int pageCount = reader.NumberOfPages; 
        myDoc.SetPageSize(reader.GetPageSizeWithRotation(1)); 

        for (int pageNum = 1; pageNum <= pageCount; pageNum++) 
        { 
         myDoc.NewPage(); 
         PdfImportedPage importedPage = pWriter.GetImportedPage(reader, pageNum); 
         pdfContentByte.AddTemplate(importedPage, 0, 0); 
        } 
        reader.Close(); 
       } 
       else 
       { 
        myDoc.NewPage(); 
        img = iTextSharp.text.Image.GetInstance(buf); 
        img.ScaleToFit(612f, 792f); 
        img.Alignment = iTextSharp.text.Image.ALIGN_CENTER | iTextSharp.text.Image.ALIGN_MIDDLE; 
        myDoc.Add(img); 
       } 
       index++; 
      } 

      pWriter.CloseStream = false; 
      myDoc.Close(); 
      ms.Position = 0; 
     } 
+1

究竟是什么问题?例外? – slfan

+0

即使图像显示没有问题,我也没有看到要显示的现有PDF。 – MrM

+0

它看起来已经足够让我纠正;虽然也许这可能有帮助 - http://khsw.blogspot.com/2006/04/merge-pdf-files-using-itextsharp.html – Reddog

回答

0

如果生成PDF和没有Exception在你的代码,这看起来的确从一个快速浏览OK抛出,我会检查两件事情:

  1. 是什么在uploadType - 是的值总是0?
  2. 什么是bufList - 有没有PDF文件? (无论是文件路径或字节数组)

由于问题还标有asp.net,这里有一个简单的工作示例(HTTP处理程序ashx的),其中bufList使用文件路径 - 这样你不需要维护uploadType集合:

<%@ WebHandler Language="C#" Class="appendExisting" %> 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Text.RegularExpressions; 
using System.Web; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

public class appendExisting : IHttpHandler { 
    public void ProcessRequest (HttpContext context) { 
    HttpResponse Response = context.Response; 
    HttpServerUtility Server = context.Server; 
    Response.ContentType = "application/pdf"; 
    string[] bufList = { 
     Server.MapPath("~/app_data/01.pdf"), 
     Server.MapPath("~/app_data/02.pdf"), 
     Server.MapPath("~/app_data/01.jpg"), 
     Server.MapPath("~/app_data/02.png") 
    }; 
    using (Document document = new Document()) { 
     PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream); 
     document.Open(); 

// simulate the existing content you were asking about 
     document.Add(new Paragraph("Paragraph")); 

     PdfContentByte cb = writer.DirectContent; 
     foreach (var buf in bufList) { 
     bool isPdf = Regex.IsMatch(
      Path.GetExtension(buf), @"\.pdf$", RegexOptions.IgnoreCase 
     ); 
     if (isPdf) { 
      PdfReader reader = new PdfReader(buf); 
      int pages = reader.NumberOfPages; 
      for (int i = 0; i < pages;) { 
      document.NewPage(); 
      PdfImportedPage page = writer.GetImportedPage(reader, ++i); 
      cb.AddTemplate(page, 0, 0); 
      } 
     } 
     else { 
      document.NewPage(); 
      iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(buf); 
      img.ScaleToFit(612f, 792f); 
      img.Alignment = iTextSharp.text.Image.ALIGN_CENTER 
       | iTextSharp.text.Image.ALIGN_MIDDLE 
      ; 
      document.Add(img);   
     } 
     } 
    } 
    } 
    public bool IsReusable { 
    get { return false; } 
    } 
} 
+0

上传类型是0或1.图像加载正常,但功能pdf合并不起作用。如果这不是有错误的代码片段,有什么建议吗? – MrM

+0

这就是我所问的;你有没有在代码中加入**'uploadType'和'bufList'的值,以确保它们实际上具有你期望的值? – kuujinbo