Exemple #1
0
        public static byte[] CreatePdfDataFromDataGridView(DataGridView grid, int startColumn, int endColumn, string title)
        {
            byte[]       pdfData  = null;
            PdfDocument  document = null;
            MemoryStream ms       = null;

            try
            {
                ms       = new MemoryStream();
                document = new PdfDocument();
                PdfWriter writer = PdfWriter.GetInstance(document, ms);

                document.Open();

                PdfContentByte cb = writer.DirectContent;

                cb.BeginText();

                float fontSizeHeader = 14f;
                float topCursor      = document.GetTop(10f);

                cb.SetFontAndSize(baseFontTimesBold, fontSizeHeader);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, title, document.GetLeft(10f), topCursor, 0);
                topCursor -= 20f;
                cb.EndText();

                int nCols = endColumn - startColumn;

                PdfPTable table = new PdfPTable(nCols);
                table.TotalWidth = document.GetRight(10) - document.GetLeft(10);

                for (int i = startColumn; i < endColumn; i++)
                {
                    PdfPCell cell = new PdfPCell(GetHeaderPhrase(grid.Columns[i].HeaderText));
                    cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
                    cell.VerticalAlignment   = PdfPCell.ALIGN_LEFT;
                    table.AddCell(cell);
                }

                for (int i = 0; i < grid.Rows.Count; i++)
                {
                    for (int j = startColumn; j < endColumn; j++)
                    {
                        PdfPCell cell = new PdfPCell(GetCellPhrase(grid.Rows[i].Cells[j].FormattedValue.ToString()));
                        cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
                        cell.VerticalAlignment   = PdfPCell.ALIGN_LEFT;
                        table.AddCell(cell);
                    }
                }

                float currHeight = topCursor;
                int   currRow = 0, pageRows = 0, nRows = grid.Rows.Count + 1;

                for (int i = 0; i < nRows; i++)
                {
                    currHeight -= table.GetRowHeight(i);
                    pageRows++;

                    if (currHeight <= document.GetBottom(10f))
                    {
                        table.WriteSelectedRows(currRow, currRow + pageRows, document.GetLeft(10), topCursor, cb);
                        document.NewPage();

                        currRow   += pageRows;
                        currHeight = topCursor = document.GetTop(10f);
                        pageRows   = 0;
                    }
                }

                if (pageRows > 0)
                {
                    table.WriteSelectedRows(currRow, currRow + pageRows, document.GetLeft(10), topCursor, cb);
                    document.NewPage();
                }
            }
            finally
            {
                document?.Close();
                if (ms != null)
                {
                    pdfData = ms.GetBuffer();
                }
            }

            return(pdfData);
        }
