2010-06-09 3 views
1

정적 인 이름을 가진 2 개의 첨부 파일이있는 PDF가 있습니다. iTextSharp를 사용하여이 파일들을 임시 디렉터리에 추출하여 그 파일들을 더 멀리 작업 할 수 있도록하고 싶습니다. 튜토리얼 here에 따라 시도했지만 아래 예제와 같이 iTextSharp.text.pdf.PdfReadergetCatalog() 메서드가없는 경우 문제가 발생했습니다.iTextSharp - 파일 첨부 파일을 열거 나 읽거나 추출하는 방법?

첨부 파일을 어떻게 추출 할 수 있습니까? PDF 문서가 "C : \ test.pdf"이고 두 개의 첨부 파일이 "attach1.xml"및 "attach2.xml"로 저장되어 있다고 쉽게 말하면됩니다.

답변

0

정확히 프로그래밍 방식은 아니지만이를 수행하는 방법을 찾았습니다. 첨부 파일을 추출 할 수있는 명령 줄 옵션이있는 PDF ToolKit 인 "pdftk.exe"라는 이진 파일이 포함되었습니다.

분명히하기 위해 pdftk.exe를 추가 한 다음 Process.Start("./pdftk", "contains_attachments.pdf unpack_files output \"C:\\output_directory\"")을 통해 호출했습니다. pdftk는 뒤에 백 슬래시가있는 폴더로 출력되지 않습니다. pdftk는 다음에서 찾을 수 있습니다. http://www.accesspdf.com/pdftk/

.exe 파일을 프로젝트에 추가 한 후에 속성을 "항상 복사"또는 "더 새로운 경우 복사"로 설정해야합니다.

0

이 해결책을 찾았습니다. 나는 그것이 최선의 방법인지 모른다. 그러나 그것은 일한다!!

protected void btnTransfer_Click(object sender, EventArgs e) 
{ 
    PdfReader reader = new PdfReader(FileUpload1.FileContent); 
    List<FileContent> lstAtt = GetAttachments(reader); 
    reader.Close(); 
} 

private class FileContent 
{ 
    public string Name { get; set; } 

    public byte[] Content { get; set; } 
} 

private List<FileContent> GetAttachments(PdfReader reader) 
{ 
    #region Variables 

    PdfDictionary catalog = null; 
    PdfDictionary documentNames = null; 
    PdfDictionary embeddedFiles = null; 
    PdfDictionary fileArray = null; 
    PdfDictionary file = null; 

    PRStream stream = null; 

    FileContent fContent = null; 
    List<FileContent> lstAtt = null; 

    #endregion 

    // Obtengo el conjunto de Diccionarios del PDF. 
    catalog = reader.Catalog; 

    // Variable que contiene la lista de archivos adjuntos. 
    lstAtt = new List<FileContent>(); 

    // Obtengo documento 
    documentNames = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES)); 

    if (documentNames != null) 
    { 
     // Obtengo diccionario de objetos embebidos 
     embeddedFiles = (PdfDictionary)PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES)); 
     if (embeddedFiles != null) 
     { 
      // Obtengo lista de documentos del Diccionario de objetos embebidos 
      PdfArray filespecs = embeddedFiles.GetAsArray(PdfName.NAMES); 

      // Cada archivo posee 2 posiciones en el array 
      for (int i = 0; i < filespecs.Size; i++) 
      { 
       // Como posee 2 posiciones por archivo, hago "i++" 
       i++; 
       fileArray = filespecs.GetAsDict(i); 

       // Obtengo diccionario del adjunto 
       file = fileArray.GetAsDict(PdfName.EF); 

       foreach (PdfName key in file.Keys) 
       { 
        stream = (PRStream)PdfReader.GetPdfObject(file.GetAsIndirectObject(key)); 

        fContent = new FileContent(); 
        // Nombre del Archivo. 
        fContent.Name = fileArray.GetAsString(key).ToString(); 

        // Array de bytes del Contenido del Archivo. 
        fContent.Content = PdfReader.GetStreamBytes(stream); 
        lstAtt.Add(fContent); 
       } 
      } 
     } 
    } 

    // Y al fin, devuelvo una lista de adjuntos del PDF - podrían haberlo echo un poco mas facil :@ 
    return lstAtt; 
} 
+0

동일한 기능을 위해 자바 스크립트에 코드를 넣을 수 있습니까? –

관련 문제