Beispiel #1
0
        public int GetHeight()
        {
            if (image != null)
            {
                return(image.Height);
            }
            if (fileType)
            {
                if (!extPH)
                {
                    ph.Open(fileName, Password);
                }
                ph.Page = page + 1;
                try
                {
                    return((int)ph.Height);
                }
                finally
                {
                    if (!extPH)
                    {
                        ph.Close();
                    }
                }
            }

            Tiff.PageInfo pi = Environment.LibTiff.GetImageFromTiff(fileName, page);
            if (pi.Image != null)
            {
                image      = pi.Image;
                annotation = pi.Annotation;
            }
            return(image != null ? image.Height : 0);
        }
Beispiel #2
0
        public static bool AddPagesToPDF(string filename, List <Tiff.PageInfo> images, int page, int from, int to)
        {
            PdfReader pdfReader = null;

            try
            {
                string password = String.Empty;
                try
                {
                    pdfReader = new PdfReader(filename);
                    if (!pdfReader.IsOpenedWithFullPermissions)
                    {
                        throw new BadPasswordException("");
                    }
                }
                catch (BadPasswordException)
                {
                    while (!pdfReader.IsOpenedWithFullPermissions)
                    {
                        pdfReader.Close();

                        if (
                            InputBox.Show(Environment.StringResources.GetString("DocControl_PDF_Encrypted"),
                                          Environment.StringResources.GetString("DocControl_PDF_EnterPass"),
                                          ref password) == DialogResult.Cancel)
                        {
                            return(false);
                        }
                        try
                        {
                            pdfReader = new PdfReader(filename, Encoding.ASCII.GetBytes(password));
                        }
                        catch (BadPasswordException)
                        {
                        }
                        catch (Exception ex) { Data.Env.WriteToLog(ex); }
                    }
                }
                catch (Exception ex) { Data.Env.WriteToLog(ex); }

                if (pdfReader.NumberOfPages == 0)
                {
                    throw new Exception("В документе отсутствуют страницы. Операция сохранения невозможна!");
                }

                string tmpFileName = Path.GetTempFileName();
                int    imagesCount = images.Count;
                using (var mem_stream = new MemoryStream())
                {
                    using (var document = new iTextSharp.text.Document())
                        using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, mem_stream))
                        {
                            document.OpenDocument();
                            for (int j = 0; j < imagesCount; j++)
                            {
                                Tiff.PageInfo info       = images[j];
                                bool          needRotate = info.Image.Width > info.Image.Height;
                                if (needRotate)
                                {
                                    info.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                                }

                                Image gif = Image.GetInstance(info.Image, ImageFormat.Png);
                                gif.SetAbsolutePosition(0, 0);
                                gif.ScaleAbsolute((float)(info.Image.Width * (72.0 / info.Image.HorizontalResolution)),
                                                  (float)(info.Image.Height * (72.0 / info.Image.VerticalResolution)));
                                gif.SetAbsolutePosition(1, 1);

                                document.SetPageSize(new Rectangle(gif.ScaledWidth, gif.ScaledHeight));
                                document.NewPage();

                                pdfWriter.DirectContent.AddImage(gif);

                                if (needRotate)
                                {
                                    pdfWriter.AddPageDictEntry(PdfName.ROTATE, new PdfNumber(270));
                                }
                            }
                            images.Clear();
                            pdfWriter.CloseStream = false;
                            document.CloseDocument();
                        }
                    mem_stream.Position = 0;
                    var addPageReader = new PdfReader(mem_stream);

                    using (var file_stream = new FileStream(tmpFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                        using (var document = new iTextSharp.text.Document(pdfReader.GetPageSize(1)))
                            using (var pdfCopy = new PdfCopy(document, file_stream))
                            {
                                document.Open();
                                if (-1 == page)
                                {
                                    for (int j = 1; j <= imagesCount; j++)
                                    {
                                        PdfImportedPage pp = pdfCopy.GetImportedPage(addPageReader, j);
                                        pdfCopy.AddPage(pp);
                                    }
                                }
                                if (-2 == page)
                                {
                                    page = pdfReader.NumberOfPages - 1;
                                }
                                for (int i = 1; i <= pdfReader.NumberOfPages; i++)
                                {
                                    PdfImportedPage pp = pdfCopy.GetImportedPage(pdfReader, i);
                                    pdfCopy.AddPage(pp);
                                    if (i == page + 1)
                                    {
                                        for (int j = 1; j <= imagesCount; j++)
                                        {
                                            pp = pdfCopy.GetImportedPage(addPageReader, j);
                                            pdfCopy.AddPage(pp);
                                        }
                                    }
                                }
                                document.Close();
                                if (pdfReader != null)
                                {
                                    pdfReader.Close();
                                    pdfReader = null;
                                }
                                addPageReader.Close();
                                File.Copy(tmpFileName, filename, true);
                                File.Delete(tmpFileName);
                                return(true);
                            }
                }
            }
            catch (Exception ex) { Data.Env.WriteToLog(ex); }
            finally
            {
                if (pdfReader != null)
                {
                    pdfReader.Close();
                }
            }
            return(false);
        }