Exemple #2
0
        public static byte[] CreatePdfDataFromAssignment(SqlConnection conn, SqlTransaction trans, Assignment assignment)
        {
            byte[]       pdfData  = null;
            PdfDocument  document = null;
            MemoryStream ms       = null;

            try
            {
                ms       = new MemoryStream();
                document = new PdfDocument();
                PdfWriter writer = PdfWriter.GetInstance(document, ms);

                document.Open();

                PdfContentByte cb = writer.DirectContent;

                cb.BeginText();

                float fontSize = 10, fontSizeHeader = 16;
                float margin = 50;
                float leftCursor = margin, topCursor = document.Top - 10f, lineSpace = 13;

                PdfImage labLogo = GetLaboratoryLogo(conn, trans, assignment.LaboratoryId);
                if (labLogo != null)
                {
                    CropImageToHeight(labLogo, 64f);
                    labLogo.SetAbsolutePosition(document.GetRight(10f) - labLogo.PlainWidth, document.GetTop(10f) - 40f);
                    document.Add(labLogo);
                }

                cb.SetFontAndSize(baseFontTimesBold, fontSizeHeader);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "OPPDRAGSOVERSIKT", leftCursor, topCursor, 0);
                cb.SetFontAndSize(baseFontTimes, fontSize);
                topCursor -= lineSpace * 2;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Oppdragsnummer: " + assignment.Name, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Oppdrag opprettet: " + assignment.CreateDate.ToString(Utils.DateFormatNorwegian), leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Utskriftsdato: " + DateTime.Now.ToString(Utils.DateFormatNorwegian), leftCursor, topCursor, 0);
                topCursor -= lineSpace * 2;
                cb.SetFontAndSize(baseFontTimesBold, fontSize);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Oppdragsgiver, Firma/Avd.", leftCursor, topCursor, 0);
                cb.SetFontAndSize(baseFontTimes, fontSize);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Navn: " + assignment.CustomerCompanyName, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Addresse: " + assignment.CustomerCompanyAddress, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Epost: " + assignment.CustomerCompanyEmail, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Telefon: " + assignment.CustomerCompanyPhone, leftCursor, topCursor, 0);
                topCursor -= lineSpace * 2;
                cb.SetFontAndSize(baseFontTimesBold, fontSize);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Oppdragsgiver, Kontakt", leftCursor, topCursor, 0);
                cb.SetFontAndSize(baseFontTimes, fontSize);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Navn: " + assignment.CustomerContactName, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Addresse: " + assignment.CustomerContactAddress, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Epost: " + assignment.CustomerContactEmail, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Telefon: " + assignment.CustomerContactPhone, leftCursor, topCursor, 0);
                topCursor -= lineSpace * 2;
                cb.SetFontAndSize(baseFontTimesBold, fontSize);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Laboratorium", leftCursor, topCursor, 0);
                cb.SetFontAndSize(baseFontTimes, fontSize);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Navn: " + assignment.LaboratoryName(conn, trans), leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Kontakt: " + assignment.ResponsibleName(conn, trans), leftCursor, topCursor, 0);
                topCursor -= lineSpace * 2;
                cb.SetFontAndSize(baseFontTimesBold, fontSize);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Måleresultater", leftCursor, topCursor, 0);
                cb.EndText();

                topCursor -= lineSpace;

                PdfPTable table = new PdfPTable(13);
                table.TotalWidth = document.GetRight(margin) - document.GetLeft(margin);

                PdfPCell cell = new PdfPCell(GetHeaderPhrase("Analysis"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Sample Type"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("P.Status"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("A.Status"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Method"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Nuclide"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Activity"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Act.Unc."));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Act.Appr."));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("MDA"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("MDA Appr."));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Reportable"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Accredited"));
                table.AddCell(cell);

                string query = @"
select 
    s.number as 'sample', 
    st.name as 'sample_type_name',
    sc.name as 'sample_component_name',
	p.number as 'preparation', 
    pm.name_short as 'preparation_method', 
    p.workflow_status_id as 'preparation_wfstatus', 
	a.number as 'analysis',     
    a.workflow_status_id as 'analysis_wfstatus', 
	am.name_short as 'analysis_method', 
	n.name as 'nuclide_name', 
	ar.activity as 'act', 
	ar.activity_uncertainty_abs as 'act.unc', 
	ar.activity_approved as 'act.appr', 
	ar.detection_limit as 'det.lim', 
	ar.detection_limit_approved as 'det.lim.appr', 
	ar.reportable, 
	ar.accredited
from sample s
    inner join sample_type st on st.id = s.sample_type_id
    left outer join sample_component sc on sc.id = s.sample_component_id
    inner join preparation p on p.sample_id = s.id and p.instance_status_id = 1
    inner join preparation_method pm on pm.id = p.preparation_method_id
    inner join analysis a on a.preparation_id = p.id and a.instance_status_id = 1 and a.assignment_id = @assignment_id
    inner join analysis_result ar on ar.analysis_id = a.id and ar.instance_status_id = 1 and ar.reportable = 1
    inner join analysis_method am on am.id = a.analysis_method_id
    inner join nuclide n on n.id = ar.nuclide_id
