Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public static bool PerfectBind_LayoutCroppedBrief(string src)
        {
            string dest = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(src), "pb on B5.pdf");

            try
            {
                using (var stream = new System.IO.FileStream(dest, System.IO.FileMode.Create))
                {
                    iTextSharp.text.Document      docB5  = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(515.76f, 728.4f)); // B5 sized page
                    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(docB5, stream);
                    docB5.Open();

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

                    for (int i = 1; i <= reader.NumberOfPages; i++)
                    {
                        if (i != 1)
                        {
                            docB5.NewPage();
                        }
                        docB5.Add(new iTextSharp.text.Chunk());

                        iTextSharp.text.pdf.PdfTemplate t = writer.GetImportedPage(reader, i);

                        iTextSharp.text.pdf.PdfContentByte contentbyte = writer.DirectContent;

                        // MEASUREMENTS GOOD: TESTED 11/16/2015
                        if (i % 2 == 1)
                        {
                            contentbyte.AddTemplate(t, /*42.5f*/ 35.5f, -141.75f); // position for front side of B5 sheet
                        }
                        else if (i % 2 == 0)
                        {
                            contentbyte.AddTemplate(t, /*110f*/ 110f, -141.75f); // position for back side of B5 sheet
                        }
                        else
                        {
                            throw new Exception();
                        }
                    }
                    docB5.Close();
                    reader.Close();
                }
                System.IO.File.Delete(src);
                System.IO.File.Move(dest, src);
                return(true);
            }
            catch (Exception excpt) { System.Diagnostics.Debug.WriteLine(excpt); return(false); }
        }
Exemplo n.º 3
0
        /// <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);
        }
        private System.IO.MemoryStream imposeTypesetPerfectBindBrief(System.IO.MemoryStream orig_stream)
        {
            System.IO.MemoryStream dest_stream = null;
            try
            {
                dest_stream = new System.IO.MemoryStream();
                var docB5  = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(515.76f, 728.4f));
                var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(docB5, dest_stream);
                docB5.Open();
                var reader = new iTextSharp.text.pdf.PdfReader(orig_stream.ToArray());

                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    if (i != 1)
                    {
                        docB5.NewPage();
                    }
                    docB5.Add(new iTextSharp.text.Chunk());

                    iTextSharp.text.pdf.PdfTemplate t = writer.GetImportedPage(reader, i);

                    iTextSharp.text.pdf.PdfContentByte contentbyte = writer.DirectContent;

                    // MEASUREMENTS GOOD: TESTED 11/16/2015
                    if (i % 2 == 1)
                    {
                        contentbyte.AddTemplate(t, /*42.5f*/ 35.5f, -141.75f); // position for front side of B5 sheet
                    }
                    else if (i % 2 == 0)
                    {
                        contentbyte.AddTemplate(t, /*110f*/ 110f, -141.75f); // position for back side of B5 sheet
                    }
                }
                docB5.Close();
                reader.Close();
                writer.Close();
            }
            catch (Exception excpt)
            {
                System.Diagnostics.Debug.WriteLine(excpt);
            }
            return(dest_stream);
        }
