Beispiel #1
0
        public void BuildPrintPage(object sender, PrintPageEventArgs e)
        {
            // Build font cache if needed
            RefreshGraphicCharactersCache();

            // Fancy filtering
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            // Draw each embedded IM image in the current page. Positional information is stored inside the IID field of each image container
            foreach (Container imc in pageContainers[curPageIndex].Structures.Select(f => f.LowestLevelContainer).Distinct()
                     .Where(c => c.Structures[0] is BII && afpFile.ParsedIMImages.ContainsKey(c)))
            {
                DrawIMImage(imc, afpFile.ParsedIMImages[imc].ToList(), 0, 0, e);
            }

            // Draw each embedded IOCA image in the current page. Positional information is stored inside of the OBD/OBP fields of each image container
            foreach (Container ioc in pageContainers[curPageIndex].Structures.Select(f => f.LowestLevelContainer).Distinct()
                     .Where(c => c.Structures[0] is BIM && afpFile.ParsedImages.ContainsKey(c)))
            {
                DrawIOCAImage(ioc, afpFile.ParsedImages[ioc].ToList(), 0, 0, e);
            }

            // Include each IM and IOCA image in IPS by looking up the loaded resource
            foreach (IPS pageSegment in pageContainers[curPageIndex].GetStructures <IPS>())
            {
                // Find the first loaded image resource of the indicated name
                Resource loadedResource = afpFile.Resources.FirstOrDefault(r => r.ResourceName == pageSegment.SegmentName.ToUpper().Trim() &&
                                                                           r.IsLoaded && r.ResourceType == Resource.eResourceType.PageSegment);

                if (loadedResource != null)
                {
                    // Get starting positional information by querying the active environment group
                    Container aegContainer   = pageContainers[curPageIndex].GetStructure <BAG>().LowestLevelContainer;
                    PGD       pageDescriptor = aegContainer.GetStructure <PGD>();
                    float     xInch          = (float)Converters.GetInches(pageSegment.XOrigin, pageDescriptor.UnitsPerYBase, pageDescriptor.BaseUnit) * 100;
                    float     yInch          = (float)Converters.GetInches(pageSegment.YOrigin, pageDescriptor.UnitsPerYBase, pageDescriptor.BaseUnit) * 100;

                    Container imc = loadedResource.Fields.Select(f => f.LowestLevelContainer).FirstOrDefault(c => c.Structures[0] is BII);
                    Container ioc = loadedResource.Fields.Select(f => f.LowestLevelContainer).FirstOrDefault(c => c.Structures[0] is BIM);
                    if (imc != null && afpFile.ParsedIMImages.ContainsKey(imc))
                    {
                        DrawIMImage(imc, afpFile.ParsedIMImages[imc].ToList(), xInch, yInch, e);
                    }
                    else if (ioc != null && afpFile.ParsedImages.ContainsKey(ioc))
                    {
                        DrawIOCAImage(ioc, afpFile.ParsedImages[ioc].ToList(), xInch, yInch, e);
                    }
                }
            }

            DrawPresentationText(e);

            // Increment the current page index and check to see if there are any more
            e.HasMorePages = ++curPageIndex < pageContainers.Count;

            if (!e.HasMorePages)
            {
                curPageIndex = 0;
            }
        }
Beispiel #2
0
        private void GetDescriptorInfo()
        {
            BAG activeEnvironmentGroup = pageContainers[curPageIndex].GetStructure <BAG>();

            if (activeEnvironmentGroup != null)
            {
                aeContainer = activeEnvironmentGroup.LowestLevelContainer;

                // Grab the Presentation Text Descriptor and store the units per base and base unit
                PGD  pageDescriptor = pageContainers[curPageIndex].GetStructure <PGD>();
                PTD1 descriptor1    = pageContainers[curPageIndex].GetStructure <PTD1>();
                PTD2 descriptor2    = pageContainers[curPageIndex].GetStructure <PTD2>();

                if (descriptor2 != null)
                {
                    unitsPerBase = descriptor2.UnitsPerXBase;
                    measurement  = descriptor2.BaseUnit;
                }
                else if (descriptor1 != null)
                {
                    unitsPerBase = descriptor1.UnitsPerXBase;
                    measurement  = descriptor1.BaseUnit;
                }
                else
                {
                    unitsPerBase = pageDescriptor.UnitsPerXBase;
                    measurement  = pageDescriptor.BaseUnit;
                }
            }
            else
            {
                throw new NotImplementedException("Presentation text could not be displayed - no active environment group found.");
            }
        }
