public TextObject(PdfTextObject obj,
                   int absIdx)
 {
     StartIndex = absIdx;
     Length     = obj.CharsCount;
     Font       = obj.Font;
     FontSize   = obj.FontSize;
     FillColor  = obj.FillColor.ToColor();
 }
Exemplo n.º 2
0
        public void CreatePdf(string model, string starting_date, string ending_date)
        {
            saveFileDialog.Filter = "PDF|*.pdf";
            saveFileDialog.Title  = "Zapisz raport";
            saveFileDialog.ShowDialog();

            int pageIndex = 0;
            int charIndex = 0;
            int imgYPos   = 450;
            int textYPos  = 770;
            int imgCount  = 0;

            PdfCommon.Initialize();
            using (var doc = PdfDocument.CreateNew())
            {
                PdfFont       calibryBold = PdfFont.CreateFont(doc, "CalibriBold", true, 0, 0, 0, FontCharSet.ANSI_CHARSET, false);
                PdfTextObject textObject;
                if (DataTableEntityList.Select(x => x.part_number).Distinct().Count() == 1)
                {
                    List <string> part_number = DataTableEntityList.Select(x => x.part_number).Distinct().ToList();
                    textObject = PdfTextObject.Create("Model: " + model + "        Numer Czesci: " + part_number[0] + "        Data: " + starting_date + " - " + ending_date, 15, 810, calibryBold, 12);
                }
                else
                {
                    textObject = PdfTextObject.Create("Model: " + model + "           Data: " + starting_date + " - " + ending_date, 15, 810, calibryBold, 12);
                }
                textObject.FillColor = Color.Black;
                doc.Pages.InsertPageAt(0, 8.27f * 72, 11.69f * 72);
                doc.Pages[0].PageObjects.Add(textObject);

                foreach (Bitmap pic in picsForRaport)
                {
                    using (var image = pic)
                    {
                        using (PdfBitmap pdfBitmap = new PdfBitmap(image.Width, image.Height, true))
                        {
                            using (var g = Graphics.FromImage(pdfBitmap.Image))
                            {
                                g.DrawImage(image, 0, 0, image.Width, image.Height);
                            }
                            PdfTextObject characteristicTextObject = PdfTextObject.Create(Translator.TranslateColumnName(btnNamesForRaport[charIndex]), 15, textYPos, calibryBold, 12);
                            textYPos -= 350;
                            characteristicTextObject.FillColor = Color.Black;
                            charIndex++;
                            doc.Pages[pageIndex].PageObjects.Add(characteristicTextObject);

                            var imageObject = PdfImageObject.Create(doc, pdfBitmap, 0, 0);

                            imageObject.Matrix = new FS_MATRIX(500, 0, 0, 300, 50, imgYPos);
                            doc.Pages[pageIndex].PageObjects.Add(imageObject);
                            imgYPos -= 350;

                            doc.Pages[pageIndex].GenerateContent();
                            imgCount++;
                            if (imgCount == 2)
                            {
                                pageIndex++;
                                doc.Pages.InsertPageAt(pageIndex, 8.27f * 72, 11.69f * 72);
                                imgYPos  = 450;
                                textYPos = 770;
                                imgCount = 0;
                            }
                        }
                    }
                }
                if (imgCount == 0)
                {
                    doc.Pages.DeleteAt(doc.Pages.Count - 1);
                }
                if (saveFileDialog.FileName != "")
                {
                    doc.Save(saveFileDialog.FileName, SaveFlags.NoIncremental);
                    saveFileDialog.FileName = "";
                }
                //doc.Save(@"C:\Users\" + Environment.UserName + @"\Documents\sample_document.pdf", SaveFlags.NoIncremental);

                if (picsForRaport.Any())
                {
                    picsForRaport.Clear();
                }
                if (btnNamesForRaport.Any())
                {
                    btnNamesForRaport.Clear();
                }
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            for (;;)
            {
                System.Console.WriteLine();
                System.Console.WriteLine("select test:");
                System.Console.WriteLine("1. open and close doc text object");
                System.Console.WriteLine("2. open and close other doc text object");
                System.Console.WriteLine("3. Quit application and open doc text object");
                System.Console.WriteLine("4. attempt to reopen same doc ");
                System.Console.WriteLine("5. deserialize json extractionblock");
                System.Console.WriteLine("6. deserialize json extraction pipeline");
                System.Console.WriteLine("7. deserialize json contains check pipeline");
                System.Console.WriteLine("8. get text from docstringtextobject");
                System.Console.WriteLine("9. kill pdf process");
                System.Console.WriteLine("or type 'exit'");
                var selectedTest = System.Console.ReadLine();

                if (selectedTest == "1")
                {
                    //doc test scenario
                    //1. open and close doc text object
                    System.Console.WriteLine("set doctextobject");
                    System.Console.ReadKey();
                    DocTextObject docText1 = new DocTextObject($@"{Directory.GetCurrentDirectory()}\testdata\Test document.docx");

                    System.Console.WriteLine("docText1.open()");
                    System.Console.ReadKey();
                    docText1.open();

                    System.Console.WriteLine("docText1.close()");
                    System.Console.ReadKey();
                    docText1.close();

                    System.Console.WriteLine("test1 finished");
                }
                if (selectedTest == "2")
                {
                    //2. open and close other doc text object
                    System.Console.WriteLine("set doctextobject 1");
                    System.Console.ReadKey();
                    DocTextObject docText1 = new DocTextObject($@"{Directory.GetCurrentDirectory()}\testdata\Test document.docx");

                    System.Console.WriteLine("set doctextobject 2");
                    System.Console.ReadKey();
                    DocTextObject docText2 = new DocTextObject($@"{Directory.GetCurrentDirectory()}\testdata\Test document2.docx");

                    System.Console.WriteLine("docText1.open()");
                    System.Console.ReadKey();
                    docText1.open();

                    System.Console.WriteLine("docText2.open()");
                    System.Console.ReadKey();
                    docText2.open();

                    System.Console.WriteLine("docText2.close()");
                    System.Console.ReadKey();
                    docText2.close();

                    System.Console.WriteLine("test2 finished");
                }
                if (selectedTest == "3")
                {
                    //2. open and close other doc text object
                    System.Console.WriteLine("set doctextobject 1");
                    System.Console.ReadKey();
                    DocTextObject docText1 = new DocTextObject($@"{Directory.GetCurrentDirectory()}\testdata\Test document.docx");

                    System.Console.WriteLine("set doctextobject 2");
                    System.Console.ReadKey();
                    DocTextObject docText2 = new DocTextObject($@"{Directory.GetCurrentDirectory()}\testdata\Test document2.docx");

                    System.Console.WriteLine("docText1.open()");
                    System.Console.ReadKey();
                    docText1.open();

                    System.Console.WriteLine("quit application");
                    System.Console.ReadKey();
                    docText1.application().Quit();

                    System.Console.WriteLine("docText2.open()");
                    System.Console.ReadKey();
                    docText2.open();

                    System.Console.WriteLine("docText2.close()");
                    System.Console.ReadKey();
                    docText2.close();

                    System.Console.WriteLine("test3 finished");
                }
                if (selectedTest == "4")
                {
                    //2. open and close other doc text object
                    System.Console.WriteLine("set doctextobject 1");
                    System.Console.ReadKey();
                    DocTextObject docText1 = new DocTextObject($@"{Directory.GetCurrentDirectory()}\testdata\Test document.docx");

                    System.Console.WriteLine("docText1.open()");
                    System.Console.ReadKey();
                    docText1.open();

                    System.Console.WriteLine("again docText1.open()");
                    System.Console.ReadKey();
                    docText1.open();

                    System.Console.WriteLine("docText1.close()");
                    System.Console.ReadKey();
                    docText1.close();

                    System.Console.WriteLine("test4 finished");
                }
                if (selectedTest == "5")
                {
                    System.Console.WriteLine("read testblock.json");
                    System.Console.ReadKey();
                    string json = File.ReadAllText("testdata\\testblock.json");

                    System.Console.WriteLine("set json deserialier");
                    System.Console.ReadKey();
                    var settings = new JsonSerializerSettings()
                    {
                        TypeNameHandling = TypeNameHandling.All
                    };

                    System.Console.WriteLine("deserialize json");
                    System.Console.ReadKey();
                    var block = JsonConvert.DeserializeObject <ExtractionBlock>(json, settings);

                    System.Console.WriteLine("print result");
                    System.Console.ReadKey();
                    System.Console.WriteLine($@"block.order: {block.order}");
                    System.Console.WriteLine($@"block.name: {block.name}");
                    System.Console.WriteLine($@"block.extractionStrategy.getType(): {block.extractionStrategy.GetType()}");
                    var textPattern = (block.extractionStrategy as PatternMatchingExtraction).textPattern;
                    System.Console.WriteLine($@"textPattern.cutBegin: {textPattern.cutBegin}");
                    System.Console.WriteLine($@"textPattern.cutEnd: {textPattern.cutEnd}");
                    System.Console.WriteLine($@"textPattern.pattern: {textPattern.pattern}");
                }
                if (selectedTest == "6")
                {
                    System.Console.WriteLine("read testpipeline.json");
                    System.Console.ReadKey();
                    string json = File.ReadAllText("testdata\\testpipeline.json");

                    System.Console.WriteLine("set json deserialier");
                    System.Console.ReadKey();
                    var settings = new JsonSerializerSettings()
                    {
                        TypeNameHandling = TypeNameHandling.All
                    };

                    System.Console.WriteLine("deserialize json");
                    System.Console.ReadKey();
                    var pipeline = JsonConvert.DeserializeObject <ExtractionPipeline>(json, settings);

                    System.Console.WriteLine("print result");
                    System.Console.ReadKey();
                    var extractionBlock2     = pipeline.extractionBlocks.ToList()[1];
                    var transformationBlock2 = pipeline.transformationBlocks.ToList()[1];
                    System.Console.WriteLine("extraction block: ");
                    System.Console.WriteLine($@"extraction block.order: {extractionBlock2.order}");
                    System.Console.WriteLine($@"extraction block.name: {extractionBlock2.name}");
                    System.Console.WriteLine($@"extraction block.extractionStrategy.getType(): {extractionBlock2.extractionStrategy.GetType()}");
                    var textPattern = (extractionBlock2.extractionStrategy as PatternMatchingExtraction).textPattern;
                    System.Console.WriteLine($@"extraction textPattern.cutBegin: {textPattern.cutBegin}");
                    System.Console.WriteLine($@"extraction textPattern.cutEnd: {textPattern.cutEnd}");
                    System.Console.WriteLine($@"extraction textPattern.pattern: {textPattern.pattern}");
                    System.Console.WriteLine();
                    System.Console.WriteLine("transformation block: ");
                    System.Console.WriteLine($@"transformation block.order: {transformationBlock2.order}");
                    System.Console.WriteLine($@"transformation block.name: {transformationBlock2.name}");
                    System.Console.WriteLine($@"transformation block.targetExtractionId: {transformationBlock2.targetExtractionId}");
                    System.Console.WriteLine($@"transformation block..extractionStrategy.GetType(): {transformationBlock2.transformationStrategy.GetType()}");
                }
                if (selectedTest == "7")
                {
                    System.Console.WriteLine("read testcheckpipeline.json");
                    System.Console.ReadKey();
                    string json = File.ReadAllText("testdata\\testcheckpipeline.json");

                    System.Console.WriteLine("set json deserialier");
                    System.Console.ReadKey();
                    var settings = new JsonSerializerSettings()
                    {
                        TypeNameHandling = TypeNameHandling.All
                    };

                    System.Console.WriteLine("deserialize json");
                    System.Console.ReadKey();
                    var pipeline = JsonConvert.DeserializeObject <ContainsCheckPipeline>(json, settings);

                    System.Console.WriteLine("print result");
                    System.Console.ReadKey();
                    var checkBlock  = pipeline.containsCheckBlocks.ToList()[1];
                    var textPattern = (checkBlock.extractionStrategy as PatternMatchingExtraction).textPattern;
                    System.Console.WriteLine($@"checkpipeline textPattern.cutBegin: {textPattern.cutBegin}");
                    System.Console.WriteLine($@"checkpipeline textPattern.cutEnd: {textPattern.cutEnd}");
                    System.Console.WriteLine($@"checkpipeline textPattern.pattern: {textPattern.pattern}");
                    System.Console.WriteLine();
                    System.Console.WriteLine($@"checkpipeline result: {pipeline.expectedResults.ToList()[1]}");
                    System.Console.WriteLine($@"checkpipeline findText: {checkBlock.findTarget}");
                }
                if (selectedTest == "8")
                {
                    System.Console.WriteLine("target path:");
                    var path = System.Console.ReadLine();
                    var docstringDocObject = new DocStringTextObject(path);
                    System.Console.WriteLine(docstringDocObject.text());
                }
                if (selectedTest == "9")
                {
                    System.Console.WriteLine("open test pdf");
                    System.Console.ReadKey();
                    var pdfObject = new PdfTextObject(@"testdata\Test document.pdf");
                    pdfObject.open();
                    System.Console.WriteLine("kill test pdf");
                    System.Console.ReadKey();
                    pdfObject.close();
                }
                if (selectedTest == "exit")
                {
                    return;
                }
            }
        }
Exemplo n.º 4
0
        public static Bitmap GetImageObject(PdfPage page, decimal dpiX, decimal dpiY)
        {
            if (page.PageObjects.Count == 0)
            {
                return(null);
            }

            List <PdfPageObject> imageObjects = page.PageObjects.Where(f => f.ObjectType == PageObjectTypes.PDFPAGE_IMAGE).ToList();
            bool otherVisibleObjects          = false;

            foreach (PdfPageObject po in page.PageObjects)
            {
                switch (po.ObjectType)
                {
                case PageObjectTypes.PDFPAGE_FORM:
                    otherVisibleObjects = true;
                    break;

                case PageObjectTypes.PDFPAGE_IMAGE:
                    break;

                case PageObjectTypes.PDFPAGE_PATH:
                    PdfPathObject path = po as PdfPathObject;
                    if (path.FillType != FillModes.None)
                    {
                        otherVisibleObjects = true;
                    }
                    break;

                case PageObjectTypes.PDFPAGE_SHADING:
                    // -- Seems like this is always visible.
                    // PdfShadingObject shading = po as PdfShadingObject;
                    otherVisibleObjects = true;
                    break;

                case PageObjectTypes.PDFPAGE_TEXT:
                    PdfTextObject text = po as PdfTextObject;
                    if (text.RenderMode != TextRenderingModes.Nothing)
                    {
                        otherVisibleObjects = true;
                    }
                    break;
                }
                if (otherVisibleObjects == true)
                {
                    break;
                }
            }


            // one image and no other visible elements
            if (imageObjects.Count == 1 && !otherVisibleObjects)
            {
                AppLogger.Trace("Using image extract method from PDF.");
                var imageObject = imageObjects.First() as PdfImageObject;

                if (imageObject == null || imageObject.Bitmap == null || imageObject.Bitmap.Image == null)
                {
                    AppLogger.Warn("Failed to get image bitmap from PDFium image object.");
                    return(null);
                }
                Bitmap returnBitmap = new Bitmap(imageObject.Bitmap.Image);
                returnBitmap.SetResolution((float)dpiX, (float)dpiY);
                return(returnBitmap);
            }

            return(null);
        }