Beispiel #3
0
        protected virtual bool IsLand(Tiff.PageInfo info)
        {
            Image img = info.Image;

            return(img.Width * img.VerticalResolution > img.Height * img.HorizontalResolution);
        }
Beispiel #4
0
        protected virtual void Print_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (printingPage == -1)
            {
                printingPage = startPage;
            }
            else
            {
                printingPage++;
            }
            Tiff.PageInfo pi = images[printingPage - startPage] as Tiff.PageInfo;

            if (pi != null && pi.Image != null)
            {
                OnPagePrinded(new PageEventArgs(printingPage - 1));
                System.Drawing.Image img = pi.Image;
                if (annotations)
                {
                    RenderAnnotations renderAnnotations = new RenderAnnotations(0);
                    byte[]            notes             = pi.Annotation;
                    TiffAnnotation    tiffAnnotation    = null;
                    if (notes != null)
                    {
                        tiffAnnotation = new TiffAnnotation(null);
                        tiffAnnotation.Parse(notes);
                        Bitmap bitmapImg = new Bitmap(img);

                        if (img.HorizontalResolution == 0 || img.VerticalResolution == 0)
                        {
                            bitmapImg.SetResolution(200, 200);
                        }
                        else
                        {
                            bitmapImg.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                        }

                        using (Graphics g = Graphics.FromImage(bitmapImg))
                        {
                            g.InterpolationMode = InterpolationMode.High;
                            Rectangle[] rects = null;
                            renderAnnotations.DrawAnnotation(g, tiffAnnotation, ImageControl.AllMarkGroupsVisibleList, bitmapImg, InterpolationMode.High, ref rects, null);
                        }
                        img = bitmapImg;
                    }
                }
                //e.Graphics.PageUnit = GraphicsUnit.Pixel;
                if (scaleMode == 1)
                {
                    float hr = (img.HorizontalResolution > 0) ? img.HorizontalResolution : 200f; float vr = (img.VerticalResolution > 0) ? img.VerticalResolution : 200f;
                    e.Graphics.DrawImage(img, 0f, 0f, img.Width * 100f / hr, img.Height * 100f / vr);
                }
                else
                {
                    IntPtr hdc        = e.Graphics.GetHdc();
                    int    realwidth  = GetDeviceCaps(hdc, HORZRES);
                    int    realheight = GetDeviceCaps(hdc, VERTRES);
                    e.Graphics.ReleaseHdc(hdc);

                    int rx = e.PageSettings.PrinterResolution.X, ry = e.PageSettings.PrinterResolution.Y, rdef = 300;
                    if (rx < 1)
                    {
                        rx = rdef;
                    }
                    if (ry < 1)
                    {
                        ry = rx;
                    }

                    if (realwidth > 0)
                    {
                        realwidth = realwidth * 100 / rx;
                    }
                    else
                    {
                        realwidth = e.PageBounds.Width * 100 / rx;
                    }
                    if (realheight > 0)
                    {
                        realheight = realheight * 100 / ry;
                    }
                    else
                    {
                        realheight = e.PageBounds.Height * 100 / ry;
                    }
                    bool isScale = (float)img.Width > (float)(realwidth) * img.VerticalResolution / 100f || img.Height > realheight * img.HorizontalResolution / 100f;
                    bool isWidth = img.Width / img.HorizontalResolution / realwidth > img.Height / img.VerticalResolution / realheight;
                    if (isScale)
                    {
                        int swidth;
                        int sheight;
                        if (isWidth)
                        {
                            swidth  = realwidth;
                            sheight = (int)((float)swidth / (float)img.Width * img.HorizontalResolution / img.VerticalResolution * (float)img.Height);
                        }
                        else
                        {
                            sheight = realheight;
                            swidth  = (int)((float)sheight / (float)img.Height * img.VerticalResolution / img.HorizontalResolution * (float)img.Width);
                        }
                        e.Graphics.DrawImage(img, 0, 0, swidth, sheight);
                    }
                    else
                    {
                        e.Graphics.DrawImage(img, 0f, 0f, (float)img.Width * 100f / img.HorizontalResolution, (float)img.Height * 100f / img.VerticalResolution);
                    }
                }
            }

            if (printingPage < endPage)
            {
                Image img = (images[printingPage - startPage] as Tiff.PageInfo).Image;
                SetOrientation(img.Width * img.VerticalResolution > img.Height * img.HorizontalResolution, e.PageSettings);
                e.HasMorePages = true;
            }
            else
            {
                e.HasMorePages = false;
                OnEndPrint();
            }
        }
Beispiel #5
0
        protected override bool IsLand(Tiff.PageInfo info)
        {
            var pInfo = info as PrintImageInfo;

            return(pInfo != null && pInfo.Image == null?pInfo.GetWidth() > pInfo.GetHeight() : base.IsLand(info));
        }