Beispiel #3
0
        /// <summary>
        /// Adds a new page container with its required fields to an existing document container
        /// </summary>
        /// <param name="docContainer">The container of the existing document in the AFP to add the page to</param>
        /// <param name="pageName">The optional 8 character name of the new page</param>
        /// <param name="groupName">The optional 8 character name of the new active environment group</param>
        /// <param name="xUnitsPer10Inches">The number of horizontal measurement units on a page/presentation space for every 10 inches</param>
        /// <param name="yUnitsPer10Inches">The number of vertical units on a page/presentation space for every 10 inches</param>
        /// <param name="pageUnitWidth">The number of units that represent the page's width. To convert to inches: (pageUnitWidth / xUnitsPer10Inches) * 10</param>
        /// <param name="pageUnitHeight">The number of units that represent the page's height. To convert to inches: (pageUnitHeight / yUnitsPer10Inches) * 10</param>
        /// <returns>The resulting page's container</returns>
        public Container AddPageToDocument(Container docContainer, string pageName = "", string groupName = "", ushort xUnitsPer10Inches = 3000,
                                           ushort yUnitsPer10Inches = 3000, ushort pageUnitWidth          = 2550, ushort pageUnitHeight  = 3300)
        {
            Container pageContainer = null;

            // Verify the container parameter is truly a document
            if (!(docContainer.Structures[0] is BDT && docContainer.Structures.Last() is EDT))
            {
                throw new Exception("The passed container parameter does not appear to be a Document container.");
            }

            // Verify the document exists in the container (and store index to insert if it's ok)
            int indexToInsert = 0;

            for (int i = 0; i < Fields.Count; i++)
            {
                // As soon as we find the begin tag, verify each field in the container matches and break
                if (Fields[i] == docContainer.Structures[0])
                {
                    for (int j = 0; j < docContainer.Structures.Count; j++)
                    {
                        if (Fields[i + j] != docContainer.Structures[j])
                        {
                            throw new Exception("Invalid container - does not exist in list of fields.");
                        }
                    }
                    indexToInsert = (i + docContainer.Structures.Count) - 1;
                    break;
                }
            }

            // Create page tags
            BPG newBPG = new BPG(pageName);
            EPG newEPG = new EPG(pageName);

            // A page needs an active environment group
            BAG newBAG = new BAG(groupName);
            EAG newAEG = new EAG(groupName);

            // An active environment group in a page needs both page and presentation text descriptor fields
            PGD  newPGD = new PGD(xUnitsPer10Inches, yUnitsPer10Inches, pageUnitWidth, pageUnitHeight);
            PTD1 newPTD = new PTD1(xUnitsPer10Inches, yUnitsPer10Inches, pageUnitWidth, pageUnitHeight);

            // Build the list of new fields and add them to the end of the document
            List <StructuredField> newFields = new List <StructuredField>()
            {
                newBPG, newBAG, newPGD, newPTD, newAEG, newEPG
            };

            AddFields(newFields, indexToInsert);

            // Set and return the created page's container
            pageContainer = newBPG.LowestLevelContainer;
            return(pageContainer);
        }
Beispiel #4
0
        public PGDBlockVirtualFile(IVirtualFile pgdFile, sbyte[] key, int dataOffset) : base(pgdFile)
        {
            if (key != null)
            {
                this.key = key.Clone();
            }
            this.dataOffset = dataOffset;

            base.ioLseek(dataOffset);

            pgd = (new CryptoEngine()).PGDEngine;

            readHeader();

            this.dataOffset += dataOffset;
        }
