2012-11-09 2 views
0

관리자 (또는 사용자)가 채워진 하나의 PDF 양식이 있습니다.
생성 된 PDF를 병합하고 싶습니다.
간단한 병합 작업을 수행했지만 먼저 여러 파일을 생성 한 다음 병합해야합니다.
사용자가 여러 양식을 채운 다음 최종 제출 또는 인쇄 할 때 채워진 PDF가 하나만 병합되는 방식이 있습니까?
병합하고 싶습니다 여러 번 채워진 PDF 양식

+1

당신은 갈 : http://stackoverflow.com/questions/6196124/merge-pdf-files – Hardrada

+0

내가 모두를 추가하려는 결국 다음 여러 번 작성하고 하나 개의 소스 파일이 채워질 파일을 병합 할 수 있습니다. –

+0

많은 사람들이 여러 번 사용하여 하나의 폴더에 'n'개의 파일을 생성 할 수있는 하나의 양식 서식 파일을 갖고 있다는 귀하의 질문을 이해합니다. 그들을 병합하고 싶습니다. 내가 게시 한 링크는 itextsharp 및 병합에만 해당됩니다. 나는 이것을 전에 사용했는데 훌륭하게 작동합니다. – Hardrada

답변

0

PDF 파일을 병합이 병합 기능을 사용 해보세요 :

public static void Merge(string[] sourceFiles, string destinationFile) 
{ 
    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; 
     //Debug.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 
     PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create)); 
     // step 3: we open the document 
     document.Open(); 
     PdfContentByte cb = writer.DirectContent; 
     PdfImportedPage page; 
     int rotation; 
     // step 4: we add content 
     while (f < sourceFiles.Length) 
     { 
      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); 
       } 
       //Debug.WriteLine("Processed page " + i); 
      } 
      f++; 
      if (f < sourceFiles.Length) 
      { 
       reader = new PdfReader(sourceFiles[f]); 
       // we retrieve the total number of pages 
       n = reader.NumberOfPages; 
       //Debug.WriteLine("There are " + n + " pages in the original file."); 
      } 
     } 
     // step 5: we close the document 
     document.Close(); 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 

참조 : Merge PDF Files using iTextSharp

+0

나는 위의 내 의견에 링크 된 원래 포스터에서 코드의 뻔뻔스러운 추출을 위해 당신을 뽑았다. 소싱 및 링크를 추가하면 아래 표가 제거됩니다. – Hardrada

+0

@Hardrada, 여기에서 내 프로젝트 중 하나에서이 코드 조각을 사용했습니다. http://www.khswsoft.be/Blog/tabid/81/EntryId/16/Merge-PDF-Files-using-iTextSharp.aspx and http : //khsw.blogspot.com/2006/04/merge-pdf-files-using-itextsharp.html –

+0

아주 좋습니다. 제거 된 투표 – Hardrada

0

내가 쓸이를 사용하여 내가 위에서 내 의견에 공급 병합 코드 .. 를 기반으로 http 응답의 출력 스트림. 바이트 []를 반환하지만 원래 코드와이 코드를 사용하여 작업을 완료 할 수 있습니다. 여기

public static byte[] MergeFiles(List<byte[]> sourceFiles) 
    { 
     Document document = new Document(); 
     MemoryStream output = new MemoryStream(); 

     try 
     { 
      // Initialize pdf writer 
      PdfWriter writer = PdfWriter.GetInstance(document, output); 

      // Open document to write 
      document.Open(); 
      PdfContentByte content = writer.DirectContent; 

      // Iterate through all pdf documents 
      for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++) 
      { 
       // Create pdf reader 
       PdfReader reader = new PdfReader(sourceFiles[fileCounter]); 
       int numberOfPages = reader.NumberOfPages; 

       // Iterate through all pages 
       for (int currentPageIndex = 1; currentPageIndex <= 
            numberOfPages; currentPageIndex++) 
       { 
        // Determine page size for the current page 
        document.SetPageSize(
         reader.GetPageSizeWithRotation(currentPageIndex)); 

        // Create page 
        document.NewPage(); 
        PdfImportedPage importedPage = 
         writer.GetImportedPage(reader, currentPageIndex); 


        // Determine page orientation 
        int pageOrientation = reader.GetPageRotation(currentPageIndex); 
        if ((pageOrientation == 90) || (pageOrientation == 270)) 
        { 
         content.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, 
          reader.GetPageSizeWithRotation(currentPageIndex).Height); 
        } 
        else 
        { 
         content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0); 
        } 
       } 
      } 
     } 
     catch (Exception exception) 
     { 
      throw new Exception("There has an unexpected exception" + 
        " occured during the pdf merging process.", exception); 
     } 
     finally 
     { 
      document.Close(); 
     } 
     return output.GetBuffer(); 
    } 
관련 문제