Пример #1
1
        // From <ClickButt> -> PdfConnector PdfCreatora <- PdfCreator(naemBank,Bodytext)
        // Form <Image> <- PdfConnector -- tworzyć pdf |
        public Form1() 
        {
            while (!fileBanksLists.EndOfStream)
            {
                    InitializeComponent();
            
                     using (FileStream file = new FileStream(String.Format(path, nameBank), FileMode.Create))
                    {

                        Document document = new Document(PageSize.A7);
                        PdfWriter writer = PdfWriter.GetInstance(document, file);
              
                        /// Create metadate pdf file
                        document.AddAuthor(nameBank);
                        document.AddLanguage("pl");
                        document.AddSubject("Payment transaction");
                        document.AddTitle("Transaction");
                        document.AddKeywords("OutcomingNumber :" + OutcomingNumber);
                        document.AddKeywords("IncomingNumber :" + IncomingNumber);
                        /// Create text in pdf file
                        document.Open();
                        document.Add(new Paragraph(_przelew + "\n"));
                        document.Add(new Paragraph(String.Format("Bank {0}: zaprasza\n", nameBank)));
                        document.Add(new Paragraph(DateTime.Now.ToString()));
                        document.Close();
                        writer.Close();
                        file.Close();
                    }
            }
            
            
        }
Пример #2
1
        public virtual void TestCreatePdfA_1() {
            String f1 = RESOURCES + "copy\\pdfa-1a.pdf";
            String testName = "testCreatePdfA_1.pdf";

            FileStream outputPdfStream = new FileStream(outputDir + testName, FileMode.Create);
            Document document = new Document();
            PdfACopy copy = new PdfACopy(document, outputPdfStream, PdfAConformanceLevel.PDF_A_1B);
            copy.CreateXmpMetadata();
            document.Open();
            document.AddLanguage("en-US");
            PdfReader reader = new PdfReader(f1);

            PdfImportedPage page = copy.GetImportedPage(reader, 1);
            PdfCopy.PageStamp stamp = copy.CreatePageStamp(page);
            Font font = FontFactory.GetFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 24);
            ColumnText.ShowTextAligned(stamp.GetUnderContent(), Element.ALIGN_CENTER, new Phrase("Hello world!", font), 100, 500, 0);
            stamp.AlterContents();
            copy.AddPage(page);

            FileStream iccProfileFileStream = File.Open(RESOURCES + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read,
                FileShare.Read);
            ICC_Profile icc = ICC_Profile.GetInstance(iccProfileFileStream);
            iccProfileFileStream.Close();

            copy.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
            copy.Close();
        }
Пример #3
0
        public bool TryCreatePdf(string nameBank)
        {
            try
            {
                using (FileStream file = new FileStream(String.Format(path,nameBank), FileMode.Create))
                {
                    Document document = new Document(PageSize.A7);
                    PdfWriter writer = PdfWriter.GetInstance(document, file);

                    /// Create metadate pdf file
                    document.AddAuthor("");
                    document.AddLanguage("pl");
                    document.AddSubject("Payment transaction");
                    document.AddTitle("Transaction");
                    /// Create text in pdf file
                    document.Open();
                    document.Add(new Paragraph(_przelew+"\n"));
                    document.Add(new Paragraph(String.Format("Bank {0}: zaprasza\n",nameBank)));
                    document.Add(new Paragraph(DateTime.Now.ToString()));
                    document.Close();
                    writer.Close();
                    file.Close();
                    return true;
                }
            }
            catch (SystemException ex)
            {
                return false;
            }
        }
