コード例 #1
0
ファイル: Program.cs プロジェクト: jmf70/PDFService
        private static void combinaPDF(List<string> files)
        {
            string originalFile = files[0];
            string destFile = "";
            PdfDocument output;

            //try
            //{
            //    output = PdfReader.Open(originalFile, PdfDocumentOpenMode.Modify);
            //}
            //catch (PdfSharp.PdfSharpException)
            //{
            //    destFile = System.IO.Path.GetTempFileName();
            //    System.IO.File.Copy(originalFile, destFile, true);
            //    output = new PdfDocument(destFile);
            //}

            destFile = System.IO.Path.GetTempFileName();
            output = new PdfDocument(destFile);

            foreach (string file in files)
            {
                //if (file != originalFile || !string.IsNullOrWhiteSpace(destFile))
                //{
                    PdfDocument input = PdfReader.Open(file, PdfDocumentOpenMode.Import);
                    int count = input.PageCount;
                    for (int idx = 0; idx < count; idx++)
                    {
                        PdfPage page = input.Pages[idx];
                        output.AddPage(page);
                    }
                    System.IO.File.Delete(file);
                //}
            }
            output.Save(originalFile);
            output.Close();
            output.Dispose();

            if (!string.IsNullOrWhiteSpace(destFile))
            {
                System.IO.File.Delete(originalFile);
                System.IO.File.Copy(destFile, originalFile, true);
                System.IO.File.Delete(destFile);
            }
        }
コード例 #2
0
        public bool ExportToPDF(string FileNamePDF, bool ShowFile)
        {
            string strFileTmpPdf = string.Empty ;
            PdfDocument objDocument = null;
            FileInfo objFileInfo = null;
            PdfPage objPage = null;
            XImage objImage = null;
            try
            {
            // Create a new PDF document
            objDocument = new PdfDocument();
            objDocument.Info.Title = "Criado por OPUS DIGITALIZADOR";
            objDocument.Info.Author = "OPUS Comérico Exterior Ltda";
            objDocument.Info.Creator = "OPUS DIGITALIZADOR";

            //Se o diretório de PDF não existir eu o crio
            if (!Directory.Exists(System.Configuration.ConfigurationSettings.AppSettings["DIRPDF"].ToString())) {
                Directory.CreateDirectory (System.Configuration.ConfigurationSettings.AppSettings["DIRPDF"].ToString());
            }

            strFileTmpPdf = System.Configuration.ConfigurationSettings.AppSettings["DIRPDF"].ToString()+ FileNamePDF;
            foreach (string strFileName in System.IO.Directory.GetFiles(System.Configuration.ConfigurationSettings.AppSettings["DIRIMGSCANNER"].ToString() + FileNamePDF))
            {
                objFileInfo = new FileInfo(strFileName);

                if (objFileInfo.Extension.ToUpper() == System.Configuration.ConfigurationSettings.AppSettings["EXTIMG"].ToString())
                {
                    // Create an empty page
                    objPage = objDocument.AddPage();

                    // Get an XGraphics object for drawing
                    objGfx = XGraphics.FromPdfPage(objPage);

                    objImage = XImage.FromFile(strFileName);

                    //double x = (250 - objImage.PixelWidth * 72 / objImage.HorizontalResolution) / 2;
                    int x = Convert.ToInt32 (System.Configuration.ConfigurationSettings.AppSettings["BEGINX"].ToString());
                    int y = Convert.ToInt32 (System.Configuration.ConfigurationSettings.AppSettings["BEGINY"].ToString());
                    int largura = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["WIDTH"].ToString());
                    int altura = Convert.ToInt32 (System.Configuration.ConfigurationSettings.AppSettings["HEIGHT"].ToString());

                    objGfx.DrawImage(objImage,x, y, largura,altura );

                    if (File.Exists(strFileTmpPdf)) File.Delete(strFileTmpPdf);

                    objDocument.Save(strFileTmpPdf);

                }
            }

            if (ShowFile) Process.Start(strFileTmpPdf);
            return true;
            }
            catch (Exception ex)
            {
            throw ex;
            }
            finally {

            objPage = null;

            objImage.Dispose();
            objImage = null;

            objDocument.Dispose();
            objDocument = null;

            objFileInfo = null;

            }
        }
