2011-12-24 2 views
0

pdf 바이트 스트림을 반환하는 메소드가 있습니다. (기입 가능한 pdf에서) 2 개의 스트림을 하나의 스트림으로 병합하고 하나의 pdf를 만드는 간단한 방법이 있습니까? 내 방법을 두 번 실행해야하지만 하나의 PDF 스트림에 두 개의 PDF 파일이 필요합니다. 감사.두 개의 pdf 바이트 스트림을 Itextsharp를 사용하여 병합

+0

iTextSharp에서 만든 바이트 스트림입니까? 두 개의 서로 다른 채우기 가능한 형식의 개별 바이트 스트림 또는 단일 형식입니까? 그들이 단일 형태에서 오는 경우에, 당신은 그 (것)들을 평평하게 했습니까? – kuujinbo

+0

잠시 동안 바이트 스트림을 잊어 버리십시오. 귀하의 질문은 "실제로 어떻게 하나의 PDF 두 개를 결합합니까?" –

+0

동일한 채울 수있는 PDF입니다. 나는 Itexsharp pdf stamper를 사용했다. asp.net에서 이것을 사용하고이 PDF를 브라우저에 스트리밍합니다. 그래서, 내 필요는 동일한 pdf를 스트림하지만 다른 데이터를주고 하나의 스트림에 eevrything을 넣는 내 메소드를 호출하는 것이다. 따라서 동일한 PDF가 단일 PDF로 두 번 나타납니다. – user282807

답변

3

당신이 PdfStamper로 채워진 형태를 평평하게하는 경우 당신은 말하지 않았다, 그래서 당신이 병합하려고 전에 평평해야한다 단지 말할 수 있습니다. 다음은 작동하는 .ashx HTTP 처리기입니다.

<%@ WebHandler Language="C#" Class="mergeByteForms" %> 
using System; 
using System.IO; 
using System.Web; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

public class mergeByteForms : IHttpHandler { 
    HttpServerUtility Server; 
    public void ProcessRequest (HttpContext context) { 
    Server = context.Server; 
    HttpResponse Response = context.Response; 
    Response.ContentType = "application/pdf"; 
    using (Document document = new Document()) { 
     using (PdfSmartCopy copy = new PdfSmartCopy(
     document, Response.OutputStream)) 
     { 
     document.Open(); 
     for (int i = 0; i < 2; ++i) { 
      PdfReader reader = new PdfReader(_getPdfBtyeStream(i.ToString())); 
      copy.AddPage(copy.GetImportedPage(reader, 1)); 
     } 
     } 
    } 
    } 
    public bool IsReusable { get { return false; } } 

// simulate your method to use __one__ byte stream for __one__ PDF 
    private byte[] _getPdfBtyeStream(string data) { 
// replace with __your__ PDF template 
    string pdfTemplatePath = Server.MapPath(
     "~/app_data/template.pdf" 
    ); 
    PdfReader reader = new PdfReader(pdfTemplatePath); 
    using (MemoryStream ms = new MemoryStream()) { 
     using (PdfStamper stamper = new PdfStamper(reader, ms)) { 
     AcroFields form = stamper.AcroFields; 
// replace this with your form field data 
     form.SetField("title", data); 
     // ... 
// this is __VERY__ important; since you're using the same fillable 
// PDF, if you don't set this property to true the second page will 
// lose the filled fields.   
     stamper.FormFlattening = true; 
     } 
     return ms.ToArray(); 
    } 
    } 
} 

잘하면 인라인 주석이 의미가 있습니다. 위의 _getPdfBtyeStream() 메소드는 PDF 바이트 스트림을 시뮬레이트합니다. FormFlatteningtrue으로 설정해야하는 이유는 PDF 양식 필드를 채울 때 이름이 고유해야한다는 것입니다. 귀하의 경우 두 번째 페이지는 동일한 채울 수있는 PDF 양식이므로 첫 번째 페이지와 동일한 필드 이름을 가지며 기입 할 때 무시됩니다. 위의 예의 줄을 주석으로 처리하십시오.

stamper.FormFlattening = true; 

즉, 일반적인 코드의 많은 Acrofield의가 고려되지 않기 때문에 (작성 가능한 형태의) 작동하지 않습니다 유래 에도 여기에 인터넷에서 PDF 파일을 병합합니다. 사실 stackoverflow의 about itextsharp tag "자주 묻는 질문 & Popular"에서 Merge PDFs까지 살펴보면 @Ray Cheng이 올바르게 표기 한 대답은 세 번째 주석에 언급되어 있습니다.

양식을 병합하지 않고 채울 수있는 PDF를 병합하는 또 다른 방법은 두 번째/다음 페이지의 양식 필드 이름을 바꾸는 것이지만 그게 더 효과적입니다.

+0

답변 및 설명에 대한 감사 쿠진 보. – user282807

+0

건배 야, 멋진 물건들. –

관련 문제