PDF处理控件Aspose.PDF功能演示:使用Java将Base64字符串转换为PDF/JPG/PNG图像

Aspose.PDF 是一款高级PDF处理API,可以在跨平台应用程序中轻松生成,修改,转换,呈现,保护和打印文档。无需使用Adobe Acrobat。此外,API提供压缩选项,表创建和处理,图形和图像功能,广泛的超链接功能,图章和水印任务,扩展的安全控件和自定义字体处理。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.pdf 最新下载(qun:761297826)icon-default.png?t=N6B9https://www.evget.com/product/4118/download

Base64字符串以ASCII格式显示数据。它很流行在HTML网页或样式表中嵌入嵌入式图像和其他信息。在本文中,将学习使用Java 将Base64字符串转换为PDF或JPG和PNG之类的图像。

在这种情况下,可以通过将PDF自动转换为PowerPoint来避免手动复制/粘贴。为了处理这种情况,本文将学习如何:

Base64示例字符串

以下是Base64字符串的示例:

Base64示例字符串的预览

PDF处理控件Aspose.PDF功能演示:使用Java将Base64字符串转换为PDF/JPG/PNG图像

PDF处理控件Aspose.PDF功能演示:使用Java将Base64字符串转换为PDF/JPG/PNG图像

使用Java将Base64字符串转换为JPG或PNG图像

JPG,PNG和其他类型的图像有时会编码为Base64字符串,以实现安全可靠的数据通信和传输。此外,在探索PDF转换之前,我们需要学习使用Java将Base64字符串转换为JPG或PNG图像的知识。因此,需要按照以下步骤进行转换:

  • 由于字符串较长,因此将数据保存在TXT文件中
  • 读取字符串值
  • 替换前缀
  • 将Base64字符串保存为JPG或PNG图像

以下代码段显示了如何使用Java将Base64字符串转换为JPG或PNG图像:

// Save base64 string in TXT file because string is long
FileInputStream fis = new FileInputStream(dataDir + "base64.txt");
String base64 = IOUtils.toString(fis, "UTF-8");
String base64ImageString = base64.replace("data:image/png;base64,", "");
byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64ImageString);

// Convert Base64 to JPG or PNG Image
FileOutputStream fos = new FileOutputStream(dataDir + "Base64 to Image.jpg");
//FileOutputStream fos = new FileOutputStream(dataDir + "Base64 to Image.png");
try {
    fos.write(imageBytes);
}
finally {
    fos.close();
}

使用Java将Base64转换为PDF

我们已经了解了有关将Base64转换为PNG或JPG图像的知识。这实际上是将Base64转换为PDF文件的中间步骤。让我们再往前走一步。将字符串另存为光栅图像后,即可轻松将其转换为PDF。您可以按照以下步骤将Base64字符串转换为PDF:

  • 删除Base64字符串的前缀
  • 将Base64字符串转换为PNG或JPG图像
  • 将输出图像转换为PDF

以下代码片段显示了如何使用Java语言将Base64字符串转换为PDF:

// Save base64 string in TXT file because string is long
FileInputStream fis = new FileInputStream(dataDir + "base64.txt");
String base64 = IOUtils.toString(fis, "UTF-8");
String base64ImageString = base64.replace("data:image/png;base64,", "");
byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64ImageString);
String path = dataDir + "Base64 to Image.png";

// Convert Base64 to PNG or JPG Image
FileOutputStream fos = new FileOutputStream(path);
try {
    fos.write(imageBytes);
}
finally {
    fos.close();
}

BufferedImage readImage = null;
try {
	readImage = ImageIO.read(new File(path));
	int h = readImage.getHeight();
	int w = readImage.getWidth();
		
	com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
	com.aspose.pdf.Page page = doc.getPages().add();
	com.aspose.pdf.Image image = new com.aspose.pdf.Image();
	image.setFile(path);
	page.getPageInfo().setHeight(h);
	page.getPageInfo().setWidth(w);
	page.getPageInfo().getMargin().setBottom(0);
	page.getPageInfo().getMargin().setTop(0);
	page.getPageInfo().getMargin().setRight(0);
	page.getPageInfo().getMargin().setLeft(0);
	page.getParagraphs().add(image);
	doc.save(dataDir + "Base64-to-PDF.pdf");
} catch (Exception e) {
	readImage = null;
}

猜你喜欢

转载自blog.csdn.net/m0_67129275/article/details/131976477