示例#1
0
        static private string WriteCompatiblePdf(string sFilename)
        {
            // string s = System.IO.Path.GetTempPath();
            string sNewPdf = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";

            sNewPdfs.Add(sNewPdf);

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sFilename);

            // we retrieve the total number of pages
            int n = reader.NumberOfPages;

            // step 1: creation of a document-object
            iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(sNewPdf, FileMode.Create));
            //write pdf that pdfsharp can understand
            writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
            // step 3: we open the document
            document.Open();
            iTextSharp.text.pdf.PdfContentByte  cb = writer.DirectContent;
            iTextSharp.text.pdf.PdfImportedPage page;

            int rotation;

            int i = 0;

            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                page     = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }
            // step 5: we close the document
            document.Close();



            return(sNewPdf);
        }
        /// <summary>
        /// from http://forum.pdfsharp.net/viewtopic.php?p=2069
        /// uses itextsharp to convert any pdf to 1.4 compatible pdf
        /// </summary>
        static private string WritePdf1pt4Version(string inputPath)
        {
            var tempFileName = Path.GetTempFileName();

            File.Delete(tempFileName);
            string outputPath = tempFileName + ".pdf";

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(inputPath);

            // we retrieve the total number of pages
            int n = reader.NumberOfPages;

            // step 1: creation of a document-object
            iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create));
            //write pdf that pdfsharp can understand
            writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
            // step 3: we open the document
            document.Open();
            iTextSharp.text.pdf.PdfContentByte  cb = writer.DirectContent;
            iTextSharp.text.pdf.PdfImportedPage page;

            int rotation;

            int i = 0;

            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                page     = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }
            // step 5: we close the document
            document.Close();
            return(outputPath);
        }
示例#3
0
        public void MergePdf(string[] pdfFiles, string outputPath)
        {
            int    pdfCount = 0;
            int    f        = 0;
            string filename = String.Empty;

            iTextSharp.text.pdf.PdfReader reader = null;
            int pageCount = 0;

            iTextSharp.text.Document            pdfDoc = null;
            iTextSharp.text.pdf.PdfWriter       writer = null;
            iTextSharp.text.pdf.PdfContentByte  cb     = null;
            iTextSharp.text.pdf.PdfImportedPage page   = null;
            int rotation = 0;

            iTextSharp.text.Font bookmarkFont = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 4, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.RED);

            try
            {
                pdfCount = pdfFiles.Length;
                if (pdfCount > 1)
                {
                    filename  = pdfFiles[f];
                    reader    = new iTextSharp.text.pdf.PdfReader(filename);
                    pageCount = reader.NumberOfPages;
                    pdfDoc    = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18);
                    writer    = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, new System.IO.FileStream(outputPath, System.IO.FileMode.Create));
                    pdfDoc.AddAuthor("OPGK w Lublinie sp. z o. o. Sławomir Aleksak");
                    pdfDoc.AddCreator("Konwerter");
                    pdfDoc.Open();
                    cb = writer.DirectContent;
                    while (f < pdfCount)
                    {
                        var i = 0;
                        while (i < pageCount)
                        {
                            i += 1;
                            pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i));
                            pdfDoc.NewPage();
                            if (i == 1)
                            {
                                iTextSharp.text.Paragraph para   = new iTextSharp.text.Paragraph(System.IO.Path.GetFileName(filename).ToUpper(), bookmarkFont);
                                iTextSharp.text.Chapter   chpter = new iTextSharp.text.Chapter(para, f + 1);
                                pdfDoc.Add(chpter);
                            }
                            page     = writer.GetImportedPage(reader, i);
                            rotation = reader.GetPageRotation(i);
                            if (rotation == 90)
                            {
                                cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                            }
                            if (rotation == 270)
                            {
                                cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30);
                            }
                            else
                            {
                                cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
                            }
                        }
                        f += 1;
                        if (f < pdfCount)
                        {
                            filename  = pdfFiles[f];
                            reader    = new iTextSharp.text.pdf.PdfReader(filename);
                            pageCount = reader.NumberOfPages;
                        }
                    }
                    pdfDoc.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#4
0
        private static void MergeFiles(string destinationFile, string[] sourceFiles)
        {
            if (System.IO.File.Exists(destinationFile))
            {
                System.IO.File.Delete(destinationFile);
            }

            string[] sSrcFile;
            sSrcFile = new string[sourceFiles.Count()];

            string[] arr = new string[sourceFiles.Count()];
            for (int i = 0; i <= sourceFiles.Length - 1; i++)
            {
                if (sourceFiles[i] != null)
                {
                    if (sourceFiles[i].Trim() != "")
                    {
                        arr[i] = sourceFiles[i].ToString();
                    }
                }
            }

            if (arr != null)
            {
                sSrcFile = new string[sourceFiles.Count()];

                for (int ic = 0; ic <= arr.Length - 1; ic++)
                {
                    sSrcFile[ic] = arr[ic].ToString();
                }
            }
            try
            {
                int f = 0;

                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sSrcFile[f]);
                int n = reader.NumberOfPages;
                using (iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4))
                {
                    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));

                    document.Open();
                    iTextSharp.text.pdf.PdfContentByte  cb = writer.DirectContent;
                    iTextSharp.text.pdf.PdfImportedPage page;

                    int rotation;
                    while (f < sSrcFile.Length)
                    {
                        int i = 0;
                        while (i < n)
                        {
                            i++;

                            document.SetPageSize(iTextSharp.text.PageSize.A4);
                            document.NewPage();
                            page = writer.GetImportedPage(reader, i);

                            rotation = reader.GetPageRotation(i);
                            if (rotation == 90 || rotation == 270)
                            {
                                cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                            }
                            else
                            {
                                cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                            }
                            //Response.Write("\n Processed page " + i);
                        }

                        f++;
                        if (f < sSrcFile.Length)
                        {
                            reader = new iTextSharp.text.pdf.PdfReader(sSrcFile[f]);
                            n      = reader.NumberOfPages;
                            // Response.Write("There are " + n + " pages in the original file.");
                        }
                    }
                    // Response.Write("Success");
                    document.Close();
                }
            }
            catch (Exception e)
            {
                // Response.Write(e.Message);
            }
        }