Convert or Save OneNote documents to PDF, Image and other formats in Java

Converting OneNote Document to PDF

Please note that you cannot set values against the Application and Producer fields, because of Aspose Ltd. and Aspose.Note for Java x.x will be displayed against these fields.

PDF documents are widely used as a standard format of exchanging documents between organizations, government sectors and individuals. It’s a popular format so developers are often asked to convert Microsoft OneNote documents to PDF documents. For this purpose, Aspose.Note for Java supports converting OneNote to PDF documents without using any other component. This topic illustrates how this conversion can be done.

Aspose.Note for Java offers the Document class that represents a OneNote file. The Document class exposes the Save method that can be called to convert a OneNote document into a PDF document. The PdfSaveOptions class provides options for creating the PDF such as PageIndex, PageCount and others.

This article shows how to:

Converting OneNote to PDF using the Default Options

The following example shows how to convert a OneNote into a PDF document using the default options. The default options create a PDF document of the maximum quality.

 1// For complete examples and data files, please go to https://github.com/aspose-note/Aspose.Note-for-Java
 2// Load the document into Aspose.Note.
 3
 4String inputFile = "Sample1.one";
 5Path inputPath = Utils.getPath(LoadDocIntoAsposeNoteUsingPdfsaveoptions.class, inputFile);
 6String outputFile = "LoadDocIntoAsposeNoteUsingPdfsaveoptions.pdf";
 7Path outputPath = Utils.getPath(LoadDocIntoAsposeNoteUsingPdfsaveoptions.class, outputFile);
 8
 9Document oneFile = new Document(inputPath.toString());
10
11// Save the document as PDF
12oneFile.save(outputPath.toString(), new PdfSaveOptions());
 1// For complete examples and data files, please go to https://github.com/aspose-note/Aspose.Note-for-Java
 2// Load the document into Aspose.Note.
 3
 4String inputFile = "Sample1.one";
 5Path inputPath = Utils.getPath(LoadDocIntoAsposeNoteUsingSaveformat.class, inputFile);
 6String outputFile = "LoadDocIntoAsposeNoteUsingSaveformat.pdf";
 7Path outputPath = Utils.getPath(LoadDocIntoAsposeNoteUsingSaveformat.class, outputFile);
 8
 9Document oneFile = new Document(inputPath.toString());
10
11// Save the document as PDF
12oneFile.save(outputPath.toString(), SaveFormat.Pdf);

Converting a Specified Page Range of OneNote to PDF

The following example shows how to convert a OneNote into a PDF document with a specified page range as provided by the PdfSaveOptions class. It sets the PageIndex and PageCount properties.

 1// Load the document into Aspose.Note.
 2String dataDir = Utils.getSharedDataDir(ConvertSpecificPageRangeToPdf.class) + "load/";
 3		
 4Document oneFile = new Document(dataDir + "Sample1.one");
 5
 6// Initialize PdfSaveOptions object
 7PdfSaveOptions opts = new PdfSaveOptions();
 8// Set page index
 9opts.setPageIndex(2);
10// Set page count
11opts.setPageCount(3);
12
13// Save the document as PDF
14oneFile.save(dataDir +"ConvertSpecificPageRangeToPdf_out.pdf", opts);
15System.out.println("File saved: " + dataDir + "ConvertSpecificPageRangeToPdf_out.pdf");

Converting OneNote to PDF with replacing of missing fonts

The following code example demonstrates how to set default font name.

 1        // The path to the documents directory.
 2        String dataDir = Utils.getSharedDataDir(SaveOneNoteDocToStream.class) + "load/";
 3
 4        // Load the document into Aspose.Note.
 5        Document oneFile = new Document(Paths.get(dataDir, "missing-font.one").toString());
 6
 7        // Save the document as PDF
 8        dataDir = dataDir + "SaveUsingDocumentFontsSubsystemWithDefaultFontName_out.pdf";
 9
10        PdfSaveOptions options = new PdfSaveOptions();
11        options.setFontsSubsystem(DocumentFontsSubsystem.usingDefaultFont("Times New Roman"));
12        oneFile.save(dataDir, options);

