/// <summary>
 /// Creates a form XObject from
 /// <see cref="iText.Kernel.Pdf.Canvas.Wmf.WmfImageData"/>
 /// .
 /// Unlike other images,
 /// <see cref="iText.Kernel.Pdf.Canvas.Wmf.WmfImageData"/>
 /// images are represented as
 /// <see cref="PdfFormXObject"/>
 /// , not as
 /// <see cref="PdfImageXObject"/>
 /// .
 /// </summary>
 /// <param name="image">image to create form object from</param>
 /// <param name="pdfDocument">document instance which is needed for writing form stream contents</param>
 public PdfFormXObject(WmfImageData image, PdfDocument pdfDocument)
     : this(new WmfImageHelper(image).CreatePdfForm(pdfDocument).GetPdfObject())
 {
 }
Beispiel #2
0
        private static int RunInternal(Options options)
        {
            if (!File.Exists(options.InputFilePath))
            {
                Console.WriteLine("Input file \"{0}\" doesn't exist.", options.InputFilePath);
                return(1);
            }

            if (string.IsNullOrWhiteSpace(options.OutputFilePath))
            {
                options.OutputFilePath = Path.ChangeExtension(options.InputFilePath, ".pdf");
            }
            else if (Directory.Exists(options.OutputFilePath))
            {
                var intputFileNameWithoutExtension = Path.GetFileNameWithoutExtension(options.InputFilePath);
                options.OutputFilePath = Path.Combine(options.OutputFilePath, intputFileNameWithoutExtension + ".pdf");
            }
            else
            {
                var directoryName = Path.GetDirectoryName(options.OutputFilePath);
                var fileName      = Path.GetFileName(options.OutputFilePath);

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

                if (string.IsNullOrEmpty(fileName))
                {
                    var intputFileNameWithoutExtension = Path.GetFileNameWithoutExtension(options.InputFilePath);
                    options.OutputFilePath = Path.Combine(directoryName, intputFileNameWithoutExtension + ".pdf");
                }
            }

            var metafile = new WmfImageData(options.InputFilePath);

            FileStream outFileStream;

            try
            {
                outFileStream = new FileStream(options.OutputFilePath, FileMode.CreateNew);
            }
            catch (IOException ex)
            {
                Console.WriteLine("Failed to create PDF document \"{0}\":\n{1}.",
                                  options.OutputFilePath, ex);
                return(1);
            }

            var pdfWriter = new PdfWriter(outFileStream);

            var pdfDocument = new PdfDocument(pdfWriter);
            var document    = new Document(pdfDocument);

            var image = new Image(metafile);

            document.Add(image);

            document.Close();

            return(0);
        }