Пример #1
0
        public static void Pdf(string filepath, string printData)
        {
            //simple file write check

            //using (System.IO.StreamWriter file =
            //new System.IO.StreamWriter(pdfFilePath, true))
            //{
            //    file.WriteLine("Fourth line");
            //}


            var author = "Dummy author";
            var stream = new FileStream(filepath, FileMode.Create);

            // step 1
            var document = new iTextSharp.text.Document();

            // step 2
            PdfWriter.GetInstance(document, stream);
            // step 3
            document.AddAuthor(author);
            document.Open();
            // step 4
            document.Add(new iTextSharp.text.Paragraph(printData));

            document.Close();
            stream.Dispose();
        }
Пример #2
0
        private HttpResponseMessage GetPdfResponseForDictionary(Dictionary dictionary)
        {
            iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4);
            var memoryStream = new MemoryStream();

            iTextSharp.text.pdf.PdfWriter.GetInstance(document, memoryStream);

            document.Open();
            string ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");

            iTextSharp.text.pdf.BaseFont baseFont = iTextSharp.text.pdf.BaseFont.CreateFont(
                ARIALUNI_TFF, iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 14, iTextSharp.text.Font.NORMAL);

            uint rowNumber = 1;

            foreach (var phrasesPair in dictionary.PhrasesPairs.OrderBy(p => p.FirstPhrase.Text))
            {
                document.Add(new iTextSharp.text.Paragraph(
                                 string.Format("{0}) {1} - {2}", rowNumber++, phrasesPair.FirstPhrase.Text, phrasesPair.SecondPhrase.Text),
                                 font));
            }
            document.NewPage();
            rowNumber = 1;
            foreach (var phrasesPair in dictionary.PhrasesPairs.OrderBy(p => p.SecondPhrase.Text))
            {
                document.Add(new iTextSharp.text.Paragraph(
                                 string.Format("{0}) {1} - {2}", rowNumber++, phrasesPair.SecondPhrase.Text, phrasesPair.FirstPhrase.Text),
                                 font));
            }
            document.AddAuthor(dictionary.OwnerId);
            document.Close();

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new ByteArrayContent(memoryStream.ToArray());
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            string filename = string.Format("{0}.pdf", dictionary.Name);

            response.Content.Headers.ContentDisposition.FileName = Uri.EscapeUriString(filename);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            return(response);
        }
