Пример #1
0
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
            string inFile  = dataDir + "TH.pdf";
            string outFile = dataDir + "TH_out.pdf";
            string logFile = dataDir + "TH_out.xml";

            // Open document
            Document document = new Document(inFile);

            // Gets tagged content and root structure element
            ITaggedContent   taggedContent = document.TaggedContent;
            StructureElement rootElement   = taggedContent.RootElement;

            // Set title for tagged pdf document
            taggedContent.SetTitle("Document with images");

            foreach (FigureElement figureElement in rootElement.FindElements <FigureElement>(true))
            {
                // Set Alternative Text  for Figure
                figureElement.AlternativeText = "Figure alternative text (technique 2)";


                // Create and Set BBox Attribute
                StructureAttribute bboxAttribute = new StructureAttribute(AttributeKey.BBox);
                bboxAttribute.SetRectangleValue(new Rectangle(0.0, 0.0, 100.0, 100.0));

                StructureAttributes figureLayoutAttributes = figureElement.Attributes.GetAttributes(AttributeOwnerStandard.Layout);
                figureLayoutAttributes.SetAttribute(bboxAttribute);
            }

            // Move Span Element into Paragraph (find wrong span and paragraph in first TD)
            TableElement     tableElement   = rootElement.FindElements <TableElement>(true)[0];
            SpanElement      spanElement    = tableElement.FindElements <SpanElement>(true)[0];
            TableTDElement   firstTdElement = tableElement.FindElements <TableTDElement>(true)[0];
            ParagraphElement paragraph      = firstTdElement.FindElements <ParagraphElement>(true)[0];

            // Move Span Element into Paragraph
            spanElement.ChangeParentElement(paragraph);


            // Save document
            document.Save(outFile);



            // Checking PDF/UA Compliance for out document
            document = new Document(outFile);

            bool isPdfUaCompliance = document.Validate(logFile, PdfFormat.PDF_UA_1);

            Console.WriteLine(String.Format("PDF/UA compliance: {0}", isPdfUaCompliance));
        }
