//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ internal bool CreatePDF(string PDFPath, ProgressBar PBar) { try { if (Tabs.Count > 0) { PBar.Invoke(new Action(() => { PBar.Maximum = Tabs.Count; PBar.Value = 0; PBar.Visible = true; PBar.Style = ProgressBarStyle.Marquee; })); if (File.Exists(PDFPath)) { File.Delete(PDFPath); } var CurDocument = new iTextSharp.text.Document(); var CurPdfWriter = PdfWriter.GetInstance(CurDocument, new FileStream(PDFPath, FileMode.Create, FileAccess.Write, FileShare.None)); CurPdfWriter.SetFullCompression(); Tabs = Tabs.OrderBy(o => o.Index).ToList(); for (int i = 0; i < Tabs.Count; i++) { var CurImage = Tabs[i].ScannedImage; var CurRectangle = new iTextSharp.text.Rectangle(CurImage.Width, CurImage.Height); CurDocument.SetPageSize(CurRectangle); CurDocument.SetMargins(0, 0, 0, 0); if (i == 0) { CurDocument.Open(); } if (i < (Tabs.Count - 1)) { CurDocument.NewPage(); } CurDocument.Add(iTextSharp.text.Image.GetInstance(CurImage, (iTextSharp.text.BaseColor)null)); PBar.Invoke(new Action(() => { PBar.Style = ProgressBarStyle.Continuous; PBar.Value++; })); } CurDocument.Close(); } else { MessageBox.Show("Keine Scanns vorhanden"); } GC.Collect(); return(true); } catch (Exception ex) { Program.MeldeFehler(ex.Message + "\n" + ex.StackTrace); Environment.Exit(1); return(false); } }
public void Save(string fileName, Func <List <IElementRoot>, List <IElementRoot> > formatFunc = null) { if (formatFunc != null) { formatFunc.Invoke(Paragraphs); } string defaultFontPath = FontUtils.GetFontPath(ReportFactory.FontList, ReportFactory.FontStyle); iTextSharp.text.FontFactory.Register(defaultFontPath); using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)) { PageSize currentPageSize = PageSize ?? ReportFactory.PageSize; PageOrientation pageOrientation = PageOrientation ?? ReportFactory.PageOrientation; iTextSharp.text.Rectangle pageSize = currentPageSize.ToRectangle(pageOrientation); using (var document = new iTextSharp.text.Document(pageSize)) { float marginTop = MarginLeft ?? ReportFactory.MarginLeft; float marginRight = MarginLeft ?? ReportFactory.MarginRight; float marginBottom = MarginLeft ?? ReportFactory.MarginBottom; float marginLeft = MarginLeft ?? ReportFactory.MarginLeft; document.SetMargins(marginLeft, marginRight, marginTop, marginBottom); iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, fileStream); document.Open(); foreach (var para in Paragraphs) { if (para is Table) { iTextSharp.text.pdf.PdfPTable tableSource = para as Table; document.Add(tableSource); } else if (para is Paragraph) { iTextSharp.text.Paragraph paragraph = para as Paragraph; document.Add(paragraph); } } document.Close(); } fileStream.Close(); } }
public static void Convert(List <Tiff.PageInfo> il, string dstPath) { using (var file_stream = new FileStream(dstPath, FileMode.Create, FileAccess.Write, FileShare.None)) using (var document = new iTextSharp.text.Document()) using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, file_stream)) { document.SetMargins(0, 0, 0, 0); document.Open(); foreach (Tiff.PageInfo info in il) { try { bool needRotate = info.Image.Width > info.Image.Height; if (needRotate) { info.Image.RotateFlip(RotateFlipType.Rotate90FlipNone); } Image gif = Image.GetInstance(info.Image, ImageFormat .Png); gif.ScaleAbsolute((float)(info.Image.Width * (72.0 / info.Image.HorizontalResolution)), (float)(info.Image.Height * (72.0 / info.Image.VerticalResolution))); gif.SetAbsolutePosition(1, 1); document.SetPageSize(new Rectangle(gif.ScaledWidth, gif.ScaledHeight)); document.NewPage(); pdfWriter.DirectContent.AddImage(gif); if (needRotate) { pdfWriter.AddPageDictEntry(PdfName.ROTATE, new PdfNumber(270)); } } catch (Exception ex) { Data.Env.WriteToLog(ex); return; } } document.Close(); } }
/// <summary> /// Create a new PDF document from contents /// </summary> /// <param name="path"></param> /// <param name="contents"></param> /// <param name="page"></param> /// <param name="marginLeft"></param> /// <param name="marginTop"></param> /// <param name="marginRight"></param> /// <param name="marginBottom"></param> public static void CreateNewPDF(string path, IEnumerable <object> contents, Content.Page page, double marginLeft = 5, double marginTop = 10, double marginRight = 5, double marginBottom = 10) { FileStream fs = new System.IO.FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); iTextSharp.text.Document doc = new iTextSharp.text.Document(page.ToPDF()); PdfWriter writer = PdfWriter.GetInstance(doc, fs); doc.SetMargins((float)marginLeft, (float)marginRight, (float)marginTop, (float)marginBottom); doc.Open(); foreach (object content in contents) { var t = content.GetType(); if (content.GetType().GetInterfaces().Contains(typeof(Content.IPDFContent))) { if (content.GetType() == typeof(Content.PageBreak)) { doc.NewPage(); } else { Content.IPDFContent co = content as Content.IPDFContent; doc.Add(co.ToPDF()); } } else { if (content.GetType() == typeof(Geometries.PDFGeometry)) { Geometries.PDFGeometry l = (Geometries.PDFGeometry)content; l.ToPDF(writer); } else { doc.Add(new iTextSharp.text.Paragraph(content.ToString())); } } } doc.Close(); }
public void SaveImage(HttpContext context) { var aa = context.Request.Form["svg"]; if (context.Request.Form["svg"] != null) { string tType = "image/png"; string tSvg = context.Request.Form["svg"].ToString(); string tFileName = ""; Random rand = new Random(24 * (int)DateTime.Now.Ticks); tFileName = rand.Next().ToString(); MemoryStream tData = new MemoryStream(Encoding.UTF8.GetBytes(tSvg)); MemoryStream tStream = new MemoryStream(); string tTmp = new Random().Next().ToString(); string tExt = ""; string tTypeString = ""; switch (tType) { case "image/png": tTypeString = "-m image/png"; tExt = "png"; break; case "image/jpeg": tTypeString = "-m image/jpeg"; tExt = "jpg"; break; case "application/pdf": tTypeString = "-m application/pdf"; tExt = "pdf"; break; case "image/svg+xml": tTypeString = "-m image/svg+xml"; tExt = "svg"; break; } if (tTypeString != "") { //string tWidth = context.Request.Form["width"].ToString(); //string tWidth = "0"; Svg.SvgDocument tSvgObj = SvgDocument.Open(tData); switch (tExt) { case "jpg": tSvgObj.Draw().Save(tStream, ImageFormat.Jpeg); break; case "png": tSvgObj.Draw().Save(tStream, ImageFormat.Png); break; case "pdf": PdfWriter tWriter = null; iTextSharp.text.Document tDocumentPdf = null; try { tSvgObj.Draw().Save(tStream, ImageFormat.Png); tDocumentPdf = new iTextSharp.text.Document(new iTextSharp.text.Rectangle((float)tSvgObj.Width, (float)tSvgObj.Height)); tDocumentPdf.SetMargins(0.0f, 0.0f, 0.0f, 0.0f); iTextSharp.text.Image tGraph = iTextSharp.text.Image.GetInstance(tStream.ToArray()); tGraph.ScaleToFit((float)tSvgObj.Width, (float)tSvgObj.Height); tStream = new MemoryStream(); tWriter = PdfWriter.GetInstance(tDocumentPdf, tStream); tDocumentPdf.Open(); tDocumentPdf.NewPage(); tDocumentPdf.Add(tGraph); tDocumentPdf.CloseDocument(); } catch (Exception ex) { throw ex; } finally { tDocumentPdf.Close(); tDocumentPdf.Dispose(); tWriter.Close(); tWriter.Dispose(); tData.Dispose(); tData.Close(); } break; case "svg": tStream = tData; break; } System.IO.MemoryStream ms = new System.IO.MemoryStream(tStream.ToArray()); System.Drawing.Image image = System.Drawing.Image.FromStream(ms); string savePath = context.Server.MapPath("image/"); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } savePath += tFileName + "." + tExt; string SavePathImage = tFileName + "." + tExt; context.Session["FirstImage"] = savePath; image.Save(savePath, System.Drawing.Imaging.ImageFormat.Png); image.Dispose(); context.Response.Write(tFileName + "." + tExt); } } }