public ExtractForm(string path) { // initialize the components InitializeComponent(); InitializeAdditionalStatusStripComponents(); // store the path and filter text and set the default locations _filePath = Path.GetFullPath(path); _filterFormatString = saveFileDialog.Filter; Text = string.Format(Text, Application.ProductName, Path.GetFileName(_filePath)); var directory = Path.GetDirectoryName(path); saveFileDialog.InitialDirectory = directory; folderBrowserDialog.SelectedPath = directory; // initialize the various formats _formatTextFormatString = toolStripDropDownButtonFormat.Text; foreach (var format in Enumerable.Repeat(ConvertFormat.Pdf, 1).Concat(ConvertFormat.GetApplicable(DocumentType.PortableDocumentFormat))) { toolStripDropDownButtonFormat.DropDownItems.Add(format.Name).Tag = format; } SetFormat(ConvertFormat.Pdf); // simulate a selection and track-bar change ListViewPages_SelectedIndexChanged(this, EventArgs.Empty); TrackBarZoom_ValueChanged(this, EventArgs.Empty); // hide the status elements ShowStatus(false, false); }
static ConvertFormat() { _formats.Add(Pdf = new ConvertFormat(Resources.Converter_FormatPdf, "pdf", true, DocumentType.Any & ~DocumentType.PortableDocumentFormat, "-sDEVICE=pdfwrite")); _formats.Add(OptimizedPdf = new ConvertFormat(Resources.Converter_FormatOptimizedPdf, "optimized.pdf", true, DocumentType.PortableDocumentFormat, "-dColorConversionStrategy=/RGB", "-dColorImageDownsampleThreshold=1.5", "-dColorImageDownsampleType=/Bicubic", "-dColorImageFilter=/DCTEncode", "-dColorImageResolution=96", "-dCompatibilityLevel=1.4", "-dCompressPages=true", "-dConvertCMYKImagesToRGB=true", "-dDetectDuplicateImages=true", "-dDownsampleColorImages=true", "-dDownsampleGrayImages=true", "-dDownsampleMonoImages=true", "-dEmbedAllFonts=true", "-dFastWebView=true", "-dGrayImageDownsampleThreshold=1.5", "-dGrayImageDownsampleType=/Bicubic", "-dGrayImageFilter=/DCTEncode", "-dGrayImageResolution=96", "-dMaxInlineImageSize=0", "-dMaxShadingBitmapSize=16000", "-dMonoImageDownsampleThreshold=1.5", "-dMonoImageDownsampleType=/Bicubic", "-dMonoImageFilter=/CCITTFaxEncode", "-dMonoImageResolution=192", "-dOptimize=true", "-dUseFlateCompression=true", "-r192", "-sDEVICE=pdfwrite" )); _formats.Add(Ps = new ConvertFormat(Resources.Converter_FormatPs, "ps", true, DocumentType.PortableDocumentFormat | DocumentType.EncapsulatedPostScript, "-sDEVICE=ps2write")); _formats.Add(Eps = new ConvertFormat(Resources.Converter_FormatEps, "eps", false, DocumentType.PortableDocumentFormat | DocumentType.PostScript, "-sDEVICE=eps2write")); _formats.Add(Png = new ConvertFormat(Resources.Converter_FormatPng, "png", false, new PngFormatDialog())); _formats.Add(Jpeg = new ConvertFormat(Resources.Converter_FormatJpeg, "jpg", false, new JpegFormatDialog())); _formats.Add(Tiff = new ConvertFormat(Resources.Converter_FormatTiff, "tif", true, new TiffFormatDialog())); _formats.Add(Bmp = new ConvertFormat(Resources.Converter_FormatBmp, "bmp", false, new BmpFormatDialog())); }
private Converter(ConvertFormat format, IEnumerable <string> files) { // store the format and title _format = format; _title = string.Format(Resources.Converter_DialogTitle, Application.ProductName, _format.Name); // create the process dialog _progressDialog = (Native.IProgressDialog)Activator.CreateInstance(Type.GetTypeFromCLSID(Native.CLSID_ProgressDialog, true)); _progressDialog.SetTitle(_title); _progressDialog.SetCancelMsg(Resources.Converter_CancelMessage, IntPtr.Zero); // start looking for files ThreadPool.QueueUserWorkItem(AsyncInitialize, files); }
public void Convert(ConvertFormat format, Action pageCompletedCallback = null, Func <bool> cancellationCallback = null) { // check the arguments and state if (format == null) { throw new ArgumentNullException(nameof(format)); } // convert the document lock (this) { CheckDisposed(); DoConvert(format, pageCompletedCallback, cancellationCallback); } }
protected override void DoConvert(ConvertFormat format, Action pageCompletedCallback, Func <bool> cancellationCallback) { // only support PDF conversion if (format != ConvertFormat.Pdf || !format.SupportsSingleFile) { throw new NotSupportedException(); } // convert all pages into a PDF using (var pdfDocument = new PdfDocument()) { for (var i = 0; i < PageCount; i++) { // select the page from the image if (PageCount > 1) { _image.SelectActiveFrame(FrameDimension.Page, i); if (cancellationCallback != null && cancellationCallback()) { throw new OperationCanceledException(); } } // draw the page into the document var page = pdfDocument.AddPage(); using (var imageWrapper = new XImageWrapper(_image)) { page.Width = imageWrapper.PointWidth; page.Height = imageWrapper.PointHeight; using (var gc = XGraphics.FromPdfPage(page)) { gc.DrawImage(imageWrapper, 0, 0, imageWrapper.PointWidth, imageWrapper.PointHeight); } } // check for cancellation and notify the caller if (cancellationCallback != null && cancellationCallback()) { throw new OperationCanceledException(); } pageCompletedCallback?.Invoke(); } // save the document pdfDocument.Save(Path.ChangeExtension(FilePath, format.FileExtension)); } }
public static void Run(IEnumerable <string> files, ConvertFormat format) { // check the arguments if (files == null) { throw new ArgumentNullException(nameof(files)); } if (format == null) { throw new ArgumentNullException(nameof(format)); } // show the dialog and run the converter if (format.ShowDialog()) { new Converter(format, files).Run(); } }
protected override void DoConvert(ConvertFormat format, Action pageCompletedCallback, Func <bool> cancellationCallback) { // convert the file using Ghostscript var outputFile = format.SupportsSingleFile ? Path.ChangeExtension(FilePath, format.FileExtension) : Path.Combine(Path.GetDirectoryName(FilePath), new StringBuilder(Path.GetFileNameWithoutExtension(FilePath)).Append("_%d.").Append(format.FileExtension).ToString()); using (var ghostscript = new Ghostscript(format.GetArguments(outputFile))) { if (cancellationCallback != null) { ghostscript.Poll += (s, e) => e.Cancel = cancellationCallback(); } if (format == ConvertFormat.Pdf) { ghostscript.Run(".setpdfwrite"); } DoRunInitialize(ghostscript); for (var i = 0; i < PageCount; i++) { DoRunPage(ghostscript, i + 1); pageCompletedCallback?.Invoke(); } } }
protected abstract void DoConvert(ConvertFormat format, Action pageCompletedCallback, Func <bool> cancellationCallback);