Knowledgebase (2311)
Children categories
Combining multiple images into a single PDF document is an efficient method for those who want to store or distribute their images in a more organized way. Converting these images into a single PDF file not only saves storage space but also ensures that all images are kept together in one place, making it easier and more convenient to share or transfer them. In this article, you will learn how to merge several images into a single PDF document in Java using Spire.PDF for Java.
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.12.16</version>
</dependency>
</dependencies>
Convert Multiple Images into a Single PDF Document in Java
In order to convert all the images in a folder to a PDF, we iterate through each image, add a new page to the PDF with the same size as the image, and then draw the image onto the new page. The following are the detailed steps.
- Create a PdfDocument object.
- Set the page margins to zero using PdfDocument.getPageSettings().setMargins() method.
- Get the folder where the images are stored.
- Iterate through each image file in the folder, and get the width and height of a specific image.
- Add a new page that has the same width and height as the image to the PDF document using PdfDocument.getPages().add() method.
- Draw the image on the page using PdfPageBase.getCanvas().drawImage() method.
- Save the document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;
import java.awt.*;
import java.io.File;
public class ConvertMultipleImagesIntoPdf {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Set the page margins to 0
doc.getPageSettings().setMargins(0);
//Get the folder where the images are stored
File folder = new File("C:/Users/Administrator/Desktop/Images");
//Iterate through the files in the folder
for (File file : folder.listFiles())
{
//Load a particular image
PdfImage pdfImage = PdfImage.fromFile(file.getPath());
//Get the image width and height
int width = pdfImage.getWidth();
int height = pdfImage.getHeight();
//Add a page that has the same size as the image
PdfPageBase page = doc.getPages().add(new Dimension(width, height));
//Draw image at (0, 0) of the page
page.getCanvas().drawImage(pdfImage, 0, 0, width, height);
}
//Save to file
doc.saveToFile("CombineImagesToPdf.pdf");
doc.dispose();
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
SVG (Scalable Vector Graphics) is a vector graphics format that can be scaled up or down without losing quality, which is especially useful when images need to be placed on large billboards/stadium screens, or when small logos and icons need to be saved. By the same token, if you want to make PDF pages infinitely scalable, you can convert them to SVG format. This article will demonstrate how to convert PDF to SVG in C++ using Spire.PDF for C++.
Install Spire.PDF for C++
There are two ways to integrate Spire.PDF for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.PDF for C++ in a C++ Application
Convert a PDF File to SVG in C++
Spire.PDF for C++ offers the PdfDocument->SaveToFile() method to convert each page in a PDF file to a single SVG file. The following are the detailed steps.
- Create a PdfDocument instance.
- Load a sample PDF file using PdfDocument->LoadFromFile() method.
- Convert the PDF file to SVG using PdfDocument->SaveToFile(LPCWSTR_S filename, FileFormat::SVG) method.
- C++
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main()
{
//Specify the input and output files
wstring inputFile = L"Data\\sample1.pdf";
wstring outputFile = L"ToSVG\\ToSVG.svg";
//Create a PdfDcoument instance
intrusive_ptr<PdfDocument> pdf = new PdfDocument();
//Load a sample PDF document
pdf->LoadFromFile(inputFile.c_str());
//Convert the PDF document to SVG
pdf->SaveToFile(outputFile.c_str(),FileFormat::SVG);
pdf->Close();
}

Convert Specified PDF Pages to SVG in C++
Spire.PDF for C++ also allows you to convert specified pages in a PDF file to SVG files. During conversion, if you want to specify the width and height of the output SVG file, you can use the PdfDocument->GetConvertOptions()->SetPdfToSvgOptions() method. The following are the detailed steps to convert a selected PDF page to SVG with custom width and height.
- Create a PdfDocument instance.
- Load a sample PDF file using PdfDocument->LoadFromFile() method.
- Specify the width and height of the output SVG file using PdfDocument->GetConvertOptions()->SetPdfToSvgOptions(float wPixel, float hPixel) method.
- Convert selected PDF pages to SVG using PdfDocument->SaveToFile(LPCWSTR_S filename, int startIndex, int endIndex, FileFormat::SVG) method.
- C++
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main()
{
//Specify the input and output files
wstring inputFile = L"Data\\sample1.pdf";
wstring outputFile = L"PDFPagetoSVG.svg";
//Create a PdfDcoument instance
intrusive_ptr<PdfDocument> pdf = new PdfDocument();
//Load a sample PDF document
pdf->LoadFromFile(inputFile.c_str());
//Specify the width and height of the output SVG file
pdf->GetConvertOptions()->SetPdfToSvgOptions(900, 1200);
//Convert the specified PDF page to SVG
pdf->SaveToFile(outputFile.c_str(), 2, 2, FileFormat::SVG);
pdf->Close();
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
HTML is a markup language for creating websites or other web applications. In daily work, you might need to convert HTML to images for various reasons, such as creating thumbnails of web pages to use in search engine results pages or social media posts, generating snapshots of your html files for archiving or other purposes, and so on. In this article, you will learn how to convert Html to JPG, PNG, or BMP images in C++ using Spire.Doc for C++.
Install Spire.Doc for C++
There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.Doc for C++ in a C++ Application
Convert HTML to JPG, PNG and BMP Images in C++
To complete the task, you’ll need to load an HTML file first and then convert it to images using Document->SaveImageToStreams() method. These images can also be further saved as JPEG, PNG, BMP, EMF, GIF or WMF image formats. The following are the detailed steps.
- Create a Document instance.
- Load a sample html file using Document->LoadFromFile(LPCWSTR_S filename, FileFormat::Html, XHTMLValidationType::None) method.
- Convert the html file to an image stream using Document->SaveImageToStreams() method.
- Convert the image stream to an Image object using Image::FromStream(intrusive_ptr<Stream> stream) method.
- Save the Image object as a JPEG, PNG, BMP, or other image formats using Image->Save(LPCWSTR_S filename, ImageFormat format) method.
- C++
#include "Spire.Doc.o.h"
using namespace std;
using namespace Spire::Doc;
int main() {
//Specify the input file path
wstring inputFile = L"Data\\sample.html";
//Create a Document instance
intrusive_ptr<Document> document = new Document();
//Load a sample html file from disk
document->LoadFromFile(inputFile.c_str(), FileFormat::Html, XHTMLValidationType::None);
//Convert the html to image streams
intrusive_ptr<Stream> imageStream = document->SaveImageToStreams(0,ImageType::Bitmap);
//Save the image stream as a PNG, JPG or BMP image
intrusive_ptr<Image> image = Image::FromStream(imageStream);
image->Save(L"HtmlToPNG.png", ImageFormat::GetPng());
image->Save(L"HtmlToJPG.jpg", ImageFormat::GetJpeg());
image->Save(L"HtmlToImage.bmp", ImageFormat::GetBmp());
document->Close();
imageStream->Dispose();
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.