order by s.number, p.number, a.number
";
                int    nRows = 1;
                using (SqlDataReader reader = DB.GetDataReader(conn, trans, query, CommandType.Text, new[] {
                    new SqlParameter("@assignment_id", assignment.Id)
                }))
                {
                    while (reader.Read())
                    {
                        int    pwfstat  = reader.GetInt32("preparation_wfstatus");
                        string spwfstat = "Unknown";
                        switch (pwfstat)
                        {
                        case WorkflowStatus.Construction:
                            spwfstat = "Construction";
                            break;

                        case WorkflowStatus.Complete:
                            spwfstat = "Complete";
                            break;

                        case WorkflowStatus.Rejected:
                            spwfstat = "Rejected";
                            break;
                        }

                        int    awfstat  = reader.GetInt32("analysis_wfstatus");
                        string sawfstat = "Unknown";
                        switch (awfstat)
                        {
                        case WorkflowStatus.Construction:
                            sawfstat = "Construction";
                            break;

                        case WorkflowStatus.Complete:
                            sawfstat = "Complete";
                            break;

                        case WorkflowStatus.Rejected:
                            sawfstat = "Rejected";
                            break;
                        }

                        string sact = "";
                        if (DB.IsValidField(reader["act"]))
                        {
                            sact = reader.GetDouble("act").ToString(Utils.ScientificFormat);
                        }

                        string sactunc = "";
                        if (DB.IsValidField(reader["act.unc"]))
                        {
                            sactunc = reader.GetDouble("act.unc").ToString(Utils.ScientificFormat);
                        }

                        string sdetlim = "";
                        if (DB.IsValidField(reader["det.lim"]))
                        {
                            sdetlim = reader.GetDouble("det.lim").ToString(Utils.ScientificFormat);
                        }

                        cell = new PdfPCell(GetCellPhrase(reader.GetString("sample") + " / " + reader.GetString("preparation") + " / " + reader.GetString("analysis")));
                        table.AddCell(cell);
                        string sampleType = reader.GetString("sample_type_name");
                        if (DB.IsValidField(reader["sample_component_name"]))
                        {
                            sampleType += " / " + reader.GetString("sample_component_name");
                        }
                        cell = new PdfPCell(GetCellPhrase(sampleType));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(spwfstat));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(sawfstat));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("preparation_method") + " / " + reader.GetString("analysis_method")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("nuclide_name")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(sact));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(sactunc));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("act.appr")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(sdetlim));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("det.lim.appr")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("reportable")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("accredited")));
                        table.AddCell(cell);
                        nRows++;
                    }
                }

                float currHeight = topCursor;
                int   currRow = 0, pageRows = 0;

                for (int i = 0; i < nRows; i++)
                {
                    currHeight -= table.GetRowHeight(i);
                    pageRows++;

                    if (currHeight <= document.GetBottom(10f))
                    {
                        table.WriteSelectedRows(currRow, currRow + pageRows, document.GetLeft(10), topCursor, cb);
                        document.NewPage();

                        currRow   += pageRows;
                        currHeight = topCursor = document.GetTop(10f);
                        pageRows   = 0;
                    }
                }

                if (pageRows > 0)
                {
                    table.WriteSelectedRows(currRow, currRow + pageRows, document.GetLeft(10), topCursor, cb);
                    document.NewPage();
                }
            }
            finally
            {
                document?.Close();
                if (ms != null)
                {
                    pdfData = ms.GetBuffer();
                }
            }

            return(pdfData);
        }
        public Boolean Obtener_Pdf_CC(String pRutaArchivo, FirmaElectronicaModel oFirmaElectronicaModel, String pImg64)
        //Fin E.Z. 13/05/2016
        {
            Boolean b_Resultado = false;

            iTextSharp.text.Document           oDocument       = null;
            iTextSharp.text.pdf.PdfWriter      oPdfWriter      = null;
            iTextSharp.text.pdf.PdfContentByte oPdfContentByte = null;
            iTextSharp.text.Chunk        oChunk  = null;
            iTextSharp.text.HeaderFooter oFooter = null;
            try
            {
                using (FileStream fs = new FileStream(pRutaArchivo, FileMode.Create, FileAccess.Write))
                {
                    // Crear PDF
                    oDocument  = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 30, 30, 15, 25);
                    oPdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(oDocument, fs);

                    oDocument.Open();
                    oPdfContentByte = oPdfWriter.DirectContent;
                    oPdfContentByte.Stroke();
                    oPdfContentByte.SetLineWidth(0.2f);

                    oDocument.Add(Obtener_Logos(null));
                    oDocument.Add(Obtener_Blanco());
                    oDocument.Add(Obtener_Titulo("098923"));
                    oDocument.Add(Obtener_Blanco());
                    oDocument.Add(Obtener_Cliente(oFirmaElectronicaModel));
                    oDocument.Add(Obtener_Blanco());
                    oDocument.Add(Obtener_Cuenta(oFirmaElectronicaModel));
                    oDocument.Add(Obtener_Blanco());
                    oDocument.Add(Obtener_Solicitud(oFirmaElectronicaModel, "ARamirez"));
                    oDocument.Add(Obtener_Blanco());
                    oDocument.Add(Obtener_Blanco());
                    oDocument.Add(Obtener_Pie(oFirmaElectronicaModel));
                    #region "Firma"
                    //Firma 1
                    //oPdfContentByte.Rectangle(59f, 40.5f, 300f, 72.5f);
                    oPdfContentByte.BeginText();
                    oPdfContentByte.SetFontAndSize(iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA_BOLD, iTextSharp.text.pdf.BaseFont.WINANSI, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED), (float)8);
                    oPdfContentByte.ShowTextAligned(iTextSharp.text.Element.ALIGN_BASELINE, "Asesor de Servicio al Cliente Finantienda", 59f, 122.5f, 0);
                    oPdfContentByte.EndText();
                    oPdfContentByte.Stroke();
                    oPdfContentByte.BeginText();
                    oPdfContentByte.SetFontAndSize(iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.WINANSI, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED), (float)8);
                    oPdfContentByte.ShowTextAligned(iTextSharp.text.Element.ALIGN_BASELINE, "Cesar Mariñoas Asmat", 59f, 102.5f, 0);
                    oPdfContentByte.EndText();
                    oPdfContentByte.Stroke();

                    oPdfContentByte.Rectangle(385.5f, 40.5f, 150f, 72.5f);
                    oPdfContentByte.BeginText();
                    oPdfContentByte.SetFontAndSize(iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA_BOLD, iTextSharp.text.pdf.BaseFont.WINANSI, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED), (float)8);
                    oPdfContentByte.ShowTextAligned(iTextSharp.text.Element.ALIGN_BASELINE, "Firma del Cliente Titular", 430.5f, 122.5f, 0);
                    oPdfContentByte.EndText();
                    oPdfContentByte.Stroke();

                    if (!string.IsNullOrEmpty(pImg64))
                    {
                        iTextSharp.text.Image _imagen = iTextSharp.text.Image.GetInstance(ConvertStringBase64ToImage(oFirmaElectronicaModel.oFirmaElectronica.SIGSTRING_64));
                        _imagen.Border      = iTextSharp.text.Rectangle.NO_BORDER;
                        _imagen.BorderColor = iTextSharp.text.Color.WHITE;
                        _imagen.SetAbsolutePosition(385.5f, 55.8f);
                        _imagen.ScaleToFit(150f, 81.5f);

                        oDocument.Add(_imagen);
                    }
                    oDocument.Close();
                    b_Resultado = true;
                }
                #endregion
            }
            catch (Exception ex) { b_Resultado = false; }
            //
            return(b_Resultado);
        }
