เพื่อที่จะดึงสิ่งที่แนบมาเราจะใช้ Aspose.PDF for Java API ซึ่งเป็นคุณลักษณะที่อุดมไปด้วยที่มีประสิทธิภาพและง่ายต่อการใช้ API แปลงสำหรับแพลตฟอร์ม java Javaคุณสามารถดาวน์โหลดรุ่นล่าสุดได้โดยตรงจาก Maven และติดตั้งภายในโครงการ Maven ตามของคุณโดยการเพิ่มการกำหนดค่าต่อไปนี้เพื่อpom.xml
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java AP</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>
สารสกัดจากเอกสารแนบจาก PDF Java
คุณจำเป็นต้อง Aspose.PDF for Java เพื่อลองรหัสในสภาพแวดล้อมของคุณ
1.รับคอลเลกชันไฟล์ที่ฝังตัว
1.ได้รับการนับของไฟล์ที่ฝังตัว
1.ห่วงผ่านคอลเลกชันที่จะได้รับสิ่งที่แนบมาทั้งหมด
1.ตรวจสอบว่าวัตถุพารามิเตอร์มีพารามิเตอร์
1.รับสิ่งที่แนบมาและเขียนไปยังแฟ้มหรือสตรีม
สารสกัดจากเอกสารแนบ PDF
// Open document
Document pdfDocument = new Document(_dataDir+"input.pdf");
// Get particular embedded file
FileSpecification fileSpecification = pdfDocument.getEmbeddedFiles().get_Item(1);
// Get the file properties
System.out.printf("Name: - " + fileSpecification.getName());
System.out.printf("\nDescription: - " + fileSpecification.getDescription());
System.out.printf("\nMime Type: - " + fileSpecification.getMIMEType());
// Get attachment form PDF file
try {
InputStream input = fileSpecification.getContents();
File file = new File(fileSpecification.getName());
// Create path for file from pdf
file.getParentFile().mkdirs();
// Create and extract file from pdf
java.io.FileOutputStream output = new java.io.FileOutputStream(fileSpecification.getName(), true);
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer)))
output.write(buffer, 0, n);
// Close InputStream object
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
// Close Document object
pdfDocument.dispose();