private void createCombinedPdf_iTextSharp(List <string> InFiles, string OutFile) { try { using (var stream = new System.IO.FileStream(OutFile, System.IO.FileMode.Create)) using (var doc = new iTextSharp.text.Document()) using (var pdf = new iTextSharp.text.pdf.PdfCopy(doc, stream)) { doc.Open(); iTextSharp.text.pdf.PdfReader reader = null; iTextSharp.text.pdf.PdfImportedPage page = null; //fixed typo InFiles.ForEach(file => { reader = new iTextSharp.text.pdf.PdfReader(file); for (int i = 0; i < reader.NumberOfPages; i++) { page = pdf.GetImportedPage(reader, i + 1); pdf.AddPage(page); } pdf.FreeReader(reader); reader.Close(); System.IO.File.Delete(file); }); } } catch (Exception excpt) { System.Diagnostics.Debug.WriteLine(excpt); } }
public void ExtractPage(string sourcePdfPath, string outputPdfPath, int pageNumber, int pageend) { iTextSharp.text.pdf.PdfReader reader = null; iTextSharp.text.Document document = null; iTextSharp.text.pdf.PdfCopy pdfCopyProvider = null; iTextSharp.text.pdf.PdfImportedPage importedPage = null; try { // Intialize a new PdfReader instance with the contents of the source Pdf file: reader = new iTextSharp.text.pdf.PdfReader(sourcePdfPath); // Capture the correct size and orientation for the page: document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(pageNumber)); // Initialize an instance of the PdfCopyClass with the source // document and an output file stream: pdfCopyProvider = new iTextSharp.text.pdf.PdfCopy(document, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create)); document.Open(); // Extract the desired page number: if (pageNumber == pageend) { importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber + 1); pdfCopyProvider.AddPage(importedPage); } else { for (int i = pageNumber; i <= pageend; i++) { importedPage = pdfCopyProvider.GetImportedPage(reader, i + 1); pdfCopyProvider.AddPage(importedPage); } } document.Close(); reader.Close(); } catch (Exception ex) { _arCommonService.CreateErrorLog(ex.Message, ex.StackTrace); } }
private void SaveFile(int desde) { if (CDPSession.Current.File == null) { return; } var pageCount = GetPageCount(); var currentPage = 1; CDPSession.Current.File.InputStream.Position = 0; using (var ms = new MemoryStream()) { CDPSession.Current.File.InputStream.CopyTo(ms); var reader = new iTextSharp.text.pdf.PdfReader(ms.ToArray()); for (var i = 1; i <= pageCount; i++) { var newFile = string.Format("{0}{1}.pdf", _folder, desde); var doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(currentPage)); var pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(newFile, System.IO.FileMode.Create)); doc.Open(); for (var j = 1; j <= 4; j++) { iTextSharp.text.pdf.PdfImportedPage page = pdfCpy.GetImportedPage(reader, currentPage); //pdfCpy.SetFullCompression(); pdfCpy.AddPage(page); currentPage += 1; } desde++; doc.Close(); pdfCpy.Close(); } reader.Close(); } }
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; } }
public void splitPdfByPages(String sourcePdf, int numOfPages, string baseNameOutPdf) { int pageCount = 0; string path = Path.GetFullPath(sourcePdf); string separtator = "_"; string extension = ".pdf"; try { raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf); reader = new iTextSharp.text.pdf.PdfReader(raf, null); pageCount = reader.NumberOfPages; if (pageCount < numOfPages) { MessageBox.Show("Nie ma co dzielić"); } else { string counter; string ext = System.IO.Path.GetExtension(baseNameOutPdf); string outfile = string.Empty; double m = pageCount / numOfPages; int n = (int)Math.Ceiling(m); int currentPage = 1; string thename = Path.GetFileNameWithoutExtension(sourcePdf); string name = Path.GetFileName(sourcePdf); for (int i = 1; i <= pageCount; i++) { if (i < 10) { counter = "00"; } else { counter = "0"; } outfile = path.Replace(name, "") + thename + separtator + counter + i + extension; doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(currentPage)); pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(outfile, System.IO.FileMode.Create)); doc.Open(); if (i < n) { for (int j = 1; j <= numOfPages; j++) { page = pdfCpy.GetImportedPage(reader, currentPage); pdfCpy.AddPage(page); currentPage += 1; } } else { for (int j = currentPage; j <= pageCount; j++) { page = pdfCpy.GetImportedPage(reader, j); pdfCpy.AddPage(page); } } doc.Close(); Console.Beep(); } } reader.Close(); } catch (Exception ex) { throw ex; } }
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 } } }
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); }