Default PDF conversion page size?

GroupDocs.Conversion appears to retain the original source document’s page size when converting into PDF format.

However, it seems that it always defaults to “Letter” page size when converting source documents which don’t have page size info, e.g. text “.txt” and email “.msg” files.

Is it possible to specify a default target page size, e.g. “A4”, when the original source document doesn’t include page size info?

@jarrodwee

When converting documents to PDF format using GroupDocs.Conversion, it appears that the library retains the original page size for documents that have this information. However, for source documents that lack page size details, such as plain text files (.txt) and email files (.msg), the default page size defaults to “Letter”.

Currently, there is an ongoing investigation regarding the ability to specify a default target page size, such as “A4”, during the conversion process for these types of documents. This feature is being looked into, and updates will be provided as they become available.

For now, if you need to set a specific page size for your conversions, you may need to check for updates or workarounds in the documentation or community posts related to this feature.

If you have further questions or need assistance with specific code examples, feel free to ask!

Sources:
[1]: Missing documenation for com.groupdocs.conversion.options.convert.CommonConvertOptions

@jarrodwee

Here’s a proposed solution that should address your requirements effectively:

// Initialize the converter with the source file
using (var converter = new GroupDocs.Conversion.Converter("source file")) 
{
    // Create PDF conversion options
    var options = new PdfConvertOptions();

    // Set the default page size to A4
    // This will ensure that files without page size information (e.g., .txt, .msg) are converted to A4 instead of the default "Letter" size
    options.PageSize = PageSize.A4;

    // Convert the source file to PDF with the specified options
    converter.Convert(@"D:/output.pdf", options); 
}

In this solution, by setting options.PageSize = PageSize.A4; within the PdfConvertOptions, you’re specifying “A4” as the default page size for the conversion. This means that even when converting files like .txt or .msg (which lack page size information), GroupDocs.Conversion will apply “A4” instead of defaulting to “Letter.”
This approach should meet your need for consistent output page sizing. Let us know if you still face any issue.

But if the source file already defines a page size, e.g. Word Document which is Letter size, this code will convert it to A4 size right?
i.e. potentially messing up any layout that the original author formatted into the Word document.

@jarrodwee

Please take a look at the following workaround:

using (var converter = new Converter(@"D:/sample.txt")) // Create a new Converter instance for the specified source file
{
    // Start the conversion process
    converter.Convert(
        () => new FileStream(@"D:/txt a4.pdf", FileMode.Create), // Specify the output file stream for the converted PDF
        (string sourceFileName, FileType sourceFileType) => // Delegate to determine conversion options based on file type
        {
            // Check if the source file is a .txt file
            if (sourceFileType == WordProcessingFileType.Txt)
            {
                return new PdfConvertOptions // Return options with A4 page size for .txt files
                {
                    PageSize = PageSize.A4
                };
            }

            // Return default conversion options for other file types
            return new PdfConvertOptions();
        }
    );
}

The code initializes a converter to convert files to PDF format. It checks the file type of the source document: if the file is a .txt file, it sets the PDF conversion options to A4 page size. For other file formats, it returns default conversion options. This ensures that text files are converted with a specified page size, while other formats are processed without specific size adjustments.

Thanks, that was what I had in mind.

Guess I was hoping that there might be some undocumented default page size property that sticks if the source and/or specific options didn’t overwrite the value.

Btw, is page size always hardcoded to default to “Letter” page size?
Or is it dependent on some other OS setting?

1 Like

@jarrodwee

By default, all ConvertOptions with a PageSize property are set to PageSize.Unset. When PageSize is Unset, the converter attempts to retain the original page size of the document if it’s available. For formats that don’t have an inherent page size (like .txt files), the default will typically fall back to “Letter” size, but this is not an OS-dependent setting.

Thanks, meaning the “Letter” fall back is hardcoded?

If so, for consideration as future enhancement - will be good to allow this fall back page size to be configurable, thanks.

1 Like

@jarrodwee
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CONVERSIONNET-7272

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.