Converting text files into .png format

Last post 11-16-2011, 2:43 AM by alexey.noskov. 1 replies.
Sort Posts: Previous Next
  •  11-15-2011, 3:18 PM 342431

    Converting text files into .png format .NET

    Hi, 

    I am wondering if Aspose can convert text files into .pdf format, just like it converts the word files into .pdf format. I need the images to be in grayscale 8 or 16 bit. 

    I am also wondering if Aspose can convert image formats such as .gif, .bmp, .tif, .jpg, .png files into grayscale 8bit or 16 bit formats? I have seen that the library just does a conversion but not to grayscale. 

    Thanks, 
    I need the info asap! 
     
  •  11-16-2011, 2:43 AM 342547 in reply to 342431

    Re: Converting text files into .png format

    Hi

     

    Thanks for your request. Sure, you can convert text documents to images using Aspose.Words. Here you can find a code that shows how you can load TXT documents into Document object:

    http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-load-plain-text-txt-files.html

    Then, you can convert the Document to PNG and make it grayscale:

    http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.saving.imagesaveoptions.imagecolormode.html

     

    Code will look like the following:

     

    [Test]

    public void Test001()

    {

        TxtToGrayScalePng(@"Test001\in.txt", @"Test001\");

    }

     

    /// <summary>

    /// Converts txt document to png images.

    /// </summary>

    private static void TxtToGrayScalePng(string inputFileName, string outputDirectory)

    {

        // Load TXT document into Document object.

     

        // This object will help us generate the document.

        DocumentBuilder builder = new DocumentBuilder();

     

        // You might need to specify a different encoding depending on your plain text files.

        using (StreamReader reader = new StreamReader(inputFileName, Encoding.UTF8))

        {

            // Read plain text "lines" and convert them into paragraphs in the document.

            string line = null;

            while ((line = reader.ReadLine()) != null)

            {

                builder.Writeln(line);

            }

        }

     

        // Save each page of the document as grayscale PNG.

        ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.Png);

        opt.PageCount = 1;

        opt.ImageColorMode = ImageColorMode.Grayscale;

        for (int i = 0; i < builder.Document.PageCount; i++)

        {

            opt.PageIndex = i;

            string outputFileName = string.Format("page_{0}.png", i);

            builder.Document.Save(Path.Combine(outputDirectory, outputFileName), opt);

        }

    }

     

    You can use code suggested here you insert your existing images into Document object:

    http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-convert-an-image-to-pdf.html

    And save the document’s pages as grayscale PNG as shown in the previous code example.

     

    Hope this helps.

     

    Best regards,


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
View as RSS news feed in XML