The following code example demonstrates how to set font from a file as default.

 1        // The path to the documents directory.
 2        String dataDir = Utils.getSharedDataDir(SaveOneNoteDocToStream.class) + "load/";
 3
 4        String fontFile = Paths.get(dataDir, "geo_1.ttf").toString();
 5
 6        // Load the document into Aspose.Note.
 7        Document oneFile = new Document(Paths.get(dataDir, "missing-font.one").toString());
 8
 9        // Save the document as PDF
10        dataDir = dataDir + "SaveUsingDocumentFontsSubsystemWithDefaultFontFromFile_out.pdf";
11
12        PdfSaveOptions options = new PdfSaveOptions();
13        options.setFontsSubsystem(DocumentFontsSubsystem.usingDefaultFontFromFile(fontFile));
14        oneFile.save(dataDir, options);

The following code example demonstrates how to set font from a stream as default.

 1        // The path to the documents directory.
 2        String dataDir = Utils.getSharedDataDir(SaveOneNoteDocToStream.class) + "load/";
 3
 4        String fontFile = Paths.get(dataDir, "geo_1.ttf").toString();
 5
 6        // Load the document into Aspose.Note.
 7        Document oneFile = new Document(Paths.get(dataDir, "missing-font.one").toString());
 8
 9        // Save the document as PDF
10        dataDir = dataDir + "SaveUsingDocumentFontsSubsystemWithDefaultFontFromStream_out.pdf";
11
12        InputStream stream = new FileInputStream(fontFile);
13        try
14        {
15            PdfSaveOptions options = new PdfSaveOptions();
16            options.setFontsSubsystem(DocumentFontsSubsystem.usingDefaultFontFromStream(stream));
17            oneFile.save(dataDir, options);
18        }
19        catch (Exception e)
20        {
21            stream.close();
22        }

Converting OneNote to Image

Aspose.Note supports converting OneNote file to images. To use this feature, import the Aspose.Note.Saving namespace into your application project. It has numerous valuable classes for rendering, for example ImageSaveOptions, PdfSaveOptions, and SaveOptions.

Aspose.Note API offers the Document class that represents a OneNote file. The Document class exposes the Save method that can be called to convert the OneNote file into an image format. The ImageSaveOptions class provides options for creating PNG, GIF, JPEG or BMP files, such as PageIndex, SaveFormat and others.

This article shows how to:

Converting OneNote to Image using the Default Options

The following example shows how to convert a OneNote into an image using the default options.

1// Load the document into Aspose.Note.
2String dataDir = Utils.getSharedDataDir(ConvertToImageUsingDefaultOptions.class) + "load/";
3Document oneFile = new Document(dataDir + "Sample1.one", new LoadOptions());
4
5// Save the document as Gif.
6oneFile.save(dataDir + "ConvertToImageUsingDefaultOptions_out.gif", SaveFormat.Gif);

Converting a Specific Page in a OneNote Document to Image

The following example shows how to convert a specific page in a OneNote file into an image using the ImageSaveOptions class. It sets the PageIndex properties.

 1// Load the document into Aspose.Note.
 2String dataDir = Utils.getSharedDataDir(ConvertSpecificPageToPngImage.class) + "load/";
 3Document oneFile = new Document(dataDir + "Sample1.one", new LoadOptions());
 4
 5// Initialize ImageSaveOptions object
 6ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Png);
 7// set page index
 8opts.setPageIndex(0);
 9
10// Save the document as PNG.
11oneFile.save(dataDir + "ConvertSpecificPageToPngImage_out.png", opts);

Set Output Image Resolution

The setResolution method of ImageSaveOptions allows specifying the output image resolution of the converted document.

1String dataDir = Utils.getSharedDataDir(SetOutputImageResolution.class) + "load/";
2
3Document doc = new Document(dataDir + "Sample1.one");
4
5ImageSaveOptions imageSaveOptions = new ImageSaveOptions(SaveFormat.Jpeg);
6
7imageSaveOptions.setResolution(120);
8
9doc.save(dataDir + "SetOutputImageResolution_out.jpeg", imageSaveOptions);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.