Free Support Forum - aspose.com

Word to Scanned PDF Converter | Scan Word Document to PDF Offline (C# .NET)

By converting Word document to scanned PDF I mean production of such a PDF file from which readers, viewers or users will not be able to select or copy content (Text, Images, Tables, Equations, Form Fields etc.). To meet this scan Word to PDF requirement programmatically, we will simply first convert all pages of Word document to JPEG image streams in memory, then stich JPEG streams all together and convert this set of JPEG images to form a scanned PDF. Essentially, each page of scanned PDF will comprise of one Image reflecting the content of each Page of source Word document.

Scan Word DOCX to PDF | C# Code of Aspose.Words for .NET API

You can build logic on the following C# code of Aspose.Words for .NET SDK to automate batch scanning of Word files to PDF in your multi-threaded desktop or web applications.

string word_File_Path = "C:\\Temp\\word.docx";
string scanned_Pdf_File_Path = "C:\\Temp\\scanned.pdf";
 
Word_To_PDF_Scanner(word_File_Path, scanned_Pdf_File_Path);

public static void Word_To_PDF_Scanner(string word_file, string scanned_PDF_File)
{
    // Load Word document from file on disk
    Document doc = new Document(word_file);
 
    #region Word document to JPEG image memory streams
 
    ImageSaveOptions jpeg_Opts = new ImageSaveOptions(SaveFormat.Jpeg);
    PageRange pageRange = new PageRange(0, doc.PageCount - 1);
    jpeg_Opts.PageSet = new PageSet(pageRange);
 
    Word_To_Jpeg_Images jpeg_Handler = new Word_To_Jpeg_Images();
    jpeg_Opts.PageSavingCallback = jpeg_Handler;
 
    MemoryStream memoryStream = new MemoryStream();
    doc.Save(memoryStream, jpeg_Opts);
 
    #endregion
 
    #region JPEG files to Scanned PDF conversion
 
    Document scanned_Pdf = new Document();
    scanned_Pdf.RemoveAllChildren();
 
    foreach (MemoryStream jpeg_Stream in jpeg_Handler.jpeg_Streams)
    {
        using (Image jpg_image = Image.FromStream(jpeg_Stream))
        {
            Document image_Doc = new Document();
            DocumentBuilder pdf_builder = new DocumentBuilder(image_Doc);
 
            PageSetup ps = pdf_builder.PageSetup;
            ps.PageWidth = ConvertUtil.PixelToPoint(jpg_image.Width, jpg_image.HorizontalResolution);
            ps.PageHeight = ConvertUtil.PixelToPoint(jpg_image.Height, jpg_image.VerticalResolution);
 
            // Insert the JPEG image into the document and position it at the top left corner of the page.
            pdf_builder.InsertImage(
                jpg_image,
                RelativeHorizontalPosition.Page,
                0,
                RelativeVerticalPosition.Page,
                0,
                ps.PageWidth,
                ps.PageHeight,
                WrapType.None);
 
            scanned_Pdf.AppendDocument(image_Doc, ImportFormatMode.KeepSourceFormatting);
        }
    }
 
    scanned_Pdf.Save(scanned_PDF_File);
 
    #endregion
}

private class Word_To_Jpeg_Images : IPageSavingCallback
{
    public ArrayList jpeg_Streams = new ArrayList();
    public void PageSaving(PageSavingArgs args)
    {
        args.PageStream = new MemoryStream();
        args.KeepPageStreamOpen = true;
        jpeg_Streams.Add(args.PageStream);
    }
}

Word to Scanned PDF Console Application - Demo

The following video is just the demonstration of how to convert a Word RTF document to scanned PDF by using above C# code of Aspose.Words for .NET API.