Exemple #4
0
    void CreateFromRawdataFile(string sourceTxtPath, string destinationPDFPath)
    {
        pdfdoc = new iTextSharp.text.Document();
        pdfdoc.SetPageSize(currentPageSize);

        gridCountInBlockRowHorizontal = (int)(currentPageSize.Width / gridSideLength);
        blockRowHeight         = gridSideLength * gridCountInBlockRowVertical;
        blockRowCount          = (int)(currentPageSize.Height / (blockRowHeight + blockRowInterval));
        rawDataCountInBlockRow = (int)(gridCountInBlockRowHorizontal * 40 * 512 / 1000);
        rawDataInterval        = currentPageSize.Width / rawDataCountInBlockRow;

        Debug.Log("gridCountInBlockRowHorizontal:" + gridCountInBlockRowHorizontal);
        Debug.Log("rawDataCountInBlockRow" + rawDataCountInBlockRow);

        if (File.Exists(destinationPDFPath))
        {
            File.Delete(destinationPDFPath);
        }

        pdfwriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfdoc, new FileStream(destinationPDFPath, FileMode.CreateNew));

        pdfdoc.Open();

        iTextSharp.text.pdf.PdfContentByte cb = pdfwriter.DirectContent;

        pdfdoc.NewPage();
        blockRowIndex = 0;
        rawDataIndex  = 0;

        iTextSharp.text.pdf.BaseFont font = iTextSharp.text.pdf.BaseFont.CreateFont();
        cb.SetFontAndSize(font, 60);
        cb.BeginText();
        cb.SetTextMatrix(100, currentPageSize.Height - 50);
        cb.ShowText("Test String .......");
        cb.EndText();

        System.IO.StreamReader reader = new StreamReader(sourceTxtPath);
        string theLine;
        int    rawData;

        while (!reader.EndOfStream)
        {
            theLine = reader.ReadLine();
            try{
                rawData = int.Parse(theLine);
            }catch (Exception e) {
                Debug.Log("Exception");
                continue;
            }

            rawData       = rawData > maxRawData ? maxRawData : rawData;
            rawData       = rawData < -maxRawData ? -maxRawData : rawData;
            scaledRawData = (rawData * (blockRowHeight / 2)) / maxRawData;

            if (rawDataIndex == 0)
            {
                currentBaseLine = currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval) + (blockRowHeight / 2));

                cb.SetColorStroke(iTextSharp.text.BaseColor.RED);

                //draw horizontal lines
                for (int i = 0; i <= gridCountInBlockRowVertical; i++)
                {
                    if (i % 5 == 0)
                    {
                        cb.SetLineWidth(2.5f);
                    }
                    else
                    {
                        cb.SetLineWidth(0.5f);
                    }
                    cb.MoveTo(0, currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval) + i * gridSideLength));
                    cb.LineTo(currentPageSize.Width, currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval) + i * gridSideLength));
                    cb.Stroke();
                }

                //draw vertical lines
                for (int j = 0; j <= gridCountInBlockRowHorizontal; j++)
                {
                    if (j % 5 == 0)
                    {
                        cb.SetLineWidth(2.5f);
                    }
                    else
                    {
                        cb.SetLineWidth(0.5f);
                    }
                    cb.MoveTo(j * gridSideLength, currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval)));
                    cb.LineTo(j * gridSideLength, currentPageSize.Height - (firstBlockRowOffsetInVertical + blockRowIndex * (blockRowHeight + blockRowInterval) + blockRowHeight));
                    cb.Stroke();
                }
                //prepare to draw ECG
                cb.SetLineWidth(1.5f);
                cb.SetColorStroke(iTextSharp.text.BaseColor.BLACK);

                cb.MoveTo(0, currentBaseLine);
            }

            cb.LineTo(rawDataIndex * rawDataInterval, currentBaseLine + scaledRawData);
            rawDataIndex++;
            if (rawDataIndex >= rawDataCountInBlockRow)
            {
                cb.Stroke();
                rawDataIndex = 0;
                blockRowIndex++;
            }
        }
        cb.Stroke();
        reader.Close();



        pdfdoc.Dispose();
        System.Diagnostics.Process.Start(destinationPDFPath);
    }