Exemplo n.º 5
0
        public static bool SaddleStitch_LayoutCroppedBrief(string src)
        {
            string dest = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(src), "ss on B4.pdf");

            try
            {
                using (var stream = new System.IO.FileStream(dest, System.IO.FileMode.Create))
                {
                    iTextSharp.text.Document      docB4  = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(1031.76f, 728.64f)); // B4 sized page
                    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(docB4, stream);
                    docB4.Open();

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

                    for (int i = 1; i <= reader.NumberOfPages; i += 2)
                    {
                        if (i != 1)
                        {
                            docB4.NewPage();
                        }
                        docB4.Add(new iTextSharp.text.Chunk());

                        iTextSharp.text.pdf.PdfTemplate tLeft  = writer.GetImportedPage(reader, i);
                        iTextSharp.text.pdf.PdfTemplate tRight = writer.GetImportedPage(reader, i + 1);

                        iTextSharp.text.pdf.PdfContentByte contentbyte = writer.DirectContent;

                        // MEASUREMENTS GOOD: TESTED 11/16/2015; RETESTED TO MATCH 951s, 11/17/2015
                        // RETESTED AGAIN FOR 951A, where most saddle stitches will print
                        contentbyte.AddTemplate(tLeft, /*113f*/ 108f /*110f*/, -126f);        // position for left side of B4 sheet
                        contentbyte.AddTemplate(tRight, /*554.5f*/ /*550.5f*/ 554.5f, -126f); // position for right side of B4 sheet
                    }
                    docB4.Close();
                    reader.Close();
                }
                System.IO.File.Delete(src);
                System.IO.File.Move(dest, src);
                return(true);
            }
            catch (Exception excpt) { System.Diagnostics.Debug.WriteLine(excpt); return(false); }
        }
        private System.IO.MemoryStream imposeTypesetSaddleStitchBrief(System.IO.MemoryStream orig_stream)
        {
            System.IO.MemoryStream dest_stream = null;
            try
            {
                dest_stream = new System.IO.MemoryStream();

                var docB4  = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(1031.76f, 728.64f)); // B4 sized page
                var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(docB4, dest_stream);
                docB4.Open();

                var reader = new iTextSharp.text.pdf.PdfReader(orig_stream.ToArray());

                for (int i = 1; i <= reader.NumberOfPages; i += 2)
                {
                    if (i != 1)
                    {
                        docB4.NewPage();
                    }
                    docB4.Add(new iTextSharp.text.Chunk());

                    iTextSharp.text.pdf.PdfTemplate tLeft  = writer.GetImportedPage(reader, i);
                    iTextSharp.text.pdf.PdfTemplate tRight = writer.GetImportedPage(reader, i + 1);

                    iTextSharp.text.pdf.PdfContentByte contentbyte = writer.DirectContent;

                    // MEASUREMENTS GOOD: TESTED 11/16/2015; RETESTED TO MATCH 951s, 11/17/2015
                    // RETESTED AGAIN FOR 951A, where most saddle stitches will print
                    contentbyte.AddTemplate(tLeft, /*113f*/ 108f /*110f*/, -126f);        // position for left side of B4 sheet
                    contentbyte.AddTemplate(tRight, /*554.5f*/ /*550.5f*/ 554.5f, -126f); // position for right side of B4 sheet
                }
                docB4.Close();
                reader.Close();
            }
            catch (Exception excpt)
            {
                System.Diagnostics.Debug.WriteLine(excpt);
            }
            return(dest_stream);
        }
Exemplo n.º 7
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;
            }
        }