Пример #4
0
        public virtual void TestMergeFields1() {
            String f1 = RESOURCES + "copy/pdfa-1a.pdf";
            String f2 = RESOURCES + "copy/pdfa-1a-2.pdf";
            String testName = "testMergeFields1.pdf";

            FileStream outputPdfStream = new FileStream(outputDir + testName, FileMode.Create);
            Document document = new Document();
            PdfACopy copy = new PdfACopy(document, outputPdfStream, PdfAConformanceLevel.PDF_A_1A);
            copy.SetMergeFields();
            copy.CreateXmpMetadata();
            copy.SetTagged();
            document.Open();
            document.AddLanguage("en-US");
            foreach (String f in new String[] {f1, f2}) {
                PdfReader reader = new PdfReader(f);
                copy.AddDocument(reader);
            }

            FileStream iccProfileFileStream = File.Open(RESOURCES + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read,
                FileShare.Read);
            ICC_Profile icc = ICC_Profile.GetInstance(iccProfileFileStream);
            iccProfileFileStream.Close();

            copy.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
            copy.Close();
        }
Пример #5
0
 protected void ManagerPDF_Click(object sender, EventArgs e)
 {
     Response.ContentType = "application/pdf";
     Response.AddHeader("content-disposition", "attachment;filename=LookupManagers.pdf");
     Response.Cache.SetCacheability(HttpCacheability.NoCache);
     iTextSharp.text.Document managerListDoc = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 10f, 10f);
     managerListDoc.AddTitle("List of Managers");
     managerListDoc.AddAuthor("Leban Mohamed");
     managerListDoc.AddLanguage("English");
     ExportPDF(ManagerGrid, managerListDoc);
 }
Пример #6
0
 protected void PDFOrganization_Click(object sender, EventArgs e)
 {
     Response.ContentType = "application/pdf";
     Response.AddHeader("content-disposition", "attachment;filename=LookupOrganization.pdf");
     Response.Cache.SetCacheability(HttpCacheability.NoCache);
     iTextSharp.text.Document orgListDoc = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 10f, 10f);
     orgListDoc.AddTitle("List of Registered Oranizations");
     orgListDoc.AddAuthor("Leban Mohamed");
     orgListDoc.AddLanguage("English");
     ExportPDF(OrganizationGrid, orgListDoc);
 }
Пример #7
0
        protected override void InitiateDocument(LnParameters lnParam, string language)
        {
            this.lnParameters    = lnParam;
            this.currentLanguage = language;

            pdf = new PdfDocument(PageSize.A4);
            foreach (string author in lnParam.authors)
            {
                pdf.AddAuthor(author);
            }
            pdf.AddCreationDate();
            pdf.AddLanguage(language);
            pdf.AddTitle(DocumentTitle);
            pdf.AddCreator(Globale.PUBLISHER);
            pdfChapters = new List <Chapter>();
        }
Пример #8
0
        private void InitializeDocument(String name, char pdfVersion) {
            output = OUT + name + ".pdf";
            document = new Document();
            writer = PdfWriter.GetInstance(document, new FileStream(output, FileMode.Create));
            writer.PdfVersion = pdfVersion;
            writer.SetTagged();
            document.Open();

            //Required for PDF/UA
            writer.ViewerPreferences = PdfWriter.DisplayDocTitle;
            document.AddLanguage("en-US");
            document.AddTitle("Some title");
            writer.CreateXmpMetadata();
            Chunk c = new Chunk("Document Header", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));
            h1 = new Paragraph(c);
            h1.Role = PdfName.H1;
        }
