Saving a File to Local Disk

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Conversion MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-conversion -- dnx GroupDocs.Conversion.Mcp --yes
Folders, licensing and more →

To save the conversion results to a local disk, you can use the convert(String filePath, ConvertOptions convertOptions) method. This implementation requires two parameters:

  • filePath – The path where the output file will be saved. It can be an absolute path (e.g., C:\files\converted.pdf) or a relative path (e.g., converted.pdf).
  • convertOptions – Options specifying the conversion settings, such as the output format or specific configurations related to the conversion process.
Note
  • If the specified output file does not exist, it will be automatically created.
  • If the file already exists, it may be overwritten unless specified otherwise in the options.

The following code snippet demonstrates how to convert a Word document (.docx) to a PDF file and save it to a specified directory on your local disk:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;

public class ConvertDocxToPdf {
    public static void convert() {
        // Specify the source file location
        Converter converter = new Converter("c:\\files\\business-plan.docx");

        // Define conversion options (in this case, converting to PDF)
        PdfConvertOptions options = new PdfConvertOptions();

        // Specify the output file location and perform the conversion
        converter.convert("c:\\files\\business-plan.pdf", options);
    }

    public static void main(String[] args){
        convert();
    }
}

business-plan.docx is sample file used in this example. Click here to download it.

business-plan.pdf is converted PDF document. Click here to download it.

Explanation:

  • Source File Specification: The Converter object is initialized with the path to the source document (business-plan.docx).
  • Conversion Options: The PdfConvertOptions object is created to configure settings specific to PDF conversion. Depending on the library you are using, you may have options to customize aspects such as page size, orientation, or compression.
  • Saving the Converted File: The convert() method is called with the desired output file path (c:\\files\\business-plan.pdf) and the conversion options (options). The file will be saved in the specified directory.