public virtual void DocWithInvalidMapping07() { NUnit.Framework.Assert.That(() => { PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "docWithInvalidMapping07.pdf", new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); pdfDocument.SetTagged(); Document document = new Document(pdfDocument); PdfNamespace stdNs2 = pdfDocument.GetTagStructureContext().FetchNamespace(StandardNamespaces.PDF_2_0); int numOfTransitiveMappings = 120; String prevRole = LayoutTaggingPdf2Test.HtmlRoles.span; for (int i = 0; i < numOfTransitiveMappings; ++i) { String nextRole = "span" + i; stdNs2.AddNamespaceRoleMapping(prevRole, nextRole, stdNs2); prevRole = nextRole; } stdNs2.AddNamespaceRoleMapping(prevRole, StandardRoles.SPAN, stdNs2); Text customRolePText1 = new Text("Hello world text."); customRolePText1.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.span); customRolePText1.GetAccessibilityProperties().SetNamespace(stdNs2); document.Add(new Paragraph(customRolePText1)); document.Close(); } , NUnit.Framework.Throws.InstanceOf <PdfException>().With.Message.EqualTo(String.Format(PdfException.RoleInNamespaceIsNotMappedToAnyStandardRole, "span", "http://iso.org/pdf2/ssn"))) ; }
public virtual void CustomRolesMappingPdf2() { PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "customRolesMappingPdf2.pdf", new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); pdfDocument.SetTagged(); TagStructureContext tagsContext = pdfDocument.GetTagStructureContext(); PdfNamespace stdNamespace2 = tagsContext.FetchNamespace(StandardNamespaces.PDF_2_0); PdfNamespace xhtmlNs = new PdfNamespace("http://www.w3.org/1999/xhtml"); PdfNamespace html4Ns = new PdfNamespace("http://www.w3.org/TR/html4"); String h9 = "H9"; String h11 = "H11"; // deliberately mapping to H9 tag xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.h1, h9, stdNamespace2); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.p, StandardRoles.P, stdNamespace2); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.img, StandardRoles.FIGURE, stdNamespace2); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.ul, StandardRoles.L, stdNamespace2); xhtmlNs.AddNamespaceRoleMapping(StandardRoles.SPAN, LayoutTaggingPdf2Test.HtmlRoles.span, xhtmlNs); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.span, StandardRoles.SPAN, stdNamespace2); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.center, StandardRoles.P, stdNamespace2); html4Ns.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.center, LayoutTaggingPdf2Test.HtmlRoles.center , xhtmlNs); // test some tricky mapping cases stdNamespace2.AddNamespaceRoleMapping(h9, h11, stdNamespace2); stdNamespace2.AddNamespaceRoleMapping(h11, h11, stdNamespace2); tagsContext.GetAutoTaggingPointer().SetNamespaceForNewTags(xhtmlNs); Document document = new Document(pdfDocument); AddContentToDocInCustomNs(pdfDocument, stdNamespace2, xhtmlNs, html4Ns, h11, document); document.Close(); CompareResult("customRolesMappingPdf2"); }
public virtual void DocWithExplicitAndImplicitDefaultNsAtTheSameTime() { PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "docWithExplicitAndImplicitDefaultNsAtTheSameTime.pdf" , new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); pdfDocument.SetTagged(); TagStructureContext tagsContext = pdfDocument.GetTagStructureContext(); tagsContext.SetDocumentDefaultNamespace(null); PdfNamespace explicitDefaultNs = tagsContext.FetchNamespace(StandardNamespaces.PDF_1_7); Document document = new Document(pdfDocument); Paragraph hPara = new Paragraph("This is header."); hPara.GetAccessibilityProperties().SetRole(StandardRoles.H); hPara.GetAccessibilityProperties().SetNamespace(explicitDefaultNs); document.Add(hPara); PdfNamespace xhtmlNs = new PdfNamespace("http://www.w3.org/1999/xhtml"); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.img, StandardRoles.FIGURE, explicitDefaultNs ); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.ul, StandardRoles.L); iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(sourceFolder + imageName)).SetWidth(100 ); img.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.img); img.GetAccessibilityProperties().SetNamespace(xhtmlNs); document.Add(img); List list = new List().SetListSymbol("-> "); list.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.ul); list.GetAccessibilityProperties().SetNamespace(xhtmlNs); list.Add("list item").Add("list item").Add("list item").Add("list item").Add("list item"); document.Add(list); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.center, "Center", explicitDefaultNs); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.p, "Note", explicitDefaultNs); explicitDefaultNs.AddNamespaceRoleMapping("Center", StandardRoles.P, explicitDefaultNs); explicitDefaultNs.AddNamespaceRoleMapping("Note", "Note"); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.span, "Note"); pdfDocument.GetStructTreeRoot().AddRoleMapping("Note", StandardRoles.P); Paragraph centerPara = new Paragraph("centered text"); centerPara.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.center); centerPara.GetAccessibilityProperties().SetNamespace(xhtmlNs); Text simpleSpan = new Text("simple p with simple span"); simpleSpan.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.span); simpleSpan.GetAccessibilityProperties().SetNamespace(xhtmlNs); Paragraph simplePara = new Paragraph(simpleSpan); simplePara.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.p); simplePara.GetAccessibilityProperties().SetNamespace(xhtmlNs); document.Add(centerPara).Add(simplePara); pdfDocument.GetStructTreeRoot().AddRoleMapping("I", StandardRoles.SPAN); Text iSpan = new Text("cursive span"); iSpan.GetAccessibilityProperties().SetRole("I"); document.Add(new Paragraph(iSpan)); document.Close(); CompareResult("docWithExplicitAndImplicitDefaultNsAtTheSameTime"); }
public virtual void DocWithInvalidMapping06() { PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "docWithInvalidMapping06.pdf", new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); pdfDocument.SetTagged(); Document document = new Document(pdfDocument); TagStructureContext tagCntxt = pdfDocument.GetTagStructureContext(); PdfNamespace pointerNs = tagCntxt.FetchNamespace(StandardNamespaces.PDF_2_0); pointerNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.span, StandardRoles.SPAN, pointerNs); // deliberately creating namespace via constructor instead of using TagStructureContext#fetchNamespace PdfNamespace stdNs2 = new PdfNamespace(StandardNamespaces.PDF_2_0); stdNs2.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.span, StandardRoles.EM, stdNs2); Text customRolePText1 = new Text("Hello world text 1."); customRolePText1.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.span); customRolePText1.GetAccessibilityProperties().SetNamespace(stdNs2); document.Add(new Paragraph(customRolePText1)); Text customRolePText2 = new Text("Hello world text 2."); customRolePText2.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.span); // not explicitly setting namespace that we've manually created. This will lead to the situation, when // /Namespaces entry in StructTreeRoot would have two different namespace dictionaries with the same name. document.Add(new Paragraph(customRolePText2)); document.Close(); CompareResult("docWithInvalidMapping06"); }
public virtual void CustomRolesMappingPdf17() { PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "customRolesMappingPdf17.pdf", new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); pdfDocument.SetTagged(); PdfNamespace xhtmlNs = new PdfNamespace("http://www.w3.org/1999/xhtml"); PdfNamespace html4Ns = new PdfNamespace("http://www.w3.org/TR/html4"); String h9 = "H9"; String h1 = StandardRoles.H1; // deliberately mapping to H9 tag xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.h1, h9); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.p, StandardRoles.P); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.img, StandardRoles.FIGURE); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.ul, StandardRoles.L); xhtmlNs.AddNamespaceRoleMapping(StandardRoles.SPAN, LayoutTaggingPdf2Test.HtmlRoles.span, xhtmlNs); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.span, StandardRoles.SPAN); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.center, "Center"); html4Ns.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.center, LayoutTaggingPdf2Test.HtmlRoles.center , xhtmlNs); // test some tricky mapping cases pdfDocument.GetStructTreeRoot().AddRoleMapping(h9, h1); pdfDocument.GetStructTreeRoot().AddRoleMapping(h1, h1); pdfDocument.GetStructTreeRoot().AddRoleMapping("Center", StandardRoles.P); pdfDocument.GetStructTreeRoot().AddRoleMapping("I", StandardRoles.SPAN); pdfDocument.GetTagStructureContext().SetDocumentDefaultNamespace(null); pdfDocument.GetTagStructureContext().GetAutoTaggingPointer().SetNamespaceForNewTags(xhtmlNs); Document document = new Document(pdfDocument); AddContentToDocInCustomNs(pdfDocument, null, xhtmlNs, html4Ns, h1, document); document.Close(); CompareResult("customRolesMappingPdf17"); }
public virtual void DocWithInvalidMapping10() { PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "docWithInvalidMapping10.pdf", new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); pdfDocument.SetTagged(); TagStructureContext tagsContext = pdfDocument.GetTagStructureContext(); PdfNamespace ssn2 = tagsContext.FetchNamespace(StandardNamespaces.PDF_2_0); ssn2.AddNamespaceRoleMapping(StandardRoles.DOCUMENT, "Book", ssn2); ssn2.AddNamespaceRoleMapping("Book", StandardRoles.PART, ssn2); Document document = new Document(pdfDocument); document.Add(new Paragraph("hello world; root tag mapping")); document.Close(); CompareResult("docWithInvalidMapping10"); }
public virtual void StampTest05() { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "simpleDocNewStdNs.pdf"), new PdfWriter (destinationFolder + "stampTest05.pdf", new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); TagStructureContext tagCntxt = pdfDocument.GetTagStructureContext(); PdfNamespace xhtmlNs = tagCntxt.FetchNamespace("http://www.w3.org/1999/xhtml"); PdfNamespace ssn2 = tagCntxt.FetchNamespace(StandardNamespaces.PDF_2_0); xhtmlNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.ul, StandardRoles.L, ssn2); TagTreePointer pointer = new TagTreePointer(pdfDocument); pointer.MoveToKid(StandardRoles.TABLE).MoveToKid(StandardRoles.TR).MoveToKid(1, StandardRoles.TD).MoveToKid (StandardRoles.L); pointer.SetRole(LayoutTaggingPdf2Test.HtmlRoles.ul).GetProperties().SetNamespace(xhtmlNs); pdfDocument.Close(); CompareResult("stampTest05"); }
public virtual void DocWithInvalidMapping02() { NUnit.Framework.Assert.That(() => { PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "docWithInvalidMapping02.pdf", new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); pdfDocument.SetTagged(); TagStructureContext tagsContext = pdfDocument.GetTagStructureContext(); tagsContext.SetDocumentDefaultNamespace(null); PdfNamespace explicitDefaultNs = tagsContext.FetchNamespace(StandardNamespaces.PDF_1_7); Document document = new Document(pdfDocument); explicitDefaultNs.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.p, StandardRoles.P); Paragraph customRolePara = new Paragraph("Hello world text."); customRolePara.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.p); document.Add(customRolePara); document.Close(); } , NUnit.Framework.Throws.InstanceOf <PdfException>().With.Message.EqualTo(String.Format(PdfException.RoleIsNotMappedToAnyStandardRole, "p"))) ; }
public virtual void DocWithInvalidMapping04() { // TODO this test passes, however it seems, that mingling two standard namespaces in the same tag structure tree should be illegal // May be this should be checked if we would implement conforming PDF/UA docs generations in a way PDF/A docs are generated PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "docWithInvalidMapping04.pdf", new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); pdfDocument.SetTagged(); TagStructureContext tagsCntxt = pdfDocument.GetTagStructureContext(); PdfNamespace stdNs2 = tagsCntxt.FetchNamespace(StandardNamespaces.PDF_2_0); stdNs2.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.p, StandardRoles.P); Document document = new Document(pdfDocument); Paragraph customRolePara = new Paragraph("Hello world text."); customRolePara.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.p); document.Add(customRolePara); document.Close(); CompareResult("docWithInvalidMapping04"); }
public virtual void StructElemTest07() { PdfWriter writer = new PdfWriter(destinationFolder + "structElemTest07.pdf"); writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION); PdfDocument document = new PdfDocument(writer); document.SetTagged(); PdfStructElem doc = document.GetStructTreeRoot().AddKid(new PdfStructElem(document, PdfName.Document)); PdfPage page = document.AddNewPage(); PdfCanvas canvas = new PdfCanvas(page); canvas.BeginText(); canvas.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.COURIER), 24); canvas.SetTextMatrix(1, 0, 0, 1, 32, 512); PdfStructElem paragraph = doc.AddKid(new PdfStructElem(document, PdfName.P)); PdfStructElem span1 = paragraph.AddKid(new PdfStructElem(document, PdfName.Span, page)); canvas.OpenTag(new CanvasTag(span1.AddKid(new PdfMcrNumber(page, span1)))); canvas.ShowText("Hello "); canvas.CloseTag(); PdfStructElem span2 = paragraph.AddKid(new PdfStructElem(document, new PdfName("Chunk"), page)); canvas.OpenTag(new CanvasTag(span2.AddKid(new PdfMcrNumber(page, span2)))); canvas.ShowText("World"); canvas.CloseTag(); canvas.EndText(); canvas.Release(); PdfNamespace @namespace = new PdfNamespace("http://www.w3.org/1999/xhtml"); span1.SetNamespace(@namespace); span1.AddRef(span2); span1.SetPhoneticAlphabet(PdfName.ipa); span1.SetPhoneme(new PdfString("Heeeelllloooooo")); @namespace.AddNamespaceRoleMapping(StandardRoles.SPAN, StandardRoles.SPAN); document.GetStructTreeRoot().AddNamespace(@namespace); page.Flush(); document.Close(); CompareResult("structElemTest07.pdf", "cmp_structElemTest07.pdf", "diff_structElem_07_"); }
public virtual void DocWithInvalidMapping05() { NUnit.Framework.Assert.That(() => { PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "docWithInvalidMapping05.pdf", new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0))); pdfDocument.SetTagged(); Document document = new Document(pdfDocument); // deliberately creating namespace via constructor instead of using TagStructureContext#fetchNamespace PdfNamespace stdNs2 = new PdfNamespace(StandardNamespaces.PDF_2_0); stdNs2.AddNamespaceRoleMapping(LayoutTaggingPdf2Test.HtmlRoles.p, StandardRoles.P, stdNs2); Paragraph customRolePara = new Paragraph("Hello world text."); customRolePara.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.p); customRolePara.GetAccessibilityProperties().SetNamespace(stdNs2); document.Add(customRolePara); Paragraph customRolePara2 = new Paragraph("Hello world text."); customRolePara2.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.p); // not explicitly setting namespace that we've manually created. This will lead to the situation, when // /Namespaces entry in StructTreeRoot would have two different namespace dictionaries with the same name. document.Add(customRolePara2); document.Close(); } , NUnit.Framework.Throws.InstanceOf <PdfException>().With.Message.EqualTo(String.Format(PdfException.RoleInNamespaceIsNotMappedToAnyStandardRole, "p", "http://iso.org/pdf2/ssn"))) ; }