public Report(System.IO.FileStream fileXML, string path, string titolo, string overrideFooter) { try { //Esegue il parsing del file XML e riempie un apposito ValueObject docTemplate = StampaXML.CreateDocTemplate(fileXML, path); if (!string.IsNullOrEmpty(titolo)) { docTemplate.page.headerPage.text = titolo; } } catch (ReportException re) { //Si è verificato un errore nel parsing del file XML //Il dettaglio è contenuto nel messaggio che viene propagato. throw re; } catch (Exception e) { //Si è verificato un errore nel parsing del file XML //Il dettaglio è contenuto nel messaggio che viene propagato. throw new ReportException(ErrorCode.XmlFileNotFound, "Errore durante l'esame del file XML: " + e.Message); } try { docPDF = StampaPDF.createDocPDF(docTemplate, overrideFooter); } catch (ReportException re) { //Si è verificato un errore nella creazione del file PDF //Il dettaglio è contenuto nel messaggio che viene propagato. throw re; } catch (Exception e) { //Si è verificato un errore nella creazione del file PDF //Il dettaglio è contenuto nel messaggio che viene propagato. throw new ReportException(ErrorCode.BadPDFFile, "Errore durante la creazione del file PDF: " + e.Message); } }
public static DocumentPDF printDataPage(DocumentPDF docPDF, StampaVO.Document templateDoc, DataTable dt) { //Stampa nel PDF gli elementi che contengono dati parametrizzati (Tabelle o Paragrafi) //A seconda della tipologia esegue l'uno o l'altro metodo. if (templateDoc != null && templateDoc.dataToPrint != null && templateDoc.dataToPrint.Count > 0) { for (int i = 0; i < templateDoc.dataToPrint.Count; i++) { string target = ((StampaVO.DataElement)templateDoc.dataToPrint[i]).target; if (target == null || target.Equals("")) { if (templateDoc.dataToPrint[i].GetType().Equals(typeof(StampaVO.Table))) { printTable((StampaVO.Table)templateDoc.dataToPrint[i], dt, docPDF); } else if (templateDoc.dataToPrint[i].GetType().Equals(typeof(StampaVO.Paragraph))) { printParagraph((StampaVO.Paragraph)templateDoc.dataToPrint[i], docPDF); } } } } //else // docPDF = printNoData(docPDF); return(docPDF); }
private static DocumentPDF setHeader(StampaVO.Document templateDoc, DocumentPDF docPDF) { //Creazione dell'header //TODO: gestire il testo, il numPagine, la dtaStampa StampaVO.Font font = templateDoc.page.headerPage.font; Font font1 = FontFactory.GetFont(font.name, font.size, Utils.getFontStyle(font.style), Utils.getColor(font.color)); string str_header = templateDoc.page.headerPage.text; if (templateDoc.page.headerPage.dtaStampa != null) { if (str_header != null && !str_header.Equals("")) { str_header += "\n"; } str_header += templateDoc.page.headerPage.dtaStampa.text + DateTime.Parse(DateTime.Now.ToString()).ToShortDateString(); } Phrase p = new Phrase(str_header, font1); HeaderFooter header = new HeaderFooter(p, false); if (templateDoc.page.headerPage.border == null || templateDoc.page.headerPage.border.Equals("0")) { header.Border = Rectangle.NO_BORDER; } header.Alignment = Utils.getAlign(templateDoc.page.headerPage.align); docPDF.Header = header; header.BackgroundColor = Utils.getColor(templateDoc.page.headerPage.bgcolor); return(docPDF); }
private static DocumentPDF setFooter(StampaVO.Document templateDoc, DocumentPDF docPDF, string overrideFooter) { //Creazione del footer //TODO: gestire numPagine, la dtaStampa ??? if (templateDoc.page.footerPage == null) { return(docPDF); } StampaVO.Font font = templateDoc.page.footerPage.font; Font font1 = FontFactory.GetFont(font.name, font.size, Utils.getFontStyle(font.style), Utils.getColor(font.color)); string str_footer = templateDoc.page.footerPage.text; if (templateDoc.page.footerPage.dtaStampa != null) { if (str_footer != null && !str_footer.Equals("")) { str_footer += "\n"; } if (string.IsNullOrEmpty(overrideFooter)) { str_footer += templateDoc.page.footerPage.dtaStampa.text + DateTime.Parse(DateTime.Now.ToString()).ToShortDateString(); } else { str_footer += templateDoc.page.footerPage.dtaStampa.text + overrideFooter; } } Phrase p = new Phrase(str_footer, font1); HeaderFooter footer; #region OLD GESTIONE NUM PAGE //Phrase pPage = null; //if (templateDoc.page.footerPage.numPagine != null) //{ // string str_Page = templateDoc.page.footerPage.numPagine.text; // StampaVO.Font fontPage = templateDoc.page.footerPage.numPagine.font; // Font font2 = FontFactory.GetFont(fontPage.name, fontPage.size, Utils.getFontStyle(fontPage.style), Utils.getColor(fontPage.color)); // pPage = new Phrase(str_Page, font2); //} //if (pPage != null) // footer = new HeaderFooter(pPage, p); //il true dovrebbe dare il num. di pagina //else // footer = new HeaderFooter(p, false); #endregion footer = new HeaderFooter(p, false); if (templateDoc.page.footerPage.border == null || templateDoc.page.footerPage.border.Equals("0")) { footer.Border = Rectangle.NO_BORDER; } footer.Alignment = Utils.getAlign(templateDoc.page.footerPage.align); docPDF.Footer = footer; return(docPDF); }
private static DocumentPDF setPage(StampaVO.Document templateDoc, DocumentPDF docPDF) { //Imposta la dimensione del foglio e i margini //PAGE SIZE & ORIENTATION bool isRotated = false; Rectangle rect = new Rectangle(PageSize.A4); switch (templateDoc.page.pageSize) { case "A4": rect = PageSize.A4; break; case "A3": rect = PageSize.A3; break; case "_11X17": rect = PageSize._11X17; break; case "LETTER": rect = PageSize.LETTER; break; default: rect = PageSize.A4.Rotate(); isRotated = true; break; } if (templateDoc.page.orientation.Equals(StampaVO.Page.ORIZZONTALE) && !isRotated) { rect = rect.Rotate(); } docPDF.SetPageSize(rect); //PAGE MARGINS if (templateDoc.page.margins != null) { docPDF.SetMargins(templateDoc.page.margins[0], templateDoc.page.margins[1], templateDoc.page.margins[2], templateDoc.page.margins[3]); /* * if (!templateDoc.page.margins[0].Equals(0)) * docPDF.Left(templateDoc.page.margins[0]); * if (!templateDoc.page.margins[1].Equals(0)) * docPDF.Right(templateDoc.page.margins[1]); * if (!templateDoc.page.margins[2].Equals(0)) * docPDF.Top(templateDoc.page.margins[2]); * if (!templateDoc.page.margins[3].Equals(0)) * docPDF.Bottom(templateDoc.page.margins[3]); */ } return(docPDF); }
private static StampaVO.Document LoadXmlDoc(XmlDocument xmlDoc) { //carico xml StampaVO.Document templateDoc = null; nodoReport = xmlDoc.SelectSingleNode("report"); if (nodoReport != null && nodoReport.ChildNodes != null && nodoReport.ChildNodes.Count > 0) { templateDoc = new StampaVO.Document(); //elemento pagina: style - header - footer : templateDoc.page = setPage(); //elemento dati - testo - spazio - tabella templateDoc.dataToPrint = setDati(); } return(templateDoc); }
private static DocumentPDF setLogo(StampaVO.Document templateDoc, DocumentPDF docPDF) { if (templateDoc.page.logo != null && templateDoc.page.logo.fileImage != null && !templateDoc.page.logo.fileImage.Equals("")) { //NB: attenzione ai file in formato gif string[] pathProspettiRiepilogativi = templateDoc.pathName.Split(new string[] { "ProspettiRiepilogativi" }, StringSplitOptions.None); if (pathProspettiRiepilogativi.Length > 0) { string fileLogo = pathProspettiRiepilogativi[0] + "\\ProspettiRiepilogativi\\Frontend\\images\\" + templateDoc.page.logo.fileImage; Image logo = Image.GetInstance(fileLogo); logo.Alignment = Utils.getImageAlign(templateDoc.page.logo.align); logo.Alt = "logo"; logo.BackgroundColor = Utils.getColor(templateDoc.page.logo.bgColor); docPDF.Add(logo); } } return(docPDF); }
/// <summary> /// overloading per passare nome e cognome dell'utente loggato,in modo da evidenziare le trasmissioni di cui l'utente è destinatario /// Dimitri /// </summary> /// <param name="dt"></param> /// <param name="docPDF"></param> /// <param name="templateDoc"></param> /// <param name="target"></param> /// <param name="newPage"></param> /// <param name="Utente"></param> /// <returns></returns> public static DocumentPDF appendData(DataTable dt, DocumentPDF docPDF, StampaVO.Document templateDoc, string target, bool newPage, DocsPaVO.utente.InfoUtente infoUt) { try { if (templateDoc != null) { if (templateDoc.dataToPrint != null && templateDoc.dataToPrint.Count > 0) { bool trovato = false; StampaVO.Table table; for (int i = 0; (i < templateDoc.dataToPrint.Count && !trovato); i++) { if (((StampaVO.DataElement)templateDoc.dataToPrint[i]).target.Equals(target)) { table = (StampaVO.Table)templateDoc.dataToPrint[i]; if (newPage) { docPDF.NewPage(); } if (dt.TableName != "REP_TRASMISSIONI") { docPDF = printTable(table, dt, docPDF); } else { docPDF = printCustomTable(table, dt, docPDF, infoUt); } trovato = true; } } } } } catch (Exception ex) { docPDF.Close(); writer.Close(); throw new ReportException(ErrorCode.IncompletePDFFile, "Errore nella scrittura dei dati: " + ex.Message); } return(docPDF); }
public static StampaVO.Document CreateDocTemplate(FileStream fileXML, string path) { StampaVO.Document templateDoc = null; try { xmlDoc = GetXmlDoc(fileXML, path); if (xmlDoc != null) { templateDoc = LoadXmlDoc(xmlDoc); templateDoc.pathName = path; } } catch (ReportException re) { throw re; } catch (Exception ex) { throw new ReportException(ErrorCode.InvalidXmlFile, "Errore nella creazione del template XML: " + ex.Message); } return(templateDoc); }
public static DocumentPDF appendData(string testo, DocumentPDF docPDF, StampaVO.Document templateDoc, string target, bool newPage) { try { if (templateDoc != null) { if (templateDoc.dataToPrint != null && templateDoc.dataToPrint.Count > 0) { bool trovato = false; StampaVO.Paragraph paragrafo; for (int i = 0; (i < templateDoc.dataToPrint.Count && !trovato); i++) { if (((StampaVO.DataElement)templateDoc.dataToPrint[i]).target.Equals(target)) { paragrafo = (StampaVO.Paragraph)templateDoc.dataToPrint[i]; if (testo != null && !testo.Equals("")) { paragrafo.text = testo; } if (newPage) { docPDF.NewPage(); } docPDF = printParagraph(paragrafo, docPDF); trovato = true; } } } } } catch (Exception ex) { docPDF.Close(); writer.Close(); throw new ReportException(ErrorCode.IncompletePDFFile, "Errore nella scrittura dei dati: " + ex.Message); } return(docPDF); }
public static DocumentPDF appendData(Hashtable parameters, DocumentPDF docPDF, StampaVO.Document templateDoc, string target, bool newPage) { try { if (templateDoc != null) { if (templateDoc.dataToPrint != null && templateDoc.dataToPrint.Count > 0) { bool trovato = false; StampaVO.Paragraph paragrafo; for (int i = 0; (i < templateDoc.dataToPrint.Count && !trovato); i++) { if (target == null || ((StampaVO.DataElement)templateDoc.dataToPrint[i]).target.Equals(target)) { if (target == null) { trovato = true; } paragrafo = (StampaVO.Paragraph)templateDoc.dataToPrint[i]; //sostituzione parametri if (!paragrafo.text.Equals("")) { //Sostituisce i parametri foreach (string key in parameters.Keys) { string pval = parameters[key].ToString(); if (pval == null) { pval = ""; } paragrafo.text = paragrafo.text.Replace(key, pval); } } if (newPage) { docPDF.NewPage(); } docPDF = printParagraph(paragrafo, docPDF); trovato = true; } } } } } catch (Exception ex) { docPDF.Close(); writer.Close(); throw new ReportException(ErrorCode.IncompletePDFFile, "Errore nella scrittura dei dati: " + ex.Message); } return(docPDF); }
public static DocumentPDF createDocPDF(StampaVO.Document templateDoc, string overrideFooter) { DocumentPDF docPDF = null; if (templateDoc == null) { throw new ReportException(ErrorCode.InvalidValueObject, "Template di formato non valido"); } try { try { //Creazione del Documento vuoto docPDF = new DocumentPDF(); docTmp = templateDoc; writer = PdfWriter.GetInstance(docPDF, docPDF.memoryStream); PDFPageEvents events = new PDFPageEvents(); writer.PageEvent = events; } catch (Exception e) { writer.Close(); throw new ReportException(ErrorCode.BadPDFFile, e.Message); } try { //Settaggio delle proprietà della pagina docPDF = setPage(templateDoc, docPDF); } catch (Exception ex) { writer.Close(); throw new ReportException(ErrorCode.IncompletePDFFile, "Fallita impostazione della Pagina: " + ex.Message); } try { //Creazione dell'header docPDF = setHeader(templateDoc, docPDF); } catch (Exception ex) { writer.Close(); throw new ReportException(ErrorCode.IncompletePDFFile, "Fallita impostazione dell'Header: " + ex.Message); } try { //Creazione del footer docPDF = setFooter(templateDoc, docPDF, overrideFooter); } catch (Exception ex) { writer.Close(); throw new ReportException(ErrorCode.IncompletePDFFile, "Fallita impostazione del Footer: " + ex.Message); } try { if (!docPDF.AddCreationDate()) { throw new Exception(); } } catch (Exception ex) { throw new ReportException(ErrorCode.IncompletePDFFile, "Fallita impostazione della Data di Stampa: " + ex.Message); } docPDF.Open(); try { docPDF = setLogo(templateDoc, docPDF); } catch (Exception ex) { docPDF.Close(); writer.Close(); throw new ReportException(ErrorCode.IncompletePDFFile, "Fallita impostazione del Logo: " + ex.Message); } } catch (ReportException re) { docPDF.Close(); writer.Close(); docPDF = null; throw re; } catch (Exception e) { docPDF.Close(); writer.Close(); docPDF = null; throw new ReportException(ErrorCode.GenericError, e.Message); } return(docPDF); }