Exemplo n.º 8
0
        private void MultiplePages(string aSourceFile, string aDestFile, int aPages, bool aLandscape)
        {
            iTextSharp.text.Document document = null;
            try {
                if (aPages < kMultiplePagesMin || aPages > kMultiplePagesMax)
                {
                    throw new Exception("Multiple Pages only supports " + kMultiplePagesMin + " to " + kMultiplePagesMin + " pages per sheet (Invalid value: " + aPages.ToString() + ")");
                }
                iProgressChanged(0, "Generating Multiple Pages Per Sheet: Initialising Document", Progress.State.eInProgress);
                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(aSourceFile);
                int n = reader.NumberOfPages;

                // step 1: creation of a document-object
                if (aLandscape && aPages != 2 && aPages != 8 && aPages != 32)                      // pages = 2,8,32: print landscape pages onto portrait document in landscape orientation
                {
                    document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate()); // landscape
                }
                else
                {
                    document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4); // portrait
                }

                // step 2: we create a writer that listens to the document
                iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(aDestFile, FileMode.Create));
                writer.ViewerPreferences = iTextSharp.text.pdf.PdfWriter.PageLayoutSinglePage;

                // step 3: we open the document
                document.Open();
                iTextSharp.text.pdf.PdfContentByte  cb   = writer.DirectContent;
                iTextSharp.text.pdf.PdfImportedPage page = null;

                float[] xPoints = new float[7] {
                    -1f, -1f, -1f, -1f, -1f, -1f, -1f
                };
                float[] yPoints = new float[9] {
                    -1f, -1f, -1f, -1f, -1f, -1f, -1f, -1f, -1f
                };
                int[] xFactors = new int[37] {
                    0, 1, 1, 0, 2, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 6
                };
                int[] yFactors = new int[37] {
                    0, 1, 2, 0, 2, 0, 0, 0, 4, 3, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 6
                };

                int   xFactor       = xFactors[aPages];
                int   yFactor       = yFactors[aPages];
                float xScaler       = 1f / xFactor;
                float yScaler       = 1f / yFactor;
                float docWidth      = document.PageSize.Width;
                float docHeight     = document.PageSize.Height;
                float specialScaler = (docWidth / docHeight) / xFactor;
                int   pyBase        = yFactor;
                if (!aLandscape)
                {
                    pyBase--;
                }

                for (uint i = 0; i <= xFactor; i++)
                {
                    xPoints[i] = (docWidth / xFactor) * i;
                }
                for (uint i = 0; i <= yFactor; i++)
                {
                    yPoints[i] = (docHeight / yFactor) * i;
                }
                int px        = 0;
                int py        = pyBase;
                int pxSpecial = xFactor - 1;
                int pySpecial = yFactor;
                int j         = 0;
                int p         = 0;

                // step 4: we add content
                int finalTotal = n / aPages;
                if (n % aPages != 0)
                {
                    finalTotal++;
                }

                int  forward           = 1;
                int  backwardBase      = (finalTotal * aPages) - (aPages / 2) + 1;
                int  backward          = backwardBase;
                int  getPage           = 0;
                bool flip              = !aLandscape;
                int  forward2          = 0;
                int  backward2         = 0;
                bool booklet           = iPrintOrderBooklet.Enabled && iPrintOrderBooklet.Checked;
                bool upsideDown        = ((aPages == 4 || aPages == 16 || aPages == 36) && !aLandscape && booklet);
                bool upsideDownSpecial = ((aPages == 2 || aPages == 8 || aPages == 32) && aLandscape && booklet);
                if (upsideDownSpecial)
                {
                    upsideDown = true;
                    flip       = true;
                }

                while (j < n)
                {
                    j++;
                    iProgressChanged((((j / aPages) + 1) * 100 / finalTotal), "Generating Multiple Pages Per Sheet: Page " + ((j / aPages) + 1) + " of " + finalTotal, Progress.State.eInProgress);

                    if (booklet)
                    {
                        bool test = false;
                        if ((aPages == 2 || aPages == 8 || aPages == 32) && !aLandscape)
                        {
                            test = (pySpecial <= (yFactor / 2));
                        }
                        else
                        {
                            if (upsideDownSpecial)
                            {
                                test = (py <= (yFactor / 2));
                            }
                            else if (upsideDown)
                            {
                                test = (py < (yFactor / 2));
                            }
                            else
                            {
                                test = (px < (xFactor / 2));
                            }
                        }
                        if (test)
                        {
                            if (!flip)
                            {
                                getPage = backward++;
                                backwardBase--;
                                if (upsideDown)
                                {
                                    getPage = forward2--;
                                }
                            }
                            else
                            {
                                getPage = forward++;
                            }
                        }
                        else
                        {
                            if (!flip)
                            {
                                getPage = forward++;
                                if (upsideDown)
                                {
                                    getPage = backward2--;
                                }
                            }
                            else
                            {
                                getPage = backward++;
                                backwardBase--;
                            }
                        }
                    }
                    else
                    {
                        getPage = j;
                    }
                    if (getPage <= n)
                    {
                        page = writer.GetImportedPage(reader, getPage);
                    }
                    else
                    {
                        j--;
                    }

                    if (p == 0)
                    {
                        // draw layout lines (once per destination document page)
                        cb.SetRGBColorStroke(0xC0, 0xC0, 0xC0);
                        foreach (float xPoint in xPoints)
                        {
                            if (xPoint >= 0)
                            {
                                cb.MoveTo(xPoint, 0);
                                cb.LineTo(xPoint, docHeight);
                                if (docWidth > docHeight && booklet)
                                {
                                    if (xPoint == (docWidth / 2))
                                    {
                                        cb.SetLineDash(2f, 4f, 0);
                                    }
                                }
                                cb.Stroke();
                                cb.SetLineDash(0);
                            }
                        }
                        foreach (float yPoint in yPoints)
                        {
                            if (yPoint >= 0)
                            {
                                cb.MoveTo(0, yPoint);
                                cb.LineTo(docWidth, yPoint);
                                if (docHeight > docWidth && booklet)
                                {
                                    if (yPoint == (docHeight / 2))
                                    {
                                        cb.SetLineDash(2f, 4f, 0);
                                    }
                                }
                                cb.Stroke();
                                cb.SetLineDash(0);
                            }
                        }
                    }

                    if (getPage <= n)
                    {
                        if (aPages == 2 || aPages == 8 || aPages == 32)
                        {
                            if (aLandscape)
                            {
                                if (upsideDown && !flip)
                                {
                                    cb.AddTemplate(page, 0, specialScaler, -specialScaler, 0, xPoints[px + 1], yPoints[py - 1]);
                                }
                                else
                                {
                                    cb.AddTemplate(page, 0, -specialScaler, specialScaler, 0, xPoints[px], yPoints[py]);
                                }
                            }
                            else
                            {
                                cb.AddTemplate(page, 0, -specialScaler, specialScaler, 0, xPoints[pxSpecial], yPoints[pySpecial]);
                            }
                        }
                        else
                        {
                            if (aLandscape)
                            {
                                cb.AddTemplate(page, 0, -xScaler, yScaler, 0, xPoints[px], yPoints[py]);
                            }
                            else
                            {
                                if (upsideDown && !flip)
                                {
                                    cb.AddTemplate(page, -xScaler, 0, 0, -yScaler, xPoints[px + 1], yPoints[py + 1]);
                                }
                                else
                                {
                                    cb.AddTemplate(page, xScaler, 0, 0, yScaler, xPoints[px], yPoints[py]);
                                }
                            }
                        }
                    }

                    cb.Stroke();
                    p++;
                    if ((p % xFactor) == 0)
                    {
                        px = 0;
                        py--;
                    }
                    else
                    {
                        px++;
                    }
                    if ((p % yFactor) == 0)
                    {
                        pxSpecial--;
                        pySpecial = yFactor;
                    }
                    else
                    {
                        pySpecial--;
                    }
                    if (p == aPages)
                    {
                        p         = 0;
                        px        = 0;
                        py        = pyBase;
                        pxSpecial = xFactor - 1;
                        pySpecial = yFactor;
                        backward  = backwardBase;
                        forward2  = (forward - 1) + (aPages / 2);
                        backward2 = (backward - 1) + (aPages / 2);
                        flip      = !flip;
                        document.NewPage();
                    }
                }
                iProgressChanged(100, "Generating Multiple Pages Per Sheet: Finalising Document", Progress.State.eInProgress);
            }
            catch (Exception e) {
                throw e;
            }
            finally {
                try {
                    // step 5: we close the document
                    if (document != null)
                    {
                        document.Close();
                    }
                }
                catch (System.IO.IOException) { // document has no pages
                }
            }
        }
