Beispiel #1
0
        public override void SaveDocument(ViewModels.DocumentViewModel document)
        {
            string srcDocPath;
            string targetFilePath = SafeFilePath(document.DocName);
            Stream stream;

            iTextSharp.text.Image image;
            PdfDictionary         pageDict;
            PdfImportedPage       importedPage;
            PdfContentByte        contentByte;
            //iTextSharp.text.Paragraph para;
            PdfCopy targetPdf;

            iTextSharp.text.Document doc;
            //iTextSharp.text.pdf.BaseFont baseFont;
            //iTextSharp.text.Font font;
            PdfReader srcReader;

            //ColumnText ct;
            PdfCopy.PageStamp pageStamp;

            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(targetFilePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(targetFilePath));
                }

                stream = new FileStream(targetFilePath, FileMode.Create);

                doc       = new iTextSharp.text.Document();
                targetPdf = new PdfCopy(doc, stream);
                doc.Open();

                //baseFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.TIMES_ROMAN, iTextSharp.text.pdf.BaseFont.CP1252, false);
                //font = new iTextSharp.text.Font(baseFont, 12f, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);

                foreach (ViewModels.PageViewModel vm in document.Pages)
                {
                    srcDocPath = FileIO.ToTempFileName(vm.DocName);

                    // Copy pageDict from source...
                    if (Path.GetExtension(srcDocPath).ToUpperInvariant() == ".PDF")
                    {
                        srcReader    = new iTextSharp.text.pdf.PdfReader(srcDocPath);
                        pageDict     = srcReader.GetPageN(vm.Number);
                        importedPage = targetPdf.GetImportedPage(srcReader, vm.Number);
                        pageStamp    = targetPdf.CreatePageStamp(importedPage);

                        //add any strings
                        foreach (Common.UIString str in vm.Strings)
                        {
                            ColumnText.ShowTextAligned(pageStamp.GetOverContent(),
                                                       iTextSharp.text.Element.ALIGN_LEFT,
                                                       new iTextSharp.text.Phrase(str.String),
                                                       (float)str.X,
                                                       (float)(importedPage.Height - str.Y - (str.Height * 0.75)),
                                                       0);
                        }
                        // apply any added rotation
                        pageDict.Put(PdfName.ROTATE, new PdfNumber((vm.FlatRotation) % 360f));
                        pageStamp.AlterContents();
                        targetPdf.AddPage(importedPage);

                        targetPdf.FreeReader(srcReader);
                        srcReader.Close();
                    }

                    if (vm.ImageStream != null && targetPdf.NewPage())
                    {
                        contentByte = new PdfContentByte(targetPdf);

                        image = iTextSharp.text.Image.GetInstance(vm.ImageStream);

                        image.ScalePercent(72f / image.DpiX * 100);
                        image.SetAbsolutePosition(0, 0);

                        contentByte.AddImage(image);
                        contentByte.ToPdf(targetPdf);
                    }
                }
                targetPdf.Close();
                doc.Close();
                stream.Close();
            }
            catch (System.IO.IOException e)
            {
                Toolbox.MessageBox(e.Message);
            }
            catch (Exception e)
            {
                Toolbox.MessageBoxException(e);
            }
            finally
            {
                //if (targetPdf != null)
                //    targetPdf.Close();

                //doc.Close();
                //memStream.Close();
            }
        }