Пример #3
0
        public void MergePdf(string[] pdfFiles, string outputPath)
        {
            int    pdfCount = 0;
            int    f        = 0;
            string filename = String.Empty;

            iTextSharp.text.pdf.PdfReader reader = null;
            int pageCount = 0;

            iTextSharp.text.Document            pdfDoc = null;
            iTextSharp.text.pdf.PdfWriter       writer = null;
            iTextSharp.text.pdf.PdfContentByte  cb     = null;
            iTextSharp.text.pdf.PdfImportedPage page   = null;
            int rotation = 0;

            iTextSharp.text.Font bookmarkFont = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 4, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.RED);

            try
            {
                pdfCount = pdfFiles.Length;
                if (pdfCount > 1)
                {
                    filename  = pdfFiles[f];
                    reader    = new iTextSharp.text.pdf.PdfReader(filename);
                    pageCount = reader.NumberOfPages;
                    pdfDoc    = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18);
                    writer    = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, new System.IO.FileStream(outputPath, System.IO.FileMode.Create));
                    pdfDoc.AddAuthor("OPGK w Lublinie sp. z o. o. Sławomir Aleksak");
                    pdfDoc.AddCreator("Konwerter");
                    pdfDoc.Open();
                    cb = writer.DirectContent;
                    while (f < pdfCount)
                    {
                        var i = 0;
                        while (i < pageCount)
                        {
                            i += 1;
                            pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i));
                            pdfDoc.NewPage();
                            if (i == 1)
                            {
                                iTextSharp.text.Paragraph para   = new iTextSharp.text.Paragraph(System.IO.Path.GetFileName(filename).ToUpper(), bookmarkFont);
                                iTextSharp.text.Chapter   chpter = new iTextSharp.text.Chapter(para, f + 1);
                                pdfDoc.Add(chpter);
                            }
                            page     = writer.GetImportedPage(reader, i);
                            rotation = reader.GetPageRotation(i);
                            if (rotation == 90)
                            {
                                cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                            }
                            if (rotation == 270)
                            {
                                cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30);
                            }
                            else
                            {
                                cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
                            }
                        }
                        f += 1;
                        if (f < pdfCount)
                        {
                            filename  = pdfFiles[f];
                            reader    = new iTextSharp.text.pdf.PdfReader(filename);
                            pageCount = reader.NumberOfPages;
                        }
                    }
                    pdfDoc.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void Exportchart(ArrayList chart)
        {
            string uid = Session["UserID"].ToString();
            MemoryStream msReport = new MemoryStream();

            try
            {
                document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 72, 72, 82, 72);
                iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, msReport);
                document.AddAuthor("Test");
                document.AddSubject("Export to PDF");
                document.Open();

                for (int i = 0; i < chart.Count; i++)
                {
                    iTextSharp.text.Chunk c = new iTextSharp.text.Chunk("Export chart to PDF", iTextSharp.text.FontFactory.GetFont("VERDANA", 15));
                    iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph();
                    p.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                    iTextSharp.text.Image hImage = null;
                    hImage = iTextSharp.text.Image.GetInstance(MapPath(chart[i].ToString()));

                    float NewWidth = 500;
                    float MaxHeight = 400;

                    if (hImage.Width <= NewWidth) { NewWidth = hImage.Width; } float NewHeight = hImage.Height * NewWidth / hImage.Width; if (NewHeight > MaxHeight)
                    {
                        NewWidth = hImage.Width * MaxHeight / hImage.Height;
                        NewHeight = MaxHeight;
                    }

                    float ratio = hImage.Width / hImage.Height;
                    hImage.ScaleAbsolute(NewWidth, NewHeight);
                    document.Add(p);
                    document.Add(hImage);
                }
                // close it
                document.Close();
                string filename = "Appraisal Overview for " + Session["Name"].ToString() + ".pdf";
                Response.AddHeader("Content-type", "application/pdf");
                Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                Response.OutputStream.Write(msReport.GetBuffer(), 0, msReport.GetBuffer().Length);

            }
            catch (System.Threading.ThreadAbortException ex)
            {
                throw new Exception("Error occured: " + ex);
            }
        }
Пример #5
0
        // Background worker
        public void PDFBackgroundWorker_DoWork_TextPDF(object sender, DoWorkEventArgs e)
        {
            int i = 0;
            int ii = 1;
            exportFilePath = string.Empty;

                if (JobType == PDFJobType.TextPDF)
                {
                    exportFilePath = directoryName + "\\" + FileNoExt + ".pdf";

                    while (System.IO.File.Exists(exportFilePath))
                    {
                        exportFilePath = directoryName + "\\" + FileNoExt + "_" + ii + ".pdf";
                        ii++;
                    }

                    using (FileStream fs = new FileStream(exportFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.B4);

                        // iTextSharp.text. Document doc = new Document(PageSize.A2);
                        // doc.SetMargins(100, 200, 0, 0);
                        //Document doc = new Document(PageSize.A5, 36, 72, 108, 180);
                        //Document doc = new Document(PageSize.A3.Rotate(),400,0,0,0);
                        //var doc = new Document(new iTextSharp.text.Rectangle(100f, 300f));
                        iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, fs);
                        iTextSharp.text.pdf.BaseFont bfTimes = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.TIMES_BOLD, iTextSharp.text.pdf.BaseFont.CP1252, false);
                        // doc.SetPageSize(PageSize.A1);
                        // doc.SetMargins(76, 0, 0, 0);
                        doc.SetMarginMirroring(false);
                        iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 14, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.CYAN);

                        if (PDFBackgroundWorker.CancellationPending)
                        {
                            e.Cancel = true;
                            PDFBackgroundWorker.ReportProgress(i);
                            return;
                        }

                        using (StreamReader read = new StreamReader(InputFilename))
                        {

                            string text = read.ReadToEnd();
                            // AddDocMetaData();
                            doc.AddTitle(GameTitle);
                            doc.AddSubject("");
                            doc.AddKeywords(GameTitle + ",  " + FileNoExt + " : by Hypermint,  ");
                            doc.AddCreator("HLM-Chk");
                            doc.AddAuthor(GameTitle);
                            doc.AddHeader(FileNoExt, GameTitle);
                            doc.Open();
                            iTextSharp.text.Paragraph paragraph = new iTextSharp.text.Paragraph();
                            paragraph.Alignment = iTextSharp.text.Element.ALIGN_MIDDLE;
                            paragraph.Add(text);
                            doc.Add(paragraph);

                            //Paragraph paragraph = new Paragraph(text);
                            doc.Close();
                        }

                    }
                }
        }