Exemplo n.º 9
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);
            }
        }
Exemplo n.º 10
0
        public static string PrintPDFResize(string fileNamePath)
        {
            Debug.Log("CashmaticApp", "PrintPDFResize");
            string fileDirectory = Path.GetDirectoryName(fileNamePath);
            string fileName      = Path.GetFileNameWithoutExtension(fileNamePath);
            string newFile       = fileDirectory + "\\" + fileName + "_print.pdf";



            try
            {
                // open the reader
                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(fileNamePath);

                iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
                //Document document = new Document(size);
                iTextSharp.text.Document document = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(size.Width + 15, size.Height + Global.CardPaymentPrintPageSizeAddHeight));

                // open the writer
                FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
                iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, fs);
                document.Open();

                // the pdf content
                iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;

                if (Global.cardholderReceipt != "")
                {
                    // select the font properties
                    iTextSharp.text.pdf.BaseFont bf = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.COURIER_BOLD, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.EMBEDDED);
                    cb.SetColorFill(iTextSharp.text.BaseColor.BLACK);
                    cb.SetFontAndSize(bf, 8);

                    int StartAt = Global.CardPaymentPrintStartAt + Global.CardPaymentPrintPageSizeAddHeight;

                    foreach (string line in Global.cardholderReceipt.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
                    {
                        // write the text in the pdf content
                        cb.BeginText();
                        // put the alignment and coordinates here
                        cb.ShowTextAligned(0, line, Global.CardPaymentPrintLeft, StartAt, 0);
                        cb.EndText();
                        StartAt = StartAt - Global.CardPaymentPrintLineHeight;
                    }
                }

                //// create the new page and add it to the pdf
                iTextSharp.text.pdf.PdfImportedPage page = writer.GetImportedPage(reader, 1);
                cb.AddTemplate(page, 10, +Global.CardPaymentPrintPageSizeAddHeight);

                // close the streams and voilá the file should be changed :)
                document.Close();
                fs.Close();
                writer.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                Debug.Log("CashmaticApp", ex.ToString());
            }

            return(newFile);
        }