private void ExportFile() { Global.checkDirSaveFile(); var doc = new Document(PageSize.A4, 20, 20, 10, 10); PdfWriter docWriter = PdfWriter.GetInstance(doc, new FileStream(BHConstant.SAVE_IN_DIRECTORY + @"\Kho.pdf", FileMode.Create)); PdfWriterEvents writerEvent; Image watermarkImage = Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory + @"logo.png"); watermarkImage.SetAbsolutePosition(doc.PageSize.Width / 2 - 70, 600); writerEvent = new PdfWriterEvents(watermarkImage); docWriter.PageEvent = writerEvent; doc.Open(); doc.Add(FormatConfig.ParaRightBeforeHeader("In ngày : " + BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate().ToString(BHConstant.DATETIME_FORMAT))); doc.Add(FormatConfig.ParaHeader("BÁO CÁO KHO VÀ THÀNH PHẨM")); doc.Add(FormatConfig.ParaRightBelowHeader("(" + textForPrint + ")")); if (modeReport != 1) { doc.Add(ProductsTable()); } else { doc.Add(FormatConfig.ParaCommonInfo("Sản phẩm : ", textInfo)); doc.Add(ProductDetailTable()); } doc.Add(FormatConfig.ParaCommonInfo("Ghi chú : ", String.Concat(Enumerable.Repeat("...", 96)))); doc.Close(); }
private void ExportFile() { Global.checkDirSaveFile(); var doc = new Document(PageSize.A4, 20, 20, 10, 10); PdfWriter docWriter = PdfWriter.GetInstance(doc, new FileStream(BHConstant.SAVE_IN_DIRECTORY + @"\KHang.pdf", FileMode.Create)); PdfWriterEvents writerEvent; Image watermarkImage = Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory + @"logo.png"); watermarkImage.SetAbsolutePosition(doc.PageSize.Width / 2 - 70, 550); writerEvent = new PdfWriterEvents(watermarkImage); docWriter.PageEvent = writerEvent; doc.Open(); doc.Add(FormatConfig.ParaRightBeforeHeader("In ngày : " + BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate().ToString(BHConstant.DATETIME_FORMAT))); doc.Add(FormatConfig.ParaHeader("DANH SÁCH KHÁCH HÀNG")); PdfPTable table = FormatConfig.Table(7, new float[] { 0.5f, 2f, 1.3f, 1.3f, 2f, 1.6f, 1.3f }); table.AddCell(FormatConfig.TableCellHeader("STT")); table.AddCell(FormatConfig.TableCellHeader("Tên khách hàng")); table.AddCell(FormatConfig.TableCellHeader("Mã KH")); table.AddCell(FormatConfig.TableCellHeader("SĐT Cty")); table.AddCell(FormatConfig.TableCellHeader("Địa chỉ")); table.AddCell(FormatConfig.TableCellHeader("Người liên lạc")); table.AddCell(FormatConfig.TableCellHeader("SĐT")); for (int i = 0; i < customers.Count; i++) { table.AddCell(FormatConfig.TableCellBody((i + 1).ToString(), PdfPCell.ALIGN_CENTER)); table.AddCell(FormatConfig.TableCellBody(customers[i].CustomerName, PdfPCell.ALIGN_LEFT)); table.AddCell(FormatConfig.TableCellBody(customers[i].CustCode, PdfPCell.ALIGN_LEFT)); table.AddCell(FormatConfig.TableCellBody(customers[i].Phone, PdfPCell.ALIGN_LEFT)); table.AddCell(FormatConfig.TableCellBody(customers[i].Address, PdfPCell.ALIGN_LEFT)); table.AddCell(FormatConfig.TableCellBody(customers[i].ContactPerson, PdfPCell.ALIGN_LEFT)); table.AddCell(FormatConfig.TableCellBody(customers[i].ContactPersonPhone, PdfPCell.ALIGN_LEFT)); } doc.Add(table); doc.Add(FormatConfig.ParaCommonInfo("Ghi chú : ", String.Concat(Enumerable.Repeat("...", 96)))); doc.Close(); }
private void ExportFile() { Global.checkDirSaveFile(); var doc = new Document(PageSize.A4, 20, 20, 10, 10); PdfWriter docWriter = PdfWriter.GetInstance(doc, new FileStream(BHConstant.SAVE_IN_DIRECTORY + @"\CongNo.pdf", FileMode.Create)); PdfWriterEvents writerEvent; Image watermarkImage = Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory + @"logo.png"); watermarkImage.SetAbsolutePosition(doc.PageSize.Width / 2 - 70, 500); writerEvent = new PdfWriterEvents(watermarkImage); docWriter.PageEvent = writerEvent; doc.Open(); if (modeReport == 1) { doc.Add(FormatConfig.ParaRightBeforeHeaderRight(BHConstant.COMPANY_NAME)); doc.Add(FormatConfig.ParaRightBeforeHeaderRight(BHConstant.COMPANY_ADDRESS)); doc.Add(FormatConfig.ParaRightBeforeHeaderRight("ĐT: " + BHConstant.COMPANY_PHONE + " Fax: " + BHConstant.COMPANY_FAX)); } doc.Add(FormatConfig.ParaRightBeforeHeader("In ngày : " + BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate().ToString(BHConstant.DATETIME_FORMAT))); doc.Add(FormatConfig.ParaHeader("BÁO CÁO CÔNG NỢ")); doc.Add(FormatConfig.ParaRightBelowHeader("(" + textForPrint + ")")); if (modeReport != 1) { doc.Add(CustomersTable()); } else { doc.Add(FormatConfig.ParaCommonInfo("Kính gửi : ", customerPrint.CustomerName)); doc.Add(FormatConfig.ParaCommonInfo("Địa chỉ : ", customerPrint.Address)); doc.Add(FormatConfig.ParaCommonInfo("ĐT : ", customerPrint.Phone + " Fax: " + customerPrint.Fax)); doc.Add(CustomerDetailTable()); } doc.Add(FormatConfig.ParaCommonInfo("", "Quý khách hàng kiểm tra và đối chiếu, nếu có thắc mắc vui lòng liên hệ số điện thoại trên.")); doc.Add(FormatConfig.ParaCommonInfo("", "Chân thành cảm ơn sự hợp tác của quí khách.")); doc.Add(FormatConfig.ParaCommonInfo("Ghi chú : ", String.Concat(Enumerable.Repeat("...", 96)))); doc.Close(); }
private void save2Pdf(Dictionary <string, PdfPTable> pdfTables, string chartTempPath, string outputPath) { Document doc = new Document(PageSize.A4, 50, 50, 50, 50); try { FileStream fs = new FileStream(outputPath, FileMode.Create); PdfWriter pw = PdfWriter.GetInstance(doc, fs); //PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 2.00f); PdfWriterEvents writerEvent = new PdfWriterEvents(""); pw.PageEvent = writerEvent; doc.Open(); doc.AddTitle("Statistical report"); //文件標題 doc.AddAuthor("NoName"); //文件作者 //header image Uri uri = new Uri(@"\Resources\header.jpg", UriKind.Relative); System.Drawing.Image testImage = System.Drawing.Image.FromHbitmap(Properties.Resources.header.GetHbitmap()); iTextSharp.text.Image headerImage = iTextSharp.text.Image.GetInstance(testImage, System.Drawing.Imaging.ImageFormat.Jpeg); headerImage.ScaleToFit(500, 64); doc.Add(headerImage); //save image to pdf string[] files = Directory.GetFiles(chartTempPath); foreach (string file in files) { iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(file); image.ScaleToFit(500, 350); doc.Add(image);//加入影像 } //next page doc.NewPage(); //save tables to pdf foreach (KeyValuePair <string, PdfPTable> item in pdfTables) { doc.Add(item.Value); doc.Add(new Paragraph(" "));//NewLine } //footer doc.Add(createFooter()); } catch (Exception ex) { Log.Error($"save2Pdf fail. ErrorMsg={ex.Message}. Stacktrace={ex.StackTrace}"); doc.Close(); File.Delete(outputPath); } finally { doc.Close(); } }
private void ExtractPages(string inputFile, string outputFile, int start, int end, int pages) { string filename = Path.GetFileNameWithoutExtension(inputFile); int count = 1; Dictionary <int, string> pageText = new Dictionary <int, string>(); Dictionary <int, string> pageTextFull = new Dictionary <int, string>(); // get input document PdfReader inputPdf = new PdfReader(new iTextSharp.text.pdf.RandomAccessFileOrArray(inputFile), null); // retrieve the total number of pages int pageCount = inputPdf.NumberOfPages; if (start <= 0) { start = 1; } if (end > pageCount) { end = pageCount; } if (end <= 0) { end = pageCount; } if (pages < 1) { pages = pageCount; } if (end < start || end > pageCount) { end = pageCount; } if (pages > pageCount) { pages = pageCount - start + 1; } if (_searchForName) { pageTextFull = GetFileText(inputPdf, start, end); } if (!string.IsNullOrWhiteSpace(_prefixPattern)) { pageText = GetFilePrefixes(inputPdf, start, end); } PdfReader coverPdf = null; if (!string.IsNullOrWhiteSpace(_coverPath)) { //Cover Page coverPdf = new PdfReader(new iTextSharp.text.pdf.RandomAccessFileOrArray(_coverPath), null); } PdfReader footerPdf = null; if (!string.IsNullOrWhiteSpace(_footerPath)) { footerPdf = new PdfReader(new iTextSharp.text.pdf.RandomAccessFileOrArray(_footerPath), null); } for (int k = start; k <= end; k += pages) { // load the input document Document inputDoc = new Document(inputPdf.GetPageSizeWithRotation(k)); string outfile = filename.Replace('(', '_').Replace(')', '_'); if (_searchForName) { bool bgot = false; string found = ""; //for (int i = k; i < (k + pages); i++) //{ // string text = PdfTextExtractor.GetTextFromPage(inputPdf, i, new SimpleTextExtractionStrategy()); // text = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(text))); // if (string.IsNullOrWhiteSpace(text)) continue; // var x2 = Regex.Match(text, @"\{\#(.*?)\#\}"); // found = x2.Groups[1].Value; // if (!string.IsNullOrWhiteSpace(found)) // { // bgot = true; // break; // } //} for (int i = k; i < (k + pages); i++) { string text = pageTextFull[i]; if (string.IsNullOrWhiteSpace(text)) { continue; } var x2 = Regex.Match(text, @"\{\#(.*?)\#\}"); found = x2.Groups[1].Value; if (!string.IsNullOrWhiteSpace(found)) { bgot = true; break; } } if (bgot) { outfile = outfile + "_" + found; } } if (!string.IsNullOrWhiteSpace(_prefixPattern)) { bool bgot = false; for (int i = k; i < (k + pages); i++) { if (!pageText.ContainsKey(i)) { continue; } string prefix = pageText[i]; if (string.IsNullOrWhiteSpace(prefix)) { continue; } if (prefix.Length > 0) { outfile = "(" + prefix.Trim() + ")_" + outfile; bgot = true; break; } } if (!bgot) { outfile = "XXXX_" + outfile; } } if (string.IsNullOrWhiteSpace(outfile)) { outfile = filename.Replace('(', '_').Replace(')', '_') + "_" + count; } if (outfile.Length > 200) { outfile = outfile.Substring(0, 195); } if (_suffixPageNumber) { outfile = outfile + "-" + k.ToString(); } else { outfile = outfile + "-" + count.ToString(); } outfile += ".pdf"; if (!Directory.Exists(outputFile)) { Directory.CreateDirectory(outputFile); } string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidFileNameChars()); Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch))); outfile = r.Replace(outfile, ""); outfile = Path.Combine(outputFile, outfile); StatusMessage += ("\n\t\t" + outfile); // create the filestream using (FileStream fs = new FileStream(outfile, FileMode.Create)) { //MemoryStream ms = new MemoryStream(); // create the output writer PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs); //Watermark if (!string.IsNullOrWhiteSpace(_waterMark)) { PdfWriterEvents writerEvent = new PdfWriterEvents(_waterMark, pages); outputWriter.PageEvent = writerEvent; } inputDoc.Open(); PdfContentByte cb1 = outputWriter.DirectContent; if (!string.IsNullOrWhiteSpace(_coverPath)) { // copy pages from Cover document to output document first for (int i = 1; i <= coverPdf.NumberOfPages; i++) { inputDoc.SetPageSize(coverPdf.GetPageSizeWithRotation(i)); inputDoc.NewPage(); PdfImportedPage page = outputWriter.GetImportedPage(coverPdf, i); /*int rotation = coverPdf.GetPageRotation(i); * * if (rotation == 90 || rotation == 270) * { * cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, coverPdf.GetPageSizeWithRotation(i).Height); * } * else * { * cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); * }*/ var pageRotation = coverPdf.GetPageRotation(i); var pageWidth = coverPdf.GetPageSizeWithRotation(i).Width; var pageHeight = coverPdf.GetPageSizeWithRotation(i).Height; switch (pageRotation) { case 0: { cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); break; } case 90: { cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, pageHeight); break; } case 180: { cb1.AddTemplate(page, -1f, 0, 0, -1f, pageWidth, pageHeight); break; } case 270: { cb1.AddTemplate(page, 0, 1f, -1f, 0, pageWidth, 0); break; } default: throw new InvalidOperationException(string.Format( "Unexpected page rotation: [{0}].", pageRotation)); } } } // copy pages from input to output document for (int i = k; i < (k + pages); i++) { inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(i)); inputDoc.NewPage(); PdfImportedPage page = outputWriter.GetImportedPage(inputPdf, i); /* * int rotation = inputPdf.GetPageRotation(i); * * if (rotation == 90 || rotation == 270) * { * cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, inputPdf.GetPageSizeWithRotation(i).Height); * } * else * { * cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); * }*/ var pageRotation = inputPdf.GetPageRotation(i); var pageWidth = inputPdf.GetPageSizeWithRotation(i).Width; var pageHeight = inputPdf.GetPageSizeWithRotation(i).Height; switch (pageRotation) { case 0: { cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); break; } case 90: { cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, pageHeight); break; } case 180: { cb1.AddTemplate(page, -1f, 0, 0, -1f, pageWidth, pageHeight); break; } case 270: { cb1.AddTemplate(page, 0, 1f, -1f, 0, pageWidth, 0); break; } default: throw new InvalidOperationException(string.Format( "Unexpected page rotation: [{0}].", pageRotation)); } } if (!string.IsNullOrWhiteSpace(_footerPath)) { // copy pages from Footer document to output document first for (int i = 1; i <= footerPdf.NumberOfPages; i++) { inputDoc.SetPageSize(footerPdf.GetPageSizeWithRotation(i)); inputDoc.NewPage(); PdfImportedPage page = outputWriter.GetImportedPage(footerPdf, i); /* * int rotation = footerPdf.GetPageRotation(i); * * if (rotation == 90 || rotation == 270) * { * cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, footerPdf.GetPageSizeWithRotation(i).Height); * } * else * { * cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); * }*/ var pageRotation = footerPdf.GetPageRotation(i); var pageWidth = footerPdf.GetPageSizeWithRotation(i).Width; var pageHeight = footerPdf.GetPageSizeWithRotation(i).Height; switch (pageRotation) { case 0: { cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); break; } case 90: { cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, pageHeight); break; } case 180: { cb1.AddTemplate(page, -1f, 0, 0, -1f, pageWidth, pageHeight); break; } case 270: { cb1.AddTemplate(page, 0, 1f, -1f, 0, pageWidth, 0); break; } default: throw new InvalidOperationException(string.Format( "Unexpected page rotation: [{0}].", pageRotation)); } } } inputDoc.Close(); count++; } } if (footerPdf != null) { footerPdf.Close(); } if (coverPdf != null) { coverPdf.Close(); } inputPdf.Close(); }