Пример #2
0
        public static void Run()
        {
            // ExStart:LinkStructureElements
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
            string outFile = dataDir + "LinkStructureElements_Output.pdf";
            string logFile = dataDir + "46035_log.xml";
            string imgFile = dataDir + "google-icon-512.png";

            // Creation document and getting Tagged Pdf Content
            Document       document      = new Document();
            ITaggedContent taggedContent = document.TaggedContent;


            // Setting Title and Nature Language for document
            taggedContent.SetTitle("Link Elements Example");
            taggedContent.SetLanguage("en-US");

            // Getting Root structure element (Document structure element)
            StructureElement rootElement = taggedContent.RootElement;


            ParagraphElement p1 = taggedContent.CreateParagraphElement();

            rootElement.AppendChild(p1);
            LinkElement link1 = taggedContent.CreateLinkElement();

            p1.AppendChild(link1);
            link1.Hyperlink = new WebHyperlink("http://google.com");
            link1.SetText("Google");
            link1.AlternateDescriptions = "Link to Google";


            ParagraphElement p2 = taggedContent.CreateParagraphElement();

            rootElement.AppendChild(p2);
            LinkElement link2 = taggedContent.CreateLinkElement();

            p2.AppendChild(link2);
            link2.Hyperlink = new WebHyperlink("http://google.com");
            SpanElement span2 = taggedContent.CreateSpanElement();

            span2.SetText("Google");
            link2.AppendChild(span2);
            link2.AlternateDescriptions = "Link to Google";


            ParagraphElement p3 = taggedContent.CreateParagraphElement();

            rootElement.AppendChild(p3);
            LinkElement link3 = taggedContent.CreateLinkElement();

            p3.AppendChild(link3);
            link3.Hyperlink = new WebHyperlink("http://google.com");
            SpanElement span31 = taggedContent.CreateSpanElement();

            span31.SetText("G");
            SpanElement span32 = taggedContent.CreateSpanElement();

            span32.SetText("oogle");
            link3.AppendChild(span31);
            link3.SetText("-");
            link3.AppendChild(span32);
            link3.AlternateDescriptions = "Link to Google";


            ParagraphElement p4 = taggedContent.CreateParagraphElement();

            rootElement.AppendChild(p4);
            LinkElement link4 = taggedContent.CreateLinkElement();

            p4.AppendChild(link4);
            link4.Hyperlink = new WebHyperlink("http://google.com");
            link4.SetText("The multiline link: Google Google Google Google Google Google Google Google Google Google Google Google Google Google Google Google Google Google Google Google");
            link4.AlternateDescriptions = "Link to Google (multiline)";


            ParagraphElement p5 = taggedContent.CreateParagraphElement();

            rootElement.AppendChild(p5);
            LinkElement link5 = taggedContent.CreateLinkElement();

            p5.AppendChild(link5);
            link5.Hyperlink = new WebHyperlink("http://google.com");
            FigureElement figure5 = taggedContent.CreateFigureElement();

            figure5.SetImage(imgFile, 1200);
            figure5.AlternativeText = "Google icon";
            StructureAttributes linkLayoutAttributes = link5.Attributes.GetAttributes(AttributeOwnerStandard.Layout);
            StructureAttribute  placementAttribute   = new StructureAttribute(AttributeKey.Placement);

            placementAttribute.SetNameValue(AttributeName.Placement_Block);
            linkLayoutAttributes.SetAttribute(placementAttribute);
            link5.AppendChild(figure5);
            link5.AlternateDescriptions = "Link to Google";


            // Save Tagged Pdf Document
            document.Save(outFile);

            // Checking PDF/UA compliance
            document = new Document(outFile);
            bool isPdfUaCompliance = document.Validate(logFile, PdfFormat.PDF_UA_1);

            Console.WriteLine(String.Format("PDF/UA compliance: {0}", isPdfUaCompliance));
            // ExEnd:LinkStructureElements
        }
        /// <summary>
        /// This feature is supported by version 19.6 or greater
        /// </summary>
        public static void Run()
        {
            // ExStart:1
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

            // Create document
            Document       document      = new Document();
            ITaggedContent taggedContent = document.TaggedContent;

            taggedContent.SetTitle("Example table");
            taggedContent.SetLanguage("en-US");

            // Get root structure element
            StructureElement rootElement = taggedContent.RootElement;


            TableElement tableElement = taggedContent.CreateTableElement();

            rootElement.AppendChild(tableElement);

            tableElement.Border = new BorderInfo(BorderSide.All, 1.2F, Color.DarkBlue);

            TableTHeadElement tableTHeadElement = tableElement.CreateTHead();
            TableTBodyElement tableTBodyElement = tableElement.CreateTBody();
            TableTFootElement tableTFootElement = tableElement.CreateTFoot();
            int rowCount = 50;
            int colCount = 4;
            int rowIndex;
            int colIndex;

            TableTRElement headTrElement = tableTHeadElement.CreateTR();

            headTrElement.AlternativeText = "Head Row";

            headTrElement.BackgroundColor = Color.LightGray;

            for (colIndex = 0; colIndex < colCount; colIndex++)
            {
                TableTHElement thElement = headTrElement.CreateTH();
                thElement.SetText(String.Format("Head {0}", colIndex));

                thElement.BackgroundColor = Color.GreenYellow;
                thElement.Border          = new BorderInfo(BorderSide.All, 4.0F, Color.Gray);

                thElement.IsNoBorder = true;
                thElement.Margin     = new MarginInfo(16.0, 2.0, 8.0, 2.0);

                thElement.Alignment = HorizontalAlignment.Right;
            }

            for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
            {
                TableTRElement trElement = tableTBodyElement.CreateTR();
                trElement.AlternativeText = String.Format("Row {0}", rowIndex);

                for (colIndex = 0; colIndex < colCount; colIndex++)
                {
                    int colSpan = 1;
                    int rowSpan = 1;

                    if (colIndex == 1 && rowIndex == 1)
                    {
                        colSpan = 2;
                        rowSpan = 2;
                    }
                    else if (colIndex == 2 && (rowIndex == 1 || rowIndex == 2))
                    {
                        continue;
                    }
                    else if (rowIndex == 2 && (colIndex == 1 || colIndex == 2))
                    {
                        continue;
                    }

                    TableTDElement tdElement = trElement.CreateTD();
                    tdElement.SetText(String.Format("Cell [{0}, {1}]", rowIndex, colIndex));


                    tdElement.BackgroundColor = Color.Yellow;
                    tdElement.Border          = new BorderInfo(BorderSide.All, 4.0F, Color.Gray);

                    tdElement.IsNoBorder = false;
                    tdElement.Margin     = new MarginInfo(8.0, 2.0, 8.0, 2.0);

                    tdElement.Alignment = HorizontalAlignment.Center;

                    TextState cellTextState = new TextState();
                    cellTextState.ForegroundColor  = Color.DarkBlue;
                    cellTextState.FontSize         = 7.5F;
                    cellTextState.FontStyle        = FontStyles.Bold;
                    cellTextState.Font             = FontRepository.FindFont("Arial");
                    tdElement.DefaultCellTextState = cellTextState;

                    tdElement.IsWordWrapped     = true;
                    tdElement.VerticalAlignment = VerticalAlignment.Center;

                    tdElement.ColSpan = colSpan;
                    tdElement.RowSpan = rowSpan;
                }
            }

            TableTRElement footTrElement = tableTFootElement.CreateTR();

            footTrElement.AlternativeText = "Foot Row";

            footTrElement.BackgroundColor = Color.LightSeaGreen;

            for (colIndex = 0; colIndex < colCount; colIndex++)
            {
                TableTDElement tdElement = footTrElement.CreateTD();
                tdElement.SetText(String.Format("Foot {0}", colIndex));

                tdElement.Alignment = HorizontalAlignment.Center;
                tdElement.StructureTextState.FontSize  = 7F;
                tdElement.StructureTextState.FontStyle = FontStyles.Bold;
            }


            StructureAttributes tableAttributes  = tableElement.Attributes.GetAttributes(AttributeOwnerStandard.Table);
            StructureAttribute  summaryAttribute = new StructureAttribute(AttributeKey.Summary);

            summaryAttribute.SetStringValue("The summary text for table");
            tableAttributes.SetAttribute(summaryAttribute);


            // Save Tagged Pdf Document
            document.Save(dataDir + "CreateTableElement.pdf");

            // Checking PDF/UA compliance
            document = new Document(dataDir + "CreateTableElement.pdf");
            bool isPdfUaCompliance = document.Validate(dataDir + "table.xml", PdfFormat.PDF_UA_1);

            Console.WriteLine(String.Format("PDF/UA compliance: {0}", isPdfUaCompliance));

            // ExEnd:1
        }