2011-11-18 97 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