Пример #6
0
        public Boolean MergePdfFiles(String[] pdfFiles, String outputPath)
        {
            //                                     Optional ByVal authorName As String = "", _
            //                                     Optional ByVal creatorName As String = "", _
            //                                     Optional ByVal subject As String = "", _
            //                                     Optional ByVal title As String = "", _
            //                                     Optional ByVal keywords As String = "")
            Boolean result   = false;
            int     pdfCount = 0;                        //'total input pdf file count
            int     f        = 0;                        //'pointer to current input pdf file
            string  fileName = String.Empty;             // 'current input pdf filename

            iTextSharp.text.pdf.PdfReader reader = null;
            int pageCount = 0;                                //'cureent input pdf page count

            iTextSharp.text.Document           pdfDoc = null; //   'the output pdf document
            iTextSharp.text.pdf.PdfWriter      writer = null;
            iTextSharp.text.pdf.PdfContentByte cb     = null;
            //    'Declare a variable to hold the imported pages
            iTextSharp.text.pdf.PdfImportedPage page = null;
            int  rotation         = 0;
            bool unethicalreading = false;

            //    'Declare a font to used for the bookmarks
            iTextSharp.text.Font bookmarkFont = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLUE);

            // Try
            pdfCount = pdfFiles.Length;
            if (pdfCount > 1)
            {
                //            'Open the 1st pad using PdfReader object
                fileName = pdfFiles[f];
                reader   = new iTextSharp.text.pdf.PdfReader(fileName);
                // reader.
                iTextSharp.text.pdf.PdfReader.unethicalreading = unethicalreading;
                //            'Get page count
                pageCount = reader.NumberOfPages;

                //            'Instantiate an new instance of pdf document and set its margins. This will be the output pdf.
                //            'NOTE: bookmarks will be added at the 1st page of very original pdf file using its filename. The location
                //            'of this bookmark will be placed at the upper left hand corner of the document. So you'll need to adjust
                //            'the margin left and margin top values such that the bookmark won't overlay on the merged pdf page. The
                //            'unit used is "points" (72 points = 1 inch), thus in this example, the bookmarks' location is at 1/4 inch from
                //            'left and 1/4 inch from top of the page.

                pdfDoc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18);
                //            'Instantiate a PdfWriter that listens to the pdf document
                writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, new System.IO.FileStream(outputPath, System.IO.FileMode.Create));    //added system
                //            'Set metadata and open the document
                // With pdfDoc
                pdfDoc.AddAuthor("");
                pdfDoc.AddCreationDate();
                pdfDoc.AddCreator("");
                pdfDoc.AddProducer();
                pdfDoc.AddSubject("");
                pdfDoc.AddTitle("");
                pdfDoc.AddKeywords("");
                pdfDoc.Open();
                //  End With
                //            'Instantiate a PdfContentByte object
                cb = writer.DirectContent;
                //            'Now loop thru the input pdfs
                while (f < pdfCount)
                {
                    //                'Declare a page counter variable
                    int i = 0;
                    //                'Loop thru the current input pdf's pages starting at page 1
                    while (i < pageCount)
                    {
                        i += 1;
                        //                    'Get the input page size
                        pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i));
                        //                    'Create a new page on the output document
                        pdfDoc.NewPage();

                        //                    'If it is the 1st page, we add bookmarks to the page
                        //                    'If i = 1 Then
                        //                    '    'First create a paragraph using the filename as the heading
                        //                    '    Dim para As New iTextSharp.text.Paragraph(IO.Path.GetFileName(fileName).ToUpper(), bookmarkFont)
                        //                    '    'Then create a chapter from the above paragraph
                        //                    '    Dim chpter As New iTextSharp.text.Chapter(para, f + 1)
                        //                    '    'Finally add the chapter to the document
                        //                    '    pdfDoc.Add(chpter)
                        //                    'End If

                        //                    'Now we get the imported page


                        page = writer.GetImportedPage(reader, i);
                        //                    'Read the imported page's rotation
                        rotation = reader.GetPageRotation(i);
                        //                    'Then add the imported page to the PdfContentByte object as a template based on the page's rotation
                        if (rotation == 90)
                        {
                            cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                        }
                        else
                        if (rotation == 270)
                        {
                            cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30);
                        }
                        else
                        {
                            cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
                        }
                    } //End While 2

                    //                'Increment f and read the next input pdf file
                    f += 1;
                    if (f < pdfCount)
                    {
                        fileName  = pdfFiles[f];
                        reader    = new iTextSharp.text.pdf.PdfReader(fileName);
                        pageCount = reader.NumberOfPages;
                    } //   End If
                }        //end while 1
                //            'When all done, we close the document so that the pdfwriter object can write it to the output file
                pdfDoc.Close();
                result = true;
            }//        End If Principal
             //    Catch ex As Exception
             //        Throw New Exception(ex.Message)
             //    End Try

            return(result);
        }
