2012-08-14 2 views
2

기본 페이지의 첨부 파일에 대한 링크를 포함하여 기존 PDF에 파일을 첨부하려면 어떻게해야합니까?링크가 포함 된 PDF 파일에 첨부 파일 추가

저는 Itext를 사용 중이며 지금까지는 문서 수준에서만 첨부 할 수있었습니다.

+0

here에서이 코드를 대해 참조? –

+0

@Aaron Digulla 아니요, 기존 pdf 파일에 파일을 첨부하고 기본 페이지 또는 하이퍼 링크에 아이콘 형태로 연결된 링크를 제공하고 싶습니다. –

+0

아래 링크는 도움이 될 수 있습니다. 확인하십시오. http://itextpdf.com/examples/iia.php?id=289 – likeToCode

답변

4

첨부 파일을 만들려면 아래 코드를 사용하십시오. 코드를 던지십시오. 몇 분 밖에 걸리지 않았으므로 을 이해하고 불필요한 코드를 제거하십시오.

pdf 또는 첨부하려는 모든 파일을 가리키는 RESOURCE 변수가 있습니다. 이 경우 여기에 public static final String RESOURCE = "chapter16/% s.pdf"; (PDF 파일을 첨부하는 경우) 16 장 폴더가 부착 와 % s로 원하는 PDF의의를 가지고

당신이 제공하고 파일의 이름으로 대체됩니다 파일 형식은 .PDF입니다

그것은 내 시스템에서 작동하고 테스트되었습니다. 나는 당신이 일에 PDF 문서를 병합하는 방법을 의미

// I create the list which has the list of files names i want to attach 
ArrayList<String> strList = new ArrayList<String>(); 
strList.add("Strings"); // its the same file I'm attaching four times 
strList.add("Strings"); // where "Strings" is the name of the file 
strList.add("Strings"); 
strList.add("Strings"); 

import java.io.ByteArrayOutputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.sql.SQLException; 
import java.util.ArrayList; 

import com.itextpdf.text.Chunk; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.List; 
import com.itextpdf.text.ListItem; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.pdf.PRStream; 
import com.itextpdf.text.pdf.PdfAnnotation; 
import com.itextpdf.text.pdf.PdfArray; 
import com.itextpdf.text.pdf.PdfDictionary; 
import com.itextpdf.text.pdf.PdfName; 
import com.itextpdf.text.pdf.PdfReader; 
import com.itextpdf.text.pdf.PdfWriter; 


/** 
* Creates a PDF listing of attachments 
* The attachments can be extracted. 
*/ 
public class AttachFiles { 

    /** Path to the resources. */ 
    public static final String RESOURCE = "chapter16/%s.pdf"; 
    /** The filename of the resulting PDF. */ 
    public static final String FILENAME = "ResultingFile.pdf"; 
    /** The path to the resulting PDFs */ 
    public static final String PATH = "chapter16/%s"; 
    /** The filename of the PDF */ 
    public static final String RESULT = String.format(PATH, FILENAME); 

    /** 
    * Creates a PDF listing 
    * @throws IOException 
    * @throws DocumentException 
    * @throws SQLException 
    */ 
    public static void main(String[] args) throws IOException, DocumentException, SQLException { 
     AttachFiles attachFiles = new AttachFiles(); 
     FileOutputStream os = new FileOutputStream(RESULT); 
     os.write(attachFiles.createPdf()); 
     os.flush(); 
     os.close(); 
     attachFiles.extractAttachments(RESULT); 
    } 

    /** 
    * Extracts attachments from an existing PDF. 
    * @param src the path to the existing PDF 
    * @param dest where to put the extracted attachments 
    * @throws IOException 
    */ 
    public void extractAttachments(String src) throws IOException { 
     PdfReader reader = new PdfReader(src); 
     PdfArray array; 
     PdfDictionary annot; 
     PdfDictionary fs; 
     PdfDictionary refs; 
     for (int i = 1; i <= reader.getNumberOfPages(); i++) { 
      array = reader.getPageN(i).getAsArray(PdfName.ANNOTS); 
      if (array == null) continue; 
      for (int j = 0; j < array.size(); j++) { 
       annot = array.getAsDict(j); 
       if (PdfName.FILEATTACHMENT.equals(annot.getAsName(PdfName.SUBTYPE))) { 
        fs = annot.getAsDict(PdfName.FS); 
        refs = fs.getAsDict(PdfName.EF); 
        for (PdfName name : refs.getKeys()) { 
         FileOutputStream fos 
          = new FileOutputStream(String.format(PATH, fs.getAsString(name).toString())); 
         fos.write(PdfReader.getStreamBytes((PRStream)refs.getAsStream(name))); 
         fos.flush(); 
         fos.close(); 
        } 
       } 
      } 
     } 
    } 

    /** 
    * Creates the PDF. 
    * @return the bytes of a PDF file. 
    * @throws DocumentExcetpion 
    * @throws IOException 
    * @throws SQLException 
    */ 
    public byte[] createPdf() throws DocumentException, IOException, SQLException { 
     // step 1 
     Document document = new Document(); 
     // step 2 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     PdfWriter writer = PdfWriter.getInstance(document, baos); 
     // step 3 
     document.open(); 
     // step 4 
     document.add(new Paragraph("This is a list pdf attachments.")); 
     PdfAnnotation annot; 
     ListItem item; 
     Chunk chunk; 
     List list = new List(); 
     // I create the list which has the list of files names i want to attach 
     ArrayList<String> strList = new ArrayList<String>(); 
     strList.add("Strings"); // its the same file I'm attaching four times 
     strList.add("Strings"); 
     strList.add("Strings"); 
     strList.add("Strings"); 

     for (String strWord : strList) { 
      annot = PdfAnnotation.createFileAttachment(
        writer, null, "Name", null, 
        String.format(RESOURCE, strWord), String.format("%s.pdf", strWord)); 
      item = new ListItem("Name"); 
      item.add("\u00a0\u00a0"); 
      chunk = new Chunk("\u00a0\u00a0\u00a0\u00a0"); 
      chunk.setAnnotation(annot); 
      item.add(chunk); 
      list.add(item); 
     } 
     document.add(list); 
     // step 5 
     document.close(); 
     return baos.toByteArray(); 
    } 
} 
관련 문제