Exemplo n.º 1
0
        public void Merge(string[] args)
        {
            if (args.Length != 2 || (args.Length > 1 && args[0] != "-f"))
            {
                Console.WriteLine("-f [xml filename]");
                Console.WriteLine();
                Console.WriteLine("XML File looks like:");
                Console.WriteLine("<pdfmerge>");
                Console.WriteLine("   <file path='filename1' nickname='nickname1'/>");
                Console.WriteLine("   <file path='filename2' nickname='nickname2'/>");
                Console.WriteLine("   <output path='filename' allowoverwrite='true or false'/>");
                Console.WriteLine("   <page source='nickname1' pageno='page #'/>");
                Console.WriteLine("   <page source='nickname2' pageno='page #'/>");
                Console.WriteLine("</pdfmerge>");
            }
            else
            {
                //LoadPdfSharpDll();
                string filename = args[1];
                if (!File.Exists(filename))
                {
                    Console.WriteLine("XML file not found: " + filename);
                }
                else
                {
                    List <SourcePdf> sources        = new List <SourcePdf>();
                    List <Page>      pages          = new List <Page>();
                    string           outputPath     = null;
                    bool             allowoverwrite = false;
                    try
                    {
                        using (FileStream s = new FileStream(filename, FileMode.Open))
                        {
                            using (XmlReader reader = XmlReader.Create(s))
                            {
                                _xmlInfo = (IXmlLineInfo)reader;
                                while (reader.Read())
                                {
                                    if (reader.NodeType == XmlNodeType.Element)
                                    {
                                        if (reader.Name == FILE)
                                        {
                                            string path     = ReadPath(reader, true);
                                            string nickname = ReadNickname(reader);
                                            sources.Add(new SourcePdf(path, nickname));
                                        }
                                        else if (reader.Name == OUTPUT)
                                        {
                                            string path            = ReadPath(reader, false);
                                            string allowoverwriteS = reader.GetAttribute(ALLOWOVERWRITE);
                                            allowoverwrite = (allowoverwriteS != null && "true".Equals(allowoverwriteS.ToLower()));
                                            if (!allowoverwrite && File.Exists(path))
                                            {
                                                Console.WriteLine("Output file already exists: " + path + " on line " + _xmlInfo.LineNumber);
                                                return;
                                            }
                                            outputPath = path;
                                        }
                                        else if (reader.Name == PAGE)
                                        {
                                            SourcePdf source = ReadSource(reader, sources);
                                            int       pageNo = ReadPageNo(reader);
                                            pages.Add(new Page(source, pageNo));
                                        }
                                    }
                                    else if (reader.NodeType == XmlNodeType.EndElement)
                                    {
                                        if (reader.Name == PDFMERGE)
                                        {
                                            if (sources.Count == 0)
                                            {
                                                throw new Exception("No file (source) elements were specified.");
                                            }
                                            if (outputPath == null)
                                            {
                                                throw new Exception("No output element was specified.");
                                            }
                                            if (pages.Count == 0)
                                            {
                                                throw new Exception("No page elements were specified.");
                                            }

                                            using (PdfDocument outPdf = new PdfDocument())
                                            {
                                                foreach (Page p in pages)
                                                {
                                                    CopyPages(p, outPdf);
                                                }
                                                if (allowoverwrite && File.Exists(PATH))
                                                {
                                                    FileInfo fi = new FileInfo(outputPath);
                                                    fi.Delete();
                                                    fi.Refresh();
                                                    int ct = 0;
                                                    while (fi.Exists && ct++ < 100)
                                                    {
                                                        System.Threading.Thread.Sleep(100);
                                                        fi.Refresh();
                                                    }
                                                    if (fi.Exists)
                                                    {
                                                        throw new Exception("Could not overwrite file '" + outputPath + "'.");
                                                    }
                                                }
                                                outPdf.Save(outputPath);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: " + ex.Message);
                    }
                }
            }
        }
Exemplo n.º 2
0
 public Page(SourcePdf source, int pageNo = -1)
 {
     _source = source;
     _pageNo = pageNo;
 }