Пример #1
0
        /// <summary>
        /// </summary>
        /// <param name="args">string xpsPath, string pdfPath, bool ToPdfA</param>
        /// <returns></returns>
        public int Convert(string[] args)
        {
            log.InfoFormat("Args: {0}", string.Join(",", args));

            var    result  = 0;
            string pdfPath = null;
            string tempPDF = null;

            try
            {
                if (args.Length < 1)
                {
                    Environment.Exit(-1);
                }

                if (args[0] == "merge")
                {
                    if (args.Length < 3)
                    {
                        Environment.Exit(-1);
                    }

                    pdfPath = args[1];
                    var inFiles = new List <string>();
                    for (int i = 2; i < args.Length; i++)
                    {
                        inFiles.Add(args[i]);
                    }

                    if (inFiles.Count == 0)
                    {
                        Environment.Exit(-1);
                    }
                    if (inFiles.Count == 1)
                    {
                        return(result);
                    }

                    GhostScriptWrapper.CallAPI(GetArgs(pdfPath, inFiles));
                }
                else
                {
                    pdfPath = args[0];

                    if (!File.Exists(pdfPath))
                    {
                        Environment.Exit(404);
                    }

                    var metadata = new MetaData();

                    if (args.Length >= 2)
                    {
                        metadata.Creator = args[1];
                    }

                    if (args.Length >= 3)
                    {
                        metadata.Author = args[2];
                    }

                    if (args.Length >= 4)
                    {
                        metadata.Language = args[3];
                    }

                    if (args.Length >= 5)
                    {
                        var path = args[4];
                        if (!string.IsNullOrWhiteSpace(path))
                        {
                            metadata.CustomMetadata = File.ReadAllBytes(args[4]);
                        }
                    }

                    SaveAsPDFA(ref tempPDF, pdfPath, metadata);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                if (result == 0)
                {
                    result = 500;
                }
            }
            finally
            {
                if (File.Exists(tempPDF))
                {
                    File.Delete(tempPDF);
                }
            }

            return(result);
        }
Пример #2
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);
        }