Base64 字符串以 ASCII 格式显示数据。它在 HTML 网页或样式表中嵌入内嵌图像和其他信息很受欢迎。在本文中,我们将学习如何使用 Java 将 Base64 字符串转换为 PDFJPGPNG

Java Base64 to PDF Converter API - 安装

您可以从 Releases 部分下载 Aspose.PDF for Java API 的 JAR 文件,或使用以下 Maven 配置:

存储库:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository

依赖:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>20.7</version>
</dependency>

所以 Aspose.PDF for Java API 已经配置好了。在继续转换 Base64 字符串之前,让我们看一下示例数据:

Base64 转换的示例字符串

您可以从 这里 下载 Base64 字符串。

Base64 示例字符串的预览

Base64 转 PNG JPG

现在,让我们继续下面的 Base64 字符串转换场景:

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

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

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

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

// 将 base64 字符串保存在 TXT 文件中,因为字符串很长
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);

// 将 Base64 转换为 JPG 或 PNG 图像
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:

  1. 移除 Base64 字符串的前缀
  2. 将 Base64 字符串转换为 PNG 或 JPG 图像
  3. 然后将输出图像转换为 PDF

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

// 将 base64 字符串保存在 TXT 文件中,因为字符串很长
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";

// 将 Base64 转换为 PNG 或 JPG 图像
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;
}

结论

综上所述,我们已经探索了如何将 Base64 字符串转换为 PDF、PNG 和 JPG 图像。使用 Java 此外,生成 PDF 文件将为进一步转换打开更多选项。就像一个 PDF 文件可以转换成 SVG、XPS、DOCX 和许多其他格式一样。有兴趣探索其他可能性吗?不要犹豫,通过免费支持论坛与我们讨论这个问题。

也可以看看