Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created June 29, 2026 05:22
Show Gist options
  • Select an option

  • Save aspose-com-gists/c1764379e513d4ce605cc955c13999c1 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/c1764379e513d4ce605cc955c13999c1 to your computer and use it in GitHub Desktop.
TXT to JPG Conversion in Java: High Quality STEP by STEP
import com.aspose.drawing.*;
import com.aspose.drawing.image.*;
import com.aspose.drawing.text.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
public class TxtToJpgConverter {
public static void main(String[] args) throws Exception {
// 1. Load TXT file
String txtPath = "sample.txt";
String text = new String(Files.readAllBytes(Paths.get(txtPath)), StandardCharsets.UTF_8);
// 2. Create document and add a page
DrawingDocument doc = new DrawingDocument();
doc.addPage();
// 3. Add text to the page
TextFragment fragment = new TextFragment(text);
fragment.getTextState().setFontSize(12);
fragment.getTextState().setFontName("Arial");
doc.getPages().get(0).addTextFragment(fragment);
// 4. Set rendering options for high quality JPG
ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.JPEG);
saveOptions.setResolution(300); // 300 DPI
saveOptions.setJpegQuality(95); // High quality
// 5. Save the result
String outputPath = "output.jpg";
doc.save(outputPath, saveOptions);
System.out.println("Conversion completed: " + outputPath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment