2
A
回答
4
使用下面的代碼來創建一個附件, 請扔掉我只花了幾分鐘的代碼,瞭解 它並刪除不必要的代碼。
有一個RESOURCE變量指向您要附加的所有 pdf或文件。 在這種情況下,它是public static final String RESOURCE =「chapter16 /%s.pdf」;
凡16章文件夾中有你想要的PDF格式的附 和%s將與您提供與文件的名稱來代替 文件格式是.PDF(如果要連接一個PDF文件)
它在我的系統中工作和測試。我refered從here
// 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();
}
}
相關問題
- 1. Phpmailer從鏈接添加PDF附件
- 2. 將PDF文件附加到郵件中
- 3. 將XHTML文件中的PDF文件附加到電子郵件
- 4. 將附加附件添加到PouchDB文檔
- 5. 將PDF附加到電子郵件
- 6. 如何將事件附加到鏈接早期附加方法的onSubmit事件?
- 7. 將文件附加到PHPMailer
- 8. 附加到PDF文件轉換在bash
- 9. 將附件添加到郵件合併到電子郵件
- 10. 將附件添加到電子郵件
- 11. 將附件添加到電子郵件
- 12. 使用iTextSharp添加附件到現有的PDF文件
- 13. 在PDF文件中添加或獲取附件文件
- 14. 如何將pdf文件作爲電子郵件的附件添加到
- 15. 如何將附件附加到郵件並下載它
- 16. 將datauri pdf轉換爲附加到電子郵件的文件
- 17. 保存後自動將pdf文件附加到郵件中
- 18. 如何將pdf文件附加到Python中的MIME郵件?
- 19. C#:我如何將PDF文件附加到電子郵件?
- 20. 使用PDFsharp將PDF文件附加到Mandrill電子郵件
- 21. 將附件移動到文件夾並使用電子郵件附加它們
- 22. Facebook發佈到流並附加鏈接
- 23. 將文件轉換爲PDF並附加到Coldfusion中的另一PDF
- 24. 附加鏈接
- 25. 將文件附加到郵件
- 26. 將文件附加到電子郵件
- 27. 將附件添加到MimeMessage [Xamarin.Android]
- 28. 如何將LIGHTBOX添加到附件?
- 29. Javascript:打開Outlook並將附件添加到新電子郵件
- 30. 鏈接到電子郵件附件
這個代碼你是說你如何合併到PDF文檔到一個? – 2012-08-14 08:26:32
@Aaron Digulla不,我想將文件附加到現有的pdf文件,並在主頁面或超鏈接上以圖標形式給出附加鏈接。 – 2012-08-14 08:33:14
以下鏈接可能有幫助,請檢查。 http://itextpdf.com/examples/iia.php?id=289 – likeToCode 2012-08-14 08:36:58