Exemple #1
0
        public void Verify_PrintPreferencesExample_CanBeCreated()
        {
            var pdfFilePath = TestUtils.GetOutputFileName();
            var stream      = new FileStream(pdfFilePath, FileMode.Create);

            var document = new Document();

            // step 2
            PdfWriter writer = PdfWriter.GetInstance(document, stream);

            writer.PdfVersion = PdfWriter.VERSION_1_5;
            writer.AddViewerPreference(PdfName.Printscaling, PdfName.None);
            writer.AddViewerPreference(PdfName.Numcopies, new PdfNumber(3));
            writer.AddViewerPreference(PdfName.Picktraybypdfsize, PdfBoolean.Pdftrue);
            // step 3
            document.AddAuthor(TestUtils.Author);
            document.Open();
            // step 4
            document.Add(new Paragraph("Hello World!"));

            document.Close();
            stream.Dispose();

            TestUtils.VerifyPdfFileIsReadable(pdfFilePath);
        }
Exemple #2
0
// ===========================================================================
        public void Write(Stream stream)
        {
            // step 1
            using (Document document = new Document()) {
                // step 2
                PdfWriter writer = PdfWriter.GetInstance(document, stream);
                writer.PdfVersion = PdfWriter.VERSION_1_5;
                writer.AddViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
                writer.AddViewerPreference(PdfName.NUMCOPIES, new PdfNumber(3));
                writer.AddViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE);
                // step 3
                document.Open();
                // step 4
                document.Add(new Paragraph("Hello World!"));
            }
        }