Exemple #5
0
        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);
        }
Exemple #6
0
        public static byte[] CreatePdfDataFromAssignment(SqlConnection conn, SqlTransaction trans, Guid assignmentId)
        {
            string   OrderName = "", LaboratoryName = "", ResponsibleName = "", CustomerName = "", CustomerCompany = "", CustomerAddress = "";
            PdfImage labLogo = null;

            using (SqlDataReader reader = DB.GetDataReader(conn, trans, "csp_select_assignment_flat", CommandType.StoredProcedure, new SqlParameter("@id", assignmentId)))
            {
                if (reader.HasRows)
                {
                    reader.Read();

                    OrderName       = reader.GetString("name");
                    LaboratoryName  = reader.GetString("laboratory_name");
                    ResponsibleName = reader.GetString("account_name");
                    CustomerName    = reader.GetString("customer_contact_name");
                    CustomerCompany = reader.GetString("customer_company_name");
                    CustomerAddress = reader.GetString("customer_contact_address");
                }
            }

            Guid labId = (Guid)DB.GetScalar(conn, trans, "select laboratory_id from assignment where id = @id", CommandType.Text, new SqlParameter("@id", assignmentId));

            if (Utils.IsValidGuid(labId))
            {
                using (SqlDataReader reader = DB.GetDataReader(conn, trans, "select laboratory_logo from laboratory where id = @id", CommandType.Text, new SqlParameter("@id", labId)))
                {
                    if (reader.HasRows)
                    {
                        reader.Read();

                        if (DB.IsValidField(reader["laboratory_logo"]))
                        {
                            labLogo = PdfImage.GetInstance((byte[])reader["laboratory_logo"]);
                        }
                    }
                }
            }

            byte[]       pdfData  = null;
            PdfDocument  document = null;
            MemoryStream ms       = null;

            try
            {
                ms       = new MemoryStream();
                document = new PdfDocument();
                PdfWriter writer = PdfWriter.GetInstance(document, ms);

                document.Open();

                PdfContentByte cb = writer.DirectContent;

                cb.BeginText();

                PdfBaseFont baseFont = PdfBaseFont.CreateFont(PdfBaseFont.TIMES_ROMAN, PdfBaseFont.CP1252, PdfBaseFont.NOT_EMBEDDED);
                PdfBaseFont baseFontBold = PdfBaseFont.CreateFont(PdfBaseFont.TIMES_BOLD, PdfBaseFont.CP1252, PdfBaseFont.NOT_EMBEDDED);
                PdfBaseFont baseFontItalic = PdfBaseFont.CreateFont(PdfBaseFont.TIMES_ITALIC, PdfBaseFont.CP1252, PdfBaseFont.NOT_EMBEDDED);
                float       fontSize = 10, fontSizeHeader = 14;
                float       margin = 50;
                float       leftCursor = margin, topCursor = document.Top - margin, lineSpace = 13;
                bool        hasLogos = false;

                if (labLogo != null)
                {
                    CropImageToHeight(labLogo, 64f);
                    labLogo.SetAbsolutePosition(leftCursor, topCursor);
                    document.Add(labLogo);
                    hasLogos = true;
                }

                if (hasLogos)
                {
                    topCursor -= labLogo.ScaledHeight;
                }

                cb.SetFontAndSize(baseFontBold, fontSizeHeader);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "OPPDRAGSOVERSIKT", leftCursor, topCursor, 0);
                cb.SetFontAndSize(baseFont, fontSize);
                topCursor -= lineSpace * 2;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Oppdrag: " + OrderName, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                string cust = CustomerName;
                if (!String.IsNullOrEmpty(CustomerCompany))
                {
                    cust += ", " + CustomerCompany;
                }
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Oppdragsgiver: " + cust, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, CustomerAddress, leftCursor, topCursor, 0);
                topCursor -= lineSpace;
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Laboratorium/Kontaktperson: " + LaboratoryName + " / " + ResponsibleName, leftCursor, topCursor, 0);
                topCursor -= lineSpace * 4;
                cb.SetFontAndSize(baseFontBold, fontSizeHeader);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Måleresultater", leftCursor, topCursor, 0);
                cb.EndText();

                topCursor -= lineSpace;

                PdfPTable table = new PdfPTable(13);
                table.TotalWidth = document.GetRight(margin) - document.GetLeft(margin);

                PdfPCell cell = new PdfPCell(GetHeaderPhrase("Analysis"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Sample Type"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("P.Status"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("A.Status"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Method"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Nuclide"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Activity"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Act.Unc."));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Act.Appr."));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("MDA"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("MDA Appr."));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Reportable"));
                table.AddCell(cell);
                cell = new PdfPCell(GetHeaderPhrase("Accredited"));
                table.AddCell(cell);

                string query = @"
select 
    s.number as 'sample', 
    st.name as 'sample_type_name',
    sc.name as 'sample_component_name',
	p.number as 'preparation', 
    pm.name_short as 'preparation_method', 
    p.workflow_status_id as 'preparation_wfstatus', 
	a.number as 'analysis',     
    a.workflow_status_id as 'analysis_wfstatus', 
	am.name_short as 'analysis_method', 
	n.name as 'nuclide_name', 
	ar.activity as 'act', 
	ar.activity_uncertainty_abs as 'act.unc', 
	ar.activity_approved as 'act.appr', 
	ar.detection_limit as 'det.lim', 
	ar.detection_limit_approved as 'det.lim.appr', 
	ar.reportable, 
	ar.accredited
from sample s
    inner join sample_type st on st.id = s.sample_type_id
    left outer join sample_component sc on sc.id = s.sample_component_id
    inner join preparation p on p.sample_id = s.id and p.instance_status_id <= 1
    inner join preparation_method pm on pm.id = p.preparation_method_id
    inner join analysis a on a.preparation_id = p.id and a.instance_status_id <= 1 and a.assignment_id = @assignment_id
    inner join analysis_result ar on ar.analysis_id = a.id
    inner join analysis_method am on am.id = a.analysis_method_id
    inner join nuclide n on n.id = ar.nuclide_id
order by s.number, p.number, a.number
";
                int    nRows = 1;
                using (SqlDataReader reader = DB.GetDataReader(conn, trans, query, CommandType.Text, new[] {
                    new SqlParameter("@assignment_id", assignmentId)
                }))
                {
                    while (reader.Read())
                    {
                        int    pwfstat  = reader.GetInt32("preparation_wfstatus");
                        string spwfstat = "Unknown";
                        switch (pwfstat)
                        {
                        case WorkflowStatus.Construction:
                            spwfstat = "Construction";
                            break;

                        case WorkflowStatus.Complete:
                            spwfstat = "Complete";
                            break;

                        case WorkflowStatus.Rejected:
                            spwfstat = "Rejected";
                            break;
                        }

                        int    awfstat  = reader.GetInt32("analysis_wfstatus");
                        string sawfstat = "Unknown";
                        switch (awfstat)
                        {
                        case WorkflowStatus.Construction:
                            sawfstat = "Construction";
                            break;

                        case WorkflowStatus.Complete:
                            sawfstat = "Complete";
                            break;

                        case WorkflowStatus.Rejected:
                            sawfstat = "Rejected";
                            break;
                        }

                        string sact = "";
                        if (DB.IsValidField(reader["act"]))
                        {
                            sact = reader.GetDouble("act").ToString(Utils.ScientificFormat);
                        }

                        string sactunc = "";
                        if (DB.IsValidField(reader["act.unc"]))
                        {
                            sactunc = reader.GetDouble("act.unc").ToString(Utils.ScientificFormat);
                        }

                        string sdetlim = "";
                        if (DB.IsValidField(reader["det.lim"]))
                        {
                            sdetlim = reader.GetDouble("det.lim").ToString(Utils.ScientificFormat);
                        }

                        cell = new PdfPCell(GetCellPhrase(reader.GetString("sample") + "/" + reader.GetString("preparation") + "/" + reader.GetString("analysis")));
                        table.AddCell(cell);
                        string sampleType = reader.GetString("sample_type_name");
                        if (DB.IsValidField(reader["sample_component_name"]))
                        {
                            sampleType += " / " + reader.GetString("sample_component_name");
                        }
                        cell = new PdfPCell(GetCellPhrase(sampleType));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(spwfstat));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(sawfstat));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("preparation_method") + " / " + reader.GetString("analysis_method")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("nuclide_name")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(sact));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(sactunc));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("act.appr")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(sdetlim));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("det.lim.appr")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("reportable")));
                        table.AddCell(cell);
                        cell = new PdfPCell(GetCellPhrase(reader.GetString("accredited")));
                        table.AddCell(cell);
                        nRows++;
                    }
                }

                float currHeight = topCursor;
                int   currRow = 0, pageRows = 0;

                for (int i = 0; i < nRows; i++)
                {
                    currHeight -= table.GetRowHeight(i);
                    pageRows++;

                    if (currHeight <= document.GetBottom(10f))
                    {
                        table.WriteSelectedRows(currRow, currRow + pageRows, document.GetLeft(10), topCursor, cb);
                        document.NewPage();

                        currRow   += pageRows;
                        currHeight = topCursor = document.GetTop(10f);
                        pageRows   = 0;
                    }
                }

                if (pageRows > 0)
                {
                    table.WriteSelectedRows(currRow, currRow + pageRows, document.GetLeft(10), topCursor, cb);
                    document.NewPage();
                }
            }
            finally
            {
                document?.Close();
                if (ms != null)
                {
                    pdfData = ms.GetBuffer();
                }
            }

            return(pdfData);
        }