public PdfDrawer(PdfContentByte contentByte, ref BoxConfig messageConfig, ref BoxLineConfig optionConfig) { if (contentByte == null) { throw new ApplicationException("PdfContentByte nullo."); } this.canvas = contentByte; this.canvas.SetGState(new PdfGState { FillOpacity = messageConfig.Opacity.Value, StrokeOpacity = messageConfig.Opacity.Value }); this.layoutInstances = new Dictionary <PdfLayout, object>(); this.msgConfig = messageConfig; this.optCfg = optionConfig; }
/// <summary> /// Assegna i valori non impostati leggendoli dal file di configurazione. /// </summary> /// <param name="source"></param> /// <returns></returns> public static BoxConfig MergeWithConfig(BoxConfig source, log4net.ILog logger = null) { if (logger != null) { logger.Debug("BoxConfig::MergeWithConfig - INIT"); } var key = ConfigurationManager.AppSettings["DefaultBoxConfig"]; if (logger != null) { logger.DebugFormat("BoxConfig::MergeWithConfig - readed key = {0}", key); } if (string.IsNullOrEmpty(key)) { if (logger != null) { logger.Debug("BoxConfig::MergeWithConfig - END"); } return(source); } var retval = source ?? new BoxConfig(); try { var configured = JsonConvert.DeserializeObject <BoxConfig>(key); if (!retval.x.HasValue) { retval.x = configured.x; } if (!retval.y.HasValue) { retval.y = configured.y; } if (!retval.pageNumber.HasValue) { retval.pageNumber = configured.pageNumber; } if (!retval.isInline.HasValue) { retval.isInline = configured.isInline; } if (!retval.borderWidth.HasValue) { retval.borderWidth = configured.borderWidth; } if (!retval.opacity.HasValue) { retval.opacity = configured.opacity; } if (string.IsNullOrEmpty(retval.bgColor)) { retval.bgColor = configured.bgColor; } if (string.IsNullOrEmpty(retval.borderColor)) { retval.borderColor = configured.borderColor; } if (retval.BoxLine == null || retval.BoxLine.Length < 1) { retval.BoxLine = new[] { BoxLineConfig.GetDefault() }; } else { BoxLineConfig line; for (int idx = 0; idx < retval.BoxLine.Length; idx++) { line = retval.BoxLine[idx]; line = BoxLineConfig.MergeWithConfig(line, logger); } } } catch (Exception ex) { if (logger != null) { logger.Error(ex); logger.Debug("BoxConfig::MergeWithConfig - END"); } return(source); } if (logger != null) { logger.Debug("BoxConfig::MergeWithConfig - END"); } return(retval); }
/// <summary> /// /// </summary> /// <param name="documentContent"></param> /// <param name="fileName"></param> /// <param name="ext"></param> /// <param name="label"></param> /// <param name="formBoxConfig"></param> /// <returns></returns> public byte[] ConvertToFormatLabeledWithForm(byte[] documentContent, string fileName, string ext, string label, BoxConfig formBoxConfig) { if (formBoxConfig == null) { throw new ConfigurationException("Nessuna informazione di configurazione legata alla form fornita in input."); } if (formBoxConfig.BoxLine == null || !formBoxConfig.BoxLine.Any()) { throw new ConfigurationException("Nessuna informazione di configurazione legata alle opzioni fornita in input."); } formBoxConfig = BoxConfig.MergeWithConfig(formBoxConfig, logger); if (formBoxConfig.DrawingPageNumber < -1) { formBoxConfig.DrawingPageNumber = -1; } if (formBoxConfig.DrawingPageNumber == 0) { formBoxConfig.DrawingPageNumber = 1; } var numPagina = formBoxConfig.DrawingPageNumber.Value; var buffer = ConvertToFormatLabeled(documentContent, fileName, ext, label); var fname = Path.GetTempFileName() + ".pdf"; try { using (var fs = File.Create(fname)) { var pdfReader = new PdfReader(buffer); try { //Non si può scrivere in una pagina inesistente: quindi viene impostata l'ultima pagina valida del documento. if (numPagina < 1 || numPagina > pdfReader.NumberOfPages) { numPagina = pdfReader.NumberOfPages; } //Recupera le dimensioni della pagina in cui si sta scrivendo. var paginaPerDimensioni = pdfReader.GetPageSizeWithRotation(numPagina); //Re-imposta le dimensioni a seconda del formato della pagina. formBoxConfig.X = (int)Math.Ceiling(Math.Abs(paginaPerDimensioni.Left - formBoxConfig.X.Value)); formBoxConfig.Y = (int)Math.Ceiling(Math.Abs(paginaPerDimensioni.Top - formBoxConfig.Y.Value)); try { using (var pdfStamper = new PdfStamper(pdfReader, fs)) { try { var canvas = pdfStamper.GetOverContent(numPagina); var cfgOpzioni = formBoxConfig.BoxLine.First(); using (var layout = new PdfDrawer(canvas, ref formBoxConfig, ref cfgOpzioni).BeginLayout()) { foreach (var opzione in formBoxConfig.BoxLine) { layout.AddMessage(opzione.Message, opzione.SelectedValue, opzione.Options); } } pdfStamper.Writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);//i tried with 1_7instead, same result pdfStamper.Writer.PDFXConformance = PdfWriter.PDFX1A2001; pdfStamper.Writer.CreateXmpMetadata(); } finally { pdfStamper.Close(); } } } finally { pdfReader.Close(); } } finally { try { pdfReader.Close(); } catch { } fs.Close(); } } buffer = File.ReadAllBytes(fname); } finally { try { File.Delete(fname); } catch { } } return(buffer); }