Beispiel #5
0
        private void btnPreview_Click(object sender, EventArgs e)
        {
            // Only display a print preview if it's a paged document. Otherwise, display an image from the page segment if we can
            switch (DocType)
            {
            case eFileType.Document:
                // Verify they want to view if there are missing resources
                if (afpFile.Resources.All(r => r.IsLoaded || r.IsNETCodePage) || MessageBox.Show("There are referenced resources that have not been located. Preview anyway?"
                                                                                                 , "Missing Resources", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    // Set up a print preview dialog and wire it to our print parser's build event
                    PrintPreviewDialog ppd = new PrintPreviewDialog()
                    {
                        Document = new PrintDocument()
                        {
                            DocumentName = opts.LastOpenedFile
                        }
                    };
                    ppd.Controls.OfType <ToolStrip>().First().Items["printToolStripButton"].Visible = false;    // Temp disable until we actually might want to print something
                    ((Form)ppd).WindowState = FormWindowState.Maximized;
                    ppd.Document.PrintPage += printParser.BuildPrintPage;

                    // Set page size by checking the first PGD. Width and height are in 1/100 inch
                    PGD pgd    = afpFile.Fields.OfType <PGD>().First();
                    int xWidth = (int)(Converters.GetInches((int)pgd.XSize, pgd.UnitsPerXBase, pgd.BaseUnit) * 100);
                    int yWidth = (int)(Converters.GetInches((int)pgd.YSize, pgd.UnitsPerYBase, pgd.BaseUnit) * 100);
                    ppd.Document.DefaultPageSettings.PaperSize = new PaperSize("Custom", xWidth, yWidth);

                    ppd.ShowDialog();
                }

                break;

            case eFileType.IOCAImage:
            case eFileType.IMImage:
                int fileCounter = 1;

                if (afpFile.ParsedImages.Any() || afpFile.ParsedIMImages.Any())
                {
                    Cursor = Cursors.WaitCursor;

                    // Clear out the directory of existing pngs
                    foreach (string file in Directory.GetFiles(Environment.CurrentDirectory))
                    {
                        if (new FileInfo(file).Extension.ToUpper() == ".PNG")
                        {
                            File.Delete(file);
                        }
                    }

                    // Generate a .png from the image data and save it to the exe directory
                    if (DocType == eFileType.IOCAImage)
                    {
                        foreach (KeyValuePair <Container, IReadOnlyList <ImageInfo> > kvp in afpFile.ParsedImages)
                        {
                            foreach (ImageInfo image in kvp.Value)
                            {
                                Bitmap png = new Bitmap(new MemoryStream(image.Data));

                                // Get resolution from descriptor
                                IDD   descriptor = kvp.Key.GetStructure <IDD>();
                                float xScale     = (float)Converters.GetInches(png.Width, descriptor.HResolution, descriptor.BaseUnit);
                                float yScale     = (float)Converters.GetInches(png.Height, descriptor.VResolution, descriptor.BaseUnit);
                                png.SetResolution(png.Width / xScale, png.Height / yScale);

                                // Generate image
                                png.Save($"{Environment.CurrentDirectory}\\Image {fileCounter++}.png", System.Drawing.Imaging.ImageFormat.Png);
                            }
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair <Container, IReadOnlyList <IMImageCell> > kvp in afpFile.ParsedIMImages)
                        {
                            IMImageCell.GenerateBitmap(kvp.Key.GetStructure <IID>(), kvp.Value.ToList())
                            .Save($"{Environment.CurrentDirectory}\\Image {fileCounter++}.png", System.Drawing.Imaging.ImageFormat.Png);
                        }
                    }

                    btnPreview.Enabled = false;
                    Cursor             = Cursors.Default;
                    if (MessageBox.Show($"{fileCounter} image(s) created in executing directory. Open directory?",
                                        "Images Created", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        System.Diagnostics.Process.Start(Environment.CurrentDirectory);
                    }
                }
                else
                {
                    MessageBox.Show("No image containers found, though this does appear to be an image file.");
                }
                break;
            }
        }