Exemple #1
0
        public static void Convert(List <Tiff.PageInfo> il, string dstPath)
        {
            using (var file_stream = new FileStream(dstPath, FileMode.Create, FileAccess.Write, FileShare.None))
                using (var document = new iTextSharp.text.Document())
                    using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, file_stream))
                    {
                        document.SetMargins(0, 0, 0, 0);

                        document.Open();

                        foreach (Tiff.PageInfo info in il)
                        {
                            try
                            {
                                bool needRotate = info.Image.Width > info.Image.Height;
                                if (needRotate)
                                {
                                    info.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                                }

                                Image gif = Image.GetInstance(info.Image,
                                                              ImageFormat
                                                              .Png);
                                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));
                                }
                            }
                            catch (Exception ex)
                            {
                                Data.Env.WriteToLog(ex);
                                return;
                            }
                        }
                        document.Close();
                    }
        }
Exemple #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);
        }
Exemple #3
0
 public override void OnEndPage(PdfWriter writer, Document document)
 {
     writer.AddPageDictEntry(PdfName.ROTATE, PdfPage.SEASCAPE);
 }
Exemple #4
0
 public override void OnStartPage(PdfWriter writer, Document document)
 {
     writer.AddPageDictEntry(PdfName.ROTATE, orientation);
 }