如何用Java创建PDF文件?
参考以下步骤:
具体代码如下:
// Initialize document object
Document document = new Document();
//Add page
Page page = document.getPages().add();
// Add text to new page
page.getParagraphs().add(new TextFragment("Hello World!"));
// Save updated PDF
document.save("HelloWorld_out.pdf");
具体代码如下:
package com.aspose.pdf.examples;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.aspose.pdf.*;
import com.aspose.pdf.internal.pcl.util.BufferedInputStream;
public final class BasicOperationsOpen {
private BasicOperationsOpen() {
}
private static Path _dataDir = Paths.get("/home/admin1/pdf-examples/Samples");
public static void main(String[] args) {
OpenDocument();
OpenDocumentStream();
OpenDocumentWithPassword();
}
public static void OpenDocument() {
String fileName = _dataDir+"/tourguidev2_gb_tags.pdf";
Document pdfDocument = new Document(fileName);
System.out.println("Pages +" + pdfDocument.getPages().size());
}
具体代码如下:
public static void OpenDocumentStream() {
String remoteURL = "https://www.sj.se/content/dam/SJ/pdf/Engelska/";
String fileName = "SJPR0033_Folder_Utland_16sid_ENG_web3.pdf";
try (BufferedInputStream in = new BufferedInputStream(new java.net.URL(remoteURL + fileName).openStream())) {
InputStream inputStream = in;
Document pdfDocument = new Document(inputStream);
System.out.println("Pages +" + pdfDocument.getPages().size());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void OpenDocumentWithPassword() {
String fileName = "C:\\tmp\\DocSite.pdf";
String pwdStr= "*****";
try {
Document pdfDocument = new Document(fileName, password);
System.out.println("Pages +" + pdfDocument.getPages().size());
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
具体代码如下:
package com.aspose.pdf.examples;
import java.io.FileOutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.aspose.pdf.*;
public final class BasicOperationsSave {
private BasicOperationsSave() {
}
private static Path _dataDir = Paths.get("/home/admin1/pdf-examples/Samples");
public static void main(String[] args) {
SaveDocument();
SaveDocumentStream();
SaveDocumentAsPDFx();
}
public static void SaveDocument() {
String originalFileName = _dataDir + "/SimpleResume.pdf";
String modifiedFileName = _dataDir + "/SimpleResumeModified.pdf";
Document pdfDocument = new Document(originalFileName);
// make some manipation, i.g add new empty page
pdfDocument.getPages().add();
pdfDocument.save(modifiedFileName);
}
具体代码如下:
public static void SaveDocumentStream() {
String originalFileName = _dataDir + "/SimpleResume.pdf";
String modifiedFileName = _dataDir + "/SimpleResumeModified.pdf";
Document pdfDocument = new Document(originalFileName);
// make some manipation, i.g add new empty page
pdfDocument.getPages().add();
try {
pdfDocument.save(new FileOutputStream(modifiedFileName));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
具体代码如下:
// @RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET)
// public void getFile(@PathVariable("file_name") String fileName, HttpServletResponse response) {
// try {
// response.setContentType("application/pdf");
// // get your file as InputStream
// InputStream is = new FileInputStream(_dataDir + fileName);
// // copy it to response's OutputStream
// org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
// response.flushBuffer();
// } catch (IOException ex) {
// log.info("Error writing file to output stream. Filename was '{}'", fileName, ex);
// throw new RuntimeException("IOError writing file to output stream");
// }
// }
关于PDF/A格式和PDF/X格式
PDF/A是ISO的标准版本,用于将电子文档归档和长期保存。PDF/A禁止了一些功能,比如字体链接和加密。
PDF/X是PDF ISO标准的一个子集。PDF/X的目的是促进图形交换,因此它有一系列与打印相关的要求,而这些要求不适用于标准PDF文件。
用convert方法进行转换,具体代码如下:
public static void SaveDocumentAsPDFx() {
Document pdfDocument = new Document("../../../Samples/SimpleResume.pdf");
pdfDocument.getPages().add();
pdfDocument.convert(new PdfFormatConversionOptions(PdfFormat.PDF_X_3));
pdfDocument.save("../../../Samples/SimpleResume_X3.pdf");
}
}