Exemple #3
0
        public Example7()
        {
            string appRootDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;

            try
            {
                // Creating System.IO.FileStream object
                using (FileStream fs = new FileStream(appRootDir + "/PDFs/" + "Chapter1_Example7.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
                    // Creating iTextSharp.text.Document object
                    using (Document doc = new Document())
                        // Creating iTextSharp.text.pdf.PdfWriter object
                        // It helps to write the Document to the Specified FileStream
                        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                        {
                            // Setting Viewer Preferences by calling method
                            writer.AddViewerPreference(PdfName.HIDEMENUBAR, new PdfBoolean(true));

                            // Setting View Preferences by setting properties
                            writer.ViewerPreferences = PdfWriter.HideMenubar;

                            // Openning the Document
                            doc.Open();

                            // Adding a paragraph
                            // NOTE: When we want to insert text, then we've to do it through creating paragraph
                            doc.NewPage();
                            doc.Add(new Paragraph("First Page"));
                            doc.NewPage();
                            doc.Add(new Paragraph("Second Page"));

                            // Closing the Document
                            doc.Close();
                        }
            }
            // Catching iTextSharp.text.DocumentException if any
            catch (DocumentException de)
            {
                throw de;
            }
            // Catching System.IO.IOException if any
            catch (IOException ioe)
            {
                throw ioe;
            }
        }
        private void applyPrintingPreferences()
        {
            var printingPreferences = PageSetup.PrintingPreferences;

            if (printingPreferences == null)
            {
                return;
            }

            if (printingPreferences.PrintScaling == PrintScaling.None)
            {
                PdfWriter.AddViewerPreference(PdfName.Printscaling, PdfName.None);
            }

            if (printingPreferences.PrintScaling == PrintScaling.Default)
            {
                PdfWriter.AddViewerPreference(PdfName.Printscaling, PdfName.Appdefault);
            }

            if (printingPreferences.PrintSide == PrintSide.Simplex)
            {
                PdfWriter.AddViewerPreference(PdfName.Duplex, PdfName.Simplex);
            }

            if (printingPreferences.PrintSide == PrintSide.DuplexFlipShortEdge)
            {
                PdfWriter.AddViewerPreference(PdfName.Duplex, PdfName.Duplexflipshortedge);
            }

            if (printingPreferences.PrintSide == PrintSide.DuplexFlipLongEdge)
            {
                PdfWriter.AddViewerPreference(PdfName.Duplex, PdfName.Duplexfliplongedge);
            }

            if (printingPreferences.PickTrayByPdfSize)
            {
                PdfWriter.AddViewerPreference(PdfName.Picktraybypdfsize, PdfBoolean.Pdftrue);
            }

            if (printingPreferences.NumberOfCopies >= 2 && printingPreferences.NumberOfCopies <= 5)
            {
                PdfWriter.AddViewerPreference(PdfName.Numcopies, new PdfNumber(printingPreferences.NumberOfCopies));
            }
        }
        private void applyPrintingPreferences()
        {
            var printingPreferences = PageSetup.PrintingPreferences;

            if (printingPreferences == null)
            {
                return;
            }

            if (printingPreferences.PrintScaling == PrintScaling.None)
            {
                PdfWriter.AddViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
            }

            if (printingPreferences.PrintScaling == PrintScaling.Default)
            {
                PdfWriter.AddViewerPreference(PdfName.PRINTSCALING, PdfName.APPDEFAULT);
            }

            if (printingPreferences.PrintSide == PrintSide.Simplex)
            {
                PdfWriter.AddViewerPreference(PdfName.DUPLEX, PdfName.SIMPLEX);
            }

            if (printingPreferences.PrintSide == PrintSide.DuplexFlipShortEdge)
            {
                PdfWriter.AddViewerPreference(PdfName.DUPLEX, PdfName.DUPLEXFLIPSHORTEDGE);
            }

            if (printingPreferences.PrintSide == PrintSide.DuplexFlipLongEdge)
            {
                PdfWriter.AddViewerPreference(PdfName.DUPLEX, PdfName.DUPLEXFLIPLONGEDGE);
            }

            if (printingPreferences.PickTrayByPdfSize)
            {
                PdfWriter.AddViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE);
            }

            if (printingPreferences.NumberOfCopies >= 2 && printingPreferences.NumberOfCopies <= 5)
            {
                PdfWriter.AddViewerPreference(PdfName.NUMCOPIES, new PdfNumber(printingPreferences.NumberOfCopies));
            }
        }
Exemple #6
0
        private void Button1_Click(object sender, EventArgs e)
        {
            // Abrimos el primer documento existente
            PdfReader reader = new PdfReader("portada.pdf");
            // Creamos el nuevo PDF
            MemoryStream m      = new MemoryStream();
            Document     pdf    = new Document(PageSize.LETTER);
            PdfWriter    writer = PdfWriter.GetInstance(pdf, m);                       // Instrucción para que el PDF imprima correctamente según el tamaño de papel seleccionado.

            writer.AddViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE); // Añadimos los atributos del nuevo PDF
            pdf.AddAuthor("Autor");
            pdf.AddTitle("Titulo del nuevo PDF");
            pdf.AddCreator("Creador del documento");
            pdf.AddCreationDate();

            // Abrimos el documento
            pdf.Open();
            // writer.PageEvent = new PdfFooter();

            PdfContentByte cb = writer.DirectContent;

            // Aquí declaramos el tipo de letra, tamaño y color que deseamos utilizar
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

            cb.SetColorFill(BaseColor.BLACK);
            cb.SetFontAndSize(bf, 12);

            // Creamos una nueva página e importamos el contenido del paso #1: portada.pdf
            PdfImportedPage page = writer.GetImportedPage(reader, 1);

            cb.AddTemplate(page, 0, 0);
            // Cerramos el nuevo archivo PDF
            //pdf.Close();
            writer.Close();// En el caso que querramos guardarlo en una carpeta
            byte[] bytArr = m.ToArray();
            using (FileStream fs = File.Create("carpetaYNombreDelNuevoPDF.pdf"))
                ;
        }