Пример #9
0
        public virtual void CreateTaggedPdf24() {
            Document document = new Document(PageSize.LETTER);

            MemoryStream baos = new MemoryStream();

            PdfWriter writer = PdfWriter.GetInstance(document, baos);

            writer.ViewerPreferences = PdfWriter.DisplayDocTitle;

//set more document properties

            writer.PdfVersion = PdfWriter.VERSION_1_7;
            writer.SetTagged(PdfWriter.markInlineElementsOnly);
            PdfDictionary info = writer.Info;
            info.Put(PdfName.TITLE, new PdfString("Testing"));

            writer.CreateXmpMetadata();


// step 3

            document.Open();
            document.AddLanguage("en_US");
            document.SetAccessibleAttribute(PdfName.LANG, new PdfString("en_US"));

// step 4

            Paragraph p = new Paragraph("Paragraph testing testing");
            p.SetAccessibleAttribute(PdfName.ACTUALTEXT, new PdfString("Paragraph ALT Text"));
            p.SetAccessibleAttribute(PdfName.ALT, new PdfString("Paragraph ALT Text"));
            document.Add(p);


            Chunk ck = new Chunk("Span testing testing", FontFactory.GetFont(RESOURCES + @"..\FreeMonoBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 12));
            ck.SetAccessibleAttribute(PdfName.ACTUALTEXT, new PdfString("Span ALT Text"));
            ck.SetAccessibleAttribute(PdfName.ALT, new PdfString("Span ALT Text"));
            p = new Paragraph(ck);

            document.Add(p);

// step 5
            document.Close();

            FileStream fos = new FileStream("TaggedPdfTest/pdf/out24.pdf", FileMode.Create);

            byte[] buff = baos.ToArray();
            fos.Write(buff, 0, buff.Length);

            fos.Flush();

            fos.Close();
            CompareResults("24");
        }
Пример #10
0
        public void TestSmartCopyCreatePdfA_1() {
            String fileName = RESOURCES + "copy/pdfa-1a.pdf";
            String testName = "testSmartCopyPdfA_1.pdf";

            FileStream outputPdfStream = File.Create(outputDir + testName);
            Document document = new Document();
            PdfCopy copy = new PdfASmartCopy(document, outputPdfStream, PdfAConformanceLevel.PDF_A_1B);
            copy.CreateXmpMetadata();
            document.Open();
            document.AddLanguage("en-US");
            PdfReader reader = new PdfReader(fileName);
            PdfImportedPage page = copy.GetImportedPage(reader, 1);
            copy.AddPage(page);
            copy.Close();
        }
Пример #11
0
        public virtual void TestImportedPage2() {
            String f1 = RESOURCES + "copy/pdfa-1a.pdf";
            String f2 = RESOURCES + "copy/pdfa-1a-2.pdf";
            String f3 = RESOURCES + "copy/pdfa-2a.pdf";

            Stream outputPdfStream = new MemoryStream();
            Document document = new Document();
            PdfACopy copy = new PdfACopy(document, outputPdfStream, PdfAConformanceLevel.PDF_A_1A);
            copy.CreateXmpMetadata();
            copy.SetTagged();
            document.Open();
            document.AddLanguage("en-US");

            bool exceptionThrown = false;
            try {
                foreach (String f in new String[] {f1, f2, f3}) {
                    PdfReader reader = new PdfReader(f);
                    for (int i = 1; i <= reader.NumberOfPages; i++) {
                        copy.AddPage(copy.GetImportedPage(reader, i, true));
                    }
                }
            } catch (PdfAConformanceException e) {
                if (e.Message.Contains("Different PDF/A version"))
                    exceptionThrown = true;
            }

            if (!exceptionThrown)
                Assert.Fail("PdfAConformanceException should be thrown.");
        }
Пример #12
0
        public virtual void TestMergeFields4() {
            String f1 = RESOURCES + "copy/pdfa-1a.pdf";
            String f2 = RESOURCES + "copy/source16.pdf";

            Stream outputPdfStream = new MemoryStream();
            Document document = new Document();
            PdfACopy copy = new PdfACopy(document, outputPdfStream, PdfAConformanceLevel.PDF_A_1B);
            copy.SetMergeFields();
            copy.CreateXmpMetadata();
            copy.SetTagged();
            document.Open();
            document.AddLanguage("en-US");
            bool exceptionThrown = false;
            try {
                foreach (String f in new String[] {f1, f2}) {
                    PdfReader reader = new PdfReader(f);
                    copy.AddDocument(reader);
                }
            } catch (PdfAConformanceException e) {
                if (e.Message.Contains("Only PDF/A documents can be added in PdfACopy"))
                    exceptionThrown = true;
            }

            if (!exceptionThrown)
                Assert.Fail("PdfAConformanceException should be thrown.");
        }
Пример #13
0
        public static byte[] FromHtml(string html)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4);
                //PdfAWriter writer = PdfAWriter.GetInstance(document, ms, PdfAConformanceLevel.PDF_A_1B);
                PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
                writer.PdfVersion = PdfWriter.VERSION_1_7;
                writer.SetTagged();
                writer.ViewerPreferences = PdfWriter.DisplayDocTitle;
                writer.CloseStream = false;
                writer.InitialLeading = 12.5f;
                writer.CompressionLevel = PdfStream.BEST_COMPRESSION;
                //writer.SetFullCompression(); //Causa erro no padrão PDF_A_1*

                try
                {
                    document.AddLanguage("pt-BR");
                    document.AddCreationDate();
                    writer.CreateXmpMetadata();
                    document.Open();

                    //FontFactory.RegisteredFonts.Add("");
                    FontFactory.RegisterDirectories();
                    ICC_Profile icc = ICC_Profile.GetInstance((byte[])Properties.Resources.ResourceManager.GetObject("icc_profile"));
                    writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);

                    //MemoryStream cssStream = new MemoryStream(Encoding.UTF8.GetBytes("html, html * { font-family: Arial, Arial, Arial!important; color: black!important; }"));
                    MemoryStream cssStream = new MemoryStream(Encoding.UTF8.GetBytes("body { font-family: \"Courier New\", Courier, monospace!important; color: black!important; }"));
                    MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html));
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlStream, cssStream);
                    document.Close();
                    writer.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                return memoryStream.ToArray();
            }
        }
