diff --git a/NAPS2.Sdk.Samples/PdfImportSample.cs b/NAPS2.Sdk.Samples/PdfImportSample.cs new file mode 100644 index 000000000..e7228648d --- /dev/null +++ b/NAPS2.Sdk.Samples/PdfImportSample.cs @@ -0,0 +1,40 @@ +using NAPS2.Images.Gdi; +using NAPS2.Pdf; +using NAPS2.Scan; + +namespace NAPS2.Sdk.Samples; + +public class PdfImportSample +{ + public static async Task PdfImportAppendAndExport() + { + // Importing PDFs requires the optional NAPS2.Pdfium.Binaries Nuget package to be installed. + + // Set up + using var scanningContext = new ScanningContext(new GdiImageContext()); + var pdfImporter = new PdfImporter(scanningContext); + var images = new List(); + + // Import original PDF + await foreach (var image in pdfImporter.Import("original.pdf")) + { + images.Add(image); + } + + // Set up scanning + var controller = new ScanController(scanningContext); + var devices = await controller.GetDeviceList(); + var options = new ScanOptions { Device = devices.First() }; + + // Append newly scanned images + await foreach (var image in controller.Scan(options)) + { + images.Add(image); + } + + // Save all images to a new PDF + // This will retain the structure of the original PDF pages (i.e. they aren't rasterized to flat images) + var pdfExporter = new PdfExporter(scanningContext); + await pdfExporter.Export("doc.pdf", images); + } +} \ No newline at end of file diff --git a/NAPS2.Sdk/README.md b/NAPS2.Sdk/README.md index c6fd79d93..eca69d492 100644 --- a/NAPS2.Sdk/README.md +++ b/NAPS2.Sdk/README.md @@ -37,8 +37,6 @@ NAPS2.Sdk is modular, and depending on your needs you may have to reference a di ## Usage -See the [Samples](https://github.com/cyanfish/naps2/tree/master/NAPS2.Sdk.Samples) for more examples and description. - ```c# // Set up using var scanningContext = new ScanningContext(new GdiImageContext()); @@ -69,6 +67,15 @@ var pdfExporter = new PdfExporter(scanningContext); await pdfExporter.Export("doc.pdf", images); ``` +More [samples](https://github.com/cyanfish/naps2/tree/master/NAPS2.Sdk.Samples): +- ["Hello World" scanning](https://github.com/cyanfish/naps2/blob/master/NAPS2.Sdk.Samples/HelloWorldSample.cs) +- [Scan and save to PDF/images](https://github.com/cyanfish/naps2/blob/master/NAPS2.Sdk.Samples/ScanAndSaveSample.cs) +- [Scan with TWAIN drivers](https://github.com/cyanfish/naps2/blob/master/NAPS2.Sdk.Samples/TwainSample.cs) +- [Scan to System.Drawing.Bitmap](https://github.com/cyanfish/naps2/blob/master/NAPS2.Sdk.Samples/ScanToBitmapSample.cs) +- [Import and export PDFs](https://github.com/cyanfish/naps2/blob/master/NAPS2.Sdk.Samples/PdfImportSample.cs) +- [Store image data on the filesystem](https://github.com/cyanfish/naps2/blob/master/NAPS2.Sdk.Samples/FileStorageSample.cs) +- [Share scanners on the local network](https://github.com/cyanfish/naps2/blob/master/NAPS2.Sdk.Samples/NetworkSharingSample.cs) + ## Drivers | | Windows | Mac | Linux |