コード例 #3
0
ファイル: Actions.cs プロジェクト: GNOME/pdfmod
        void OnExtractPages (object o, EventArgs args)
        {
            var to_doc = new PdfDocument ();
            var from_doc = PdfSharp.Pdf.IO.PdfReader.Open (new Uri (app.Document.CurrentStateUri).LocalPath, PdfDocumentOpenMode.Import, null);
            var pages = app.IconView.SelectedPages.ToList ();

            foreach (var index in pages.Select (p => p.Index)) {
                to_doc.AddPage (from_doc.Pages[index]);
            }

            var path = Client.GetTmpFilename ();
            to_doc.Save (path);
            to_doc.Dispose ();

            app.LoadPath (path, Path.Combine (
                Path.GetDirectoryName (app.Document.SuggestedSavePath),
                Hyena.StringUtil.EscapeFilename (String.Format ("{0} [{1}].pdf",
                    Path.GetFileNameWithoutExtension (app.Document.SuggestedSavePath),
                    GLib.Markup.EscapeText (Document.GetPageSummary (pages, 10))))
            ));
        }
コード例 #4
0
        public void write(int paperFormatIndex, String fileName)
        {
            PageSize pageSize;
            paperFormatIndex++;

            switch (paperFormatIndex)
            {
                case (int)PageSize.A0:
                    pageSize = PageSize.A0;
                    break;
                case (int)PageSize.A1:
                    pageSize = PageSize.A1;
                    break;
                case (int)PageSize.A2:
                    pageSize = PageSize.A2;
                    break;
                case (int)PageSize.A3:
                    pageSize = PageSize.A3;
                    break;
                default:
                    pageSize = PageSize.A4;
                    break;
            }

            //Grayscale Map
            //Image grayscaledMap = MapWithQRCodeWriter.MakeGrayscale(map);

            // scale map
            Image scaledMap = this.scaleImageToFitFormat(pageSize, map);
            this.drawFrame(ref scaledMap);

            // get location
            int locationX = Convert.ToInt32(((this.codeSize / 2.0f) / scaledMap.Width) * map.Width);
            int locationY = Convert.ToInt32(((this.codeSize / 2.0f) / scaledMap.Height) * map.Height);
            PointLatLng location = MapOverlayForm.Instance.FromLocalToLatLng(locationX, locationY);

            // get scale
            int p1X = Convert.ToInt32((92.0f / scaledMap.Width) * map.Width);
            int p1Y = Convert.ToInt32((92.0f / scaledMap.Width) * map.Width);
            PointLatLng p1Location = MapOverlayForm.Instance.FromLocalToLatLng(p1X, p1Y);
            int p2X = Convert.ToInt32((356.0f / scaledMap.Width) * map.Width);
            int p2Y = Convert.ToInt32((92.0f / scaledMap.Width) * map.Width);
            PointLatLng p2Location = MapOverlayForm.Instance.FromLocalToLatLng(p2X, p2Y);
            double scale = MapOverlayForm.Instance.gMapControl.MapProvider.Projection.GetDistance(p1Location, p2Location);

            // create qr code
            com.google.zxing.qrcode.QRCodeWriter qrCode = new com.google.zxing.qrcode.QRCodeWriter();
            String locationString = location.Lat.ToString() + ";" + location.Lng.ToString() + ";" + scale.ToString().Substring(0, 6);
            ByteMatrix byteIMG = qrCode.encode(locationString, com.google.zxing.BarcodeFormat.QR_CODE, this.codeSize, this.codeSize);

            // draw qr code on map image
            this.drawQRCodeOnImage(byteIMG, ref scaledMap);

            PdfDocument document = new PdfDocument();
            PdfPage page = document.AddPage();
            page.Size = pageSize;
            page.Orientation = PageOrientation.Landscape;
            XGraphics gfx = XGraphics.FromPdfPage(page);

            XImage image = XImage.FromGdiPlusImage(scaledMap);

            XUnit imgHeight = XUnit.FromInch(image.PixelHeight / image.VerticalResolution);
            XUnit imgWidth = XUnit.FromInch(image.PixelWidth / image.HorizontalResolution);

            double width = image.PixelWidth * 72 / image.HorizontalResolution;
            double height = image.PixelHeight * 72 / image.VerticalResolution;

            gfx.DrawImage(image, (page.Width.Point - imgWidth.Point) / 2.0, (page.Height.Point - imgHeight.Point) / 2.0, width, height);
            try
            {
                document.Save(fileName);
            }
            catch (IOException)
            {
                System.Windows.Forms.MessageBox.Show("Could not save the file. Maybe the file is used by another program. ", "IO Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
            }
            document.Dispose();
        }