Пример #14
0
        public virtual void CreateTagedPdf25() {
            Document document = new Document();
            MemoryStream baos = new MemoryStream();

            PdfWriter writer = PdfWriter.GetInstance(document, baos);

            writer.ViewerPreferences = PdfWriter.DisplayDocTitle;

            writer.PdfVersion = PdfWriter.VERSION_1_7;
            writer.SetTagged();
            PdfDictionary info = writer.Info;
            info.Put(PdfName.TITLE, new PdfString("Testing"));
            writer.CreateXmpMetadata();

            document.Open();
            document.AddLanguage("en_US");
            document.SetAccessibleAttribute(PdfName.LANG, new PdfString("en_US"));

            string longParagraphString = "Long teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeext Paragraph testing testing";
            BaseFont bFont = BaseFont.CreateFont(RESOURCES + @"..\FreeMonoBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
            Font font = new Font(bFont, 12);
            Paragraph p = new Paragraph(longParagraphString, font);
            document.Add(p);

            string longChunkString = "Long teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeext Span testing testing";
            Chunk ck = new Chunk(longChunkString, font);
            p = new Paragraph(ck);
            document.Add(p);

            document.Close();
            FileStream fos = new FileStream("TaggedPdfTest/pdf/out25.pdf", FileMode.Create);
            byte[] buff = baos.ToArray();
            fos.Write(buff, 0, buff.Length);
            fos.Flush();
            fos.Close();
            CompareResults("25");

            CompareTool compareTool = new CompareTool();
            string cmpFile = RESOURCES + @"out25.pdf";
            string errorMessage = compareTool.CompareByContent("TaggedPdfTest/pdf/out25.pdf", cmpFile,
                "TaggedPdfTest/pdf/", "diff_");
            if (errorMessage != null) {
                Assert.Fail(errorMessage);
            }

        }
Пример #15
0
        /**
         * Creates an accessible PDF with images and text.
         */
        public void createPdf(String dest)
        {
            Document document = new Document(PageSize.A4.Rotate());
            PdfWriter writer = PdfWriter.GetInstance(document, File.OpenRead(dest));
            writer.PdfVersion = PdfWriter.VERSION_1_7;
            //TAGGED PDF
            //Make document tagged
            writer.SetTagged();
            //===============
            //PDF/UA
            //Set document metadata
            writer.ViewerPreferences = PdfWriter.DisplayDocTitle;
            document.AddLanguage("en-US");
            document.AddTitle("English pangram");
            writer.CreateXmpMetadata();
            //=====================
            document.Open();

            Paragraph p = new Paragraph();
            //PDF/UA
            //Embed font
            Font font = FontFactory.GetFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 20);
            p.Font = font;
            //==================
            Chunk c = new Chunk("The quick brown ");
            p.Add(c);
            Image i = Image.GetInstance(FOX);
            c = new Chunk(i, 0, -24);
            //PDF/UA
            //Set alt text
            c.SetAccessibleAttribute(PdfName.ALT, new PdfString("Fox"));
            //==============
            p.Add(c);
            p.Add(new Chunk(" jumps over the lazy "));
            i = Image.GetInstance(DOG);
            c = new Chunk(i, 0, -24);
            //PDF/UA
            //Set alt text
            c.SetAccessibleAttribute(PdfName.ALT, new PdfString("Dog"));
            //==================
            p.Add(c);
            document.Add(p);

            p = new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n", font);
            document.Add(p);
            List list = new List(true);
            list.Add(new ListItem("quick", font));
            list.Add(new ListItem("brown", font));
            list.Add(new ListItem("fox", font));
            list.Add(new ListItem("jumps", font));
            list.Add(new ListItem("over", font));
            list.Add(new ListItem("the", font));
            list.Add(new ListItem("lazy", font));
            list.Add(new ListItem("dog", font));
            document.Add(list);
            document.Close();
        }
