cyanfish / naps2

Scan documents to PDF and more, as simply as possible.
https://www.naps2.com
Other
2.58k stars 315 forks source link

How to batch scan pdf-files to specified folders by sdk #376

Closed 800903 closed 2 months ago

800903 commented 2 months ago

how to set one file per page ScanningContext scanningContext = new ScanningContext(new GdiImageContext()); var scanController = new ScanController(scanningContext); var devices = await scanController.GetDeviceList(Driver.Wia);
var options = new ScanOptions { Device = devices.First(), PageSize = PageSize.A4, PaperSource = NAPS2.Scan.PaperSource.Flatbed, Dpi = 600 }; var images =await scanController.Scan(options).ToListAsync(); var pdfExporter = new PdfExporter(scanningContext); await pdfExporter.Export("doc.pdf", images); ????? ??????? specified dynamic folders

800903 commented 2 months ago

I don't know if it will be effective.

public async Task ScanWithTwain(string filepath) {

        ScanningContext scanningContext = new ScanningContext(new GdiImageContext());
        var scanController = new ScanController(scanningContext);
        var devices = await scanController.GetDeviceList(Driver.Wia);
        var options = new ScanOptions
        {
            Device = devices.First(),
            PageSize = PageSize.A4,
            PaperSource = NAPS2.Scan.PaperSource.Flatbed,
            Dpi = 600
        };           
        var images =await scanController.Scan(options).ToListAsync();
        var pdfExporter = new PdfExporter(scanningContext);
        string currentFileName = "";
        int pages= 0;
        var compat = PdfCompat.Default;
        PdfSettings pdfSettings = new PdfSettings();
        pdfSettings.Compat = compat;
        pdfSettings.SinglePagePdfs = true;
        pdfSettings.DefaultFileName = "0000.pdf";
        var imagesByFile = pdfSettings.SinglePagePdfs ? images.Select(x => new[] { x }).ToArray(): new[] { images.ToArray() };
        foreach (var imagesForFile in imagesByFile)
        {
            {
                pages++;
                currentFileName = filepath + pages.ToString("D4") + ".pdf";
                await pdfExporter.Export(currentFileName, imagesForFile, new PdfExportParams(pdfSettings.Metadata, pdfSettings.Encryption, pdfSettings.Compat));
            }
        }

    }
cyanfish commented 2 months ago

First, I'd recommend updating to the latest SDK version, where PdfSettings isn't available because it doesn't do anything.

But as far as code, you don't need any special NAPS2 functionality, just a for loop:

int i = 1;
foreach (var image in images) {
    await pdfExporter.Export($"{i++:D4}.pdf", new[] { image });
}
cyanfish commented 2 months ago

Well, you'll need to put in a full file path depending on where you want to save it.

800903 commented 2 months ago

Here's the full code. public async Task ScanWithWia() { ScanningContext scanningContext = new ScanningContext(new GdiImageContext()); var scanController = new ScanController(scanningContext); var devices = await scanController.GetDeviceList(Driver.Wia); var options = new ScanOptions { Device = devices.First(), PageSize = PageSize.A4, PaperSource = NAPS2.Scan.PaperSource.Flatbed, Dpi = 600 }; var images = await scanController.Scan(options).ToListAsync(); var pdfExporter = new PdfExporter(scanningContext); int pages = 0; foreach (var image in images) { pages++; await pdfExporter.Export(@"D:\" + pages.ToString("D4") + ".pdf", new[] { image }); } }

800903 commented 2 months ago

ScanningContext scanningContext = new ScanningContext(new GdiImageContext()); // got it done must be add FileStorageManager object, Otherwise, the pdf file cannot be saved FileStorageManager fileStorageManager = new FileStorageManager(@"c:\temp");
scanningContext.FileStorageManager = fileStorageManager; //=======================================================