Exemple #7
0
        public SaveLog(DUTSample testSample, string logFileName)
        {
            string appRootDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;

            try
            {
                // Creating System.IO.FileStream object
                using (FileStream filestream = new FileStream(Properties.Settings.Default.savedLogFileLocation + "\\" + logFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                    // Creating iTextSharp.text.Document object
                    using (Document document = new Document(iTextSharp.text.PageSize.A4.Rotate(), 25, 25, 30, 30))

                        // Creating iTextSharp.text.pdf.PdfWriter object
                        // It helps to write the Document to the Specified FileStream
                        using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, filestream))
                        {
                            // Setting Viewer Preferences by calling method
                            pdfWriter.AddViewerPreference(PdfName.HIDEMENUBAR, new PdfBoolean(true));

                            // Setting View Preferences by setting properties
                            pdfWriter.ViewerPreferences = PdfWriter.HideMenubar;

                            document.AddAuthor("Author");
                            document.AddCreator("Creator");
                            document.AddKeywords("Keywords");
                            document.AddSubject("Document subject");
                            document.AddTitle("The document title");

                            // Openning the Document
                            document.Open();

                            PdfContentByte pdfContentByte = pdfWriter.DirectContent;
                            BaseFont       baseFont       = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

                            document.NewPage();

                            pdfContentByte.BeginText();
                            pdfContentByte.SetFontAndSize(baseFont, 15);
                            pdfContentByte.SetTextMatrix(380, 540);
                            pdfContentByte.ShowText("ES Lab Report");
                            pdfContentByte.EndText();

                            pdfContentByte.BeginText();
                            pdfContentByte.SetFontAndSize(baseFont, 24);
                            pdfContentByte.SetTextMatrix(230, 440);
                            pdfContentByte.ShowText("ES Lab Continuous Monitoring System Report");
                            pdfContentByte.EndText();

                            iTextSharp.text.Image imglogo = iTextSharp.text.Image.GetInstance(appRootDir + "/Resources/" + "logo.png");
                            imglogo.SetAbsolutePosition(40, 470);
                            imglogo.ScalePercent(65f);
                            pdfContentByte.AddImage(imglogo);

                            PdfPTable pdfTable = new PdfPTable(2);
                            PdfPCell  pdfCell  = new PdfPCell();
                            pdfCell.Colspan             = 2;
                            pdfCell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
                            pdfTable.SpacingBefore      = 50f;
                            pdfTable.SpacingAfter       = 30f;
                            pdfTable.TotalWidth         = 750f;
                            pdfTable.AddCell(pdfCell);
                            pdfTable.AddCell("Test ID");
                            pdfTable.AddCell(testSample.TestID);
                            pdfTable.AddCell("Test Name");
                            pdfTable.AddCell(testSample.TestType);
                            pdfTable.AddCell("Part Number");
                            pdfTable.AddCell(testSample.PartNumber);
                            pdfTable.AddCell("Customer");
                            pdfTable.AddCell(testSample.Customer);
                            pdfTable.AddCell("Channel No");
                            pdfTable.AddCell(testSample.ChNumber);
                            pdfTable.AddCell("Upper Test Limit (mA)");
                            pdfTable.AddCell(testSample.UpperTestLimit.ToString());
                            pdfTable.AddCell("Lower Test Limit (mA)");
                            pdfTable.AddCell(testSample.LowerTestLimit.ToString());
                            pdfTable.AddCell("Overall test result");
                            pdfTable.AddCell(testSample.TestResult);
                            pdfTable.AddCell("Start Time");
                            pdfTable.AddCell(Properties.Settings.Default.savedTestStartTime);
                            pdfTable.AddCell("End Time");
                            pdfTable.AddCell(Properties.Settings.Default.savedTestFinishedTime);
                            pdfTable.AddCell("Measurement instrument");
                            pdfTable.AddCell(Properties.Settings.Default.savedInstrumentName);
                            pdfTable.AddCell("Measurement instrument serial");
                            pdfTable.AddCell(Properties.Settings.Default.savedInstrumentSerial);
                            pdfTable.AddCell("Climatic chamber name");
                            pdfTable.AddCell(Properties.Settings.Default.savedChamberName);
                            pdfTable.AddCell("Climatic chamber serial");
                            pdfTable.AddCell(Properties.Settings.Default.savedChamberSerial);
                            pdfTable.AddCell("Climatic chamber program file path");
                            pdfTable.AddCell(Properties.Settings.Default.savedChamberProgramFilePath + Properties.Settings.Default.savedChamberProgramFileName);
                            pdfTable.AddCell("Climatic chamber archive file path");
                            pdfTable.AddCell(Properties.Settings.Default.savedChamberArchiveFilePath + Properties.Settings.Default.savedChamberArchiveFileName);

                            pdfTable.WriteSelectedRows(0, -1, 50, 400, pdfContentByte);

                            document.NewPage();

                            pdfContentByte.BeginText();
                            pdfContentByte.SetFontAndSize(baseFont, 15);
                            pdfContentByte.SetTextMatrix(380, 540);
                            pdfContentByte.ShowText("ES Lab Report");
                            pdfContentByte.EndText();

                            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Properties.Settings.Default.savedLogFileLocation + "\\" + "chart" + testSample.ChNumber + ".png");
                            img.SetAbsolutePosition(150, 100);
                            img.ScalePercent(85f);
                            pdfContentByte.AddImage(img);

                            // Closing the Document
                            document.Close();
                        }
            }
            // Catching iTextSharp.text.DocumentException if any
            catch (DocumentException de)
            {
                throw de;
            }
            // Catching System.IO.IOException if any
            catch (IOException ioe)
            {
                throw ioe;
            }
        }