Пример #16
0
        private void SaveAsPDFA(ref string tempPDF, string pdfPath, MetaData md)
        {
            string tempdir = Path.Combine(Path.GetTempPath(), "GhostScriptWrapper");

            if (!Directory.Exists(tempdir))
            {
                Directory.CreateDirectory(tempdir);
            }

            tempPDF = Path.Combine(tempdir, string.Format("{0}_tmp.pdf", Guid.NewGuid()));

            File.Copy(pdfPath, tempPDF);

            GhostScriptWrapper.CallAPI(GetArgs(tempPDF, pdfPath));

            var document = new it.Document();

            using (var fs = new FileStream(tempPDF, FileMode.Create))
            {
                // step 2: we create a writer that listens to the document
                //PdfCopy writer = new PdfCopy(document, fs);
                var pdfaWriter = ip.PdfAWriter.GetInstance(document, fs, ip.PdfAConformanceLevel.PDF_A_1B);

                pdfaWriter.SetTagged();
                pdfaWriter.CreateXmpMetadata();
                // step 3: we open the document
                document.Open();

                document.AddAuthor(md.Author);
                document.AddCreator(md.Creator);
                document.AddLanguage(md.Language);
                document.AddProducer();
                document.AddTitle(Path.GetFileNameWithoutExtension(pdfPath));

                // we create a reader for a certain document
                var reader = new ip.PdfReader(pdfPath);
                reader.ConsolidateNamedDestinations();

                document.NewPage();

                var icc = ip.ICC_Profile.GetInstance(Environment.GetEnvironmentVariable("SystemRoot") + @"\System32\spool\drivers\color\sRGB Color Space Profile.icm");
                pdfaWriter.SetOutputIntents("sRGB", null, "http://www.color.org", "sRGB IEC61966-2.1", icc.Data);

                // step 4: we add content
                for (var i = 1; i <= reader.NumberOfPages; i++)
                {
                    var page = pdfaWriter.GetImportedPage(reader, i);
                    pdfaWriter.DirectContentUnder.AddTemplate(page, 0, 0);

                    document.NewPage();
                }

                // step 5: we close the document and writer

                document.AddCreationDate();
                pdfaWriter.Flush();

                try
                {
                    pdfaWriter.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                reader.Close();
                try
                {
                    document.Close();
                }
                catch
                {
                }
            }

            ManipulatePdf(tempPDF, pdfPath, md);
        }