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. 如何将链接添加到PDF文档的文件附件
- 2. iText - 将html转换为pdf并添加并链接到附件
- 3. 将文件附加到PDF
- 4. Phpmailer从链接添加PDF附件
- 5. 将PDF文件附加到邮件中
- 6. 将xdf文件附加到pdf与PHP
- 7. 链接到附件
- 8. 链接到文档附件的链接
- 9. 将附件添加到Slackbot
- 10. 编程添加wordpress后附附件(pdf,word,excel或ppt文件)
- 11. 将XHTML文件中的PDF文件附加到电子邮件
- 12. 将附加附件添加到PouchDB文档
- 13. 将PDF附加到电子邮件
- 14. 如何将事件附加到链接早期附加方法的onSubmit事件?
- 15. 将文件附加到PHPMailer
- 16. 添加相关链接到pdf文件
- 17. 附加到PDF文件转换在bash
- 18. 将附件添加到邮件合并到电子邮件
- 19. 将JSON附加到文件上传
- 20. 将附件添加到电子邮件
- 21. 将附件添加到电子邮件
- 22. 将附件添加到PHP邮件
- 23. 使用iTextSharp添加附件到现有的PDF文件
- 24. 邮件附件pdf文件
- 25. 在PDF文件中添加或获取附件文件
- 26. 如何将pdf文件作为电子邮件的附件添加到
- 27. 如何将附件附加到邮件并下载它
- 28. C#:我如何将PDF文件附加到电子邮件?
- 29. 如何将pdf文件附加到Python中的MIME邮件?
- 30. 使用PDFsharp将PDF文件附加到Mandrill电子邮件
这个代码你是说你如何合并到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