Пример #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("GVD.dat extractor for \"Ni no kuni\"\t-> by pleonex\n");

            #region Get arguments
            if (args.Length < 3)
            {
                Show_Help();
                return;
            }

            for (int i = 0; i < args.Length - 2; i++)
            {
                switch (args[i])
                {
                // Modes
                case "-x": mode = 0; break;

                case "-en":
                    if (i + 1 == args.Length)
                    {
                        Show_Help();
                        return;
                    }
                    mode = 1;

                    // Get number of pages to extract
                    string[] nums = args[++i].Split(',');
                    pages_num = new int[nums.Length];
                    for (int n = 0; n < nums.Length; n++)
                    {
                        try { pages_num[n] = Convert.ToInt32(nums[n]); }
                        catch { Show_Help(); return; }
                    }
                    break;

                case "-es":
                    mode       = 2;
                    pages_name = args[++i].Split(',');
                    break;

                // Options
                case "-bx": blocked = 0; break;

                case "-bb": blocked = 1; break;

                case "-bu": blocked = 2; break;

                case "-q":
                    try { quality = Convert.ToInt32(args[++i]); }
                    catch { Show_Help(); return; }
                    break;

                case "-pdf": convert = 1; break;

                case "-nofol": nofolder = true; break;

                default: Show_Help(); return;
                }
            }
            fileIn    = args[args.Length - 2];
            folderOut = args[args.Length - 1];
            if (!Directory.Exists(folderOut))
            {
                Directory.CreateDirectory(folderOut);
            }
            #endregion

            DateTime start = DateTime.Now;

            // Convert to PDF, initialize it
            if (convert == 1)
            {
                string pdf_file = Path.Combine(folderOut, Path.GetFileNameWithoutExtension(fileIn) + ".pdf");
                if (File.Exists(pdf_file))
                {
                    File.Delete(pdf_file);
                }
                pdf = new iTextSharp.text.Document();
                PdfWriter.GetInstance(pdf, new FileStream(pdf_file, FileMode.Create));
                pdf.Open();
                pdf.AddAuthor("Studio Ghibli - Level 5");
                pdf.AddTitle("Ni no kuni - Vademecum");
                pdf.AddSubject("Book extracted from the PS3 version of Ni no kuni.");
            }

            // WORK!
            Read_BookPack(fileIn, folderOut);

            if (convert == 1)
            {
                if (pdf.PageNumber == 0)
                {
                    pdf.NewPage();
                    pdf.Add(iTextSharp.text.Paragraph.GetInstance("No pages!"));
                }
                pdf.Close();
            }

            Console.WriteLine("\nFinished in {0}!", (DateTime.Now - start).ToString());
            Console.ReadKey(true);
        }