private void DrawSimulatedDiv(PdfPage page, IDictionary <int, Object> styles, float[] margins, bool drawBackground , int pageNumber, bool recalculateBodyAreaForContentSize) { Div pageBordersSimulation = new Div().SetFillAvailableArea(true); foreach (KeyValuePair <int, Object> entry in styles) { if ((entry.Key == Property.BACKGROUND || entry.Key == Property.BACKGROUND_IMAGE) && !drawBackground) { continue; } pageBordersSimulation.SetProperty(entry.Key, entry.Value); } pageBordersSimulation.GetAccessibilityProperties().SetRole(StandardRoles.ARTIFACT); Rectangle backgroundArea = new Rectangle(page.GetTrimBox()).ApplyMargins(margins[0], margins[1], margins[2 ], margins[3], false); if (recalculateBodyAreaForContentSize) { if (pageStylesPropertiesMap.Get(pageNumber).lowestAndHighest == null) { return; } HtmlBodyStylesApplierHandler.LowestAndHighest lowestAndHighest = pageStylesPropertiesMap.Get(pageNumber).lowestAndHighest; RecalculateBackgroundAreaForBody(backgroundArea, pageBordersSimulation, lowestAndHighest); } if (pdfCanvas == null) { pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), page.GetDocument()); } iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, backgroundArea); canvas.EnableAutoTagging(page); canvas.Add(pageBordersSimulation); canvas.Close(); }
public virtual void CheckIfOcgForPdfA2Works() { String fileName = "createdOcgPdfA.pdf"; Stream colorStream = new FileStream(sourceFolder + "color/sRGB_CS_profile.icm", FileMode.Open, FileAccess.Read ); String outFileName = destinationFolder + fileName; String cmpFileName = sourceFolder + "cmp/PdfA2LayoutOcgTest/cmp_" + fileName; PdfDocument pdfDoc = new PdfADocument(new PdfWriter(outFileName), PdfAConformanceLevel.PDF_A_2A, new PdfOutputIntent ("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", colorStream)); pdfDoc.SetTagged(); pdfDoc.GetCatalog().SetLang(new PdfString("en-US")); pdfDoc.AddNewPage(); iText.Layout.Element.Image image1 = new Image(ImageDataFactory.Create(sourceFolder + "images/manualTransparency_for_png.png" )); PdfCanvas pdfCanvas = new PdfCanvas(pdfDoc, 1); iText.Layout.Canvas canvas1 = new iText.Layout.Canvas(pdfCanvas, pdfDoc, new Rectangle(0, 0, 590, 420)); PdfLayer imageLayer1 = new PdfLayer("*[email protected]", pdfDoc); imageLayer1.SetOn(true); pdfCanvas.BeginLayer(imageLayer1); canvas1.Add(image1); pdfCanvas.EndLayer(); canvas1.Close(); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff01_")); }
/// <summary>Draws page border.</summary> /// <param name="page">the page</param> private void DrawPageBorders(PdfPage page) { if (pageBordersSimulation == null) { return; } iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(page), page.GetTrimBox()); canvas.EnableAutoTagging(page); canvas.Add(pageBordersSimulation); canvas.Close(); }
/// <summary>Draws page background and borders.</summary> /// <param name="page">the page</param> private void DrawPageBackgroundAndBorders(PdfPage page) { iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(page), page.GetBleedBox()); canvas.EnableAutoTagging(page); canvas.Add(pageBackgroundSimulation); canvas.Close(); canvas = new iText.Layout.Canvas(new PdfCanvas(page), page.GetTrimBox()); canvas.EnableAutoTagging(page); canvas.Add(pageBordersSimulation); canvas.Close(); }
/// <summary>Draws page background.</summary> /// <param name="page">the page</param> /// <returns>pdfCanvas instance if there was a background to draw, otherwise returns null</returns> internal virtual PdfCanvas DrawPageBackground(PdfPage page) { PdfCanvas pdfCanvas = null; if (pageBackgroundSimulation != null) { pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), page.GetDocument()); iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, page.GetBleedBox()); canvas.EnableAutoTagging(page); canvas.Add(pageBackgroundSimulation); canvas.Close(); } return(pdfCanvas); }
public virtual void OverflowVisibleOnCanvasTest02() { String outFileName = destinationFolder + "overflowVisibleOnCanvasTest02.pdf"; String cmpFileName = sourceFolder + "cmp_overflowVisibleOnCanvasTest02.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName)); PdfPage page = pdfDocument.AddNewPage(); iText.Layout.Canvas canvas = new iText.Layout.Canvas(page, page.GetPageSize().Clone().ApplyMargins(36, 36, 36, 36, false)); AddParaWithImgSetOverflowX(canvas, OverflowPropertyValue.VISIBLE); canvas.Close(); pdfDocument.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
public virtual void CanvasWithPageLinkTest() { String testName = "canvasWithPageLinkTest"; String @out = destinationFolder + testName + ".pdf"; String cmp = sourceFolder + "cmp_" + testName + ".pdf"; PdfDocument pdf = new PdfDocument(new PdfWriter(@out)); PdfPage page = pdf.AddNewPage(); Rectangle pageSize = page.GetPageSize(); Rectangle rectangle = new Rectangle(pageSize.GetX() + 36, pageSize.GetTop() - 80, pageSize.GetWidth() - 72 , 50); iText.Layout.Canvas canvas = new iText.Layout.Canvas(page, rectangle); canvas.Add(new Paragraph(new Link("Google link!", PdfAction.CreateURI("https://www.google.com")).SetUnderline ().SetFontColor(ColorConstants.BLUE))); canvas.Close(); pdf.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder)); }
public virtual void ParentElemWithAbsolPositionKidNotSuitCanvasTest() { String testName = "parentElemWithAbsolPositionKidNotSuitCanvas"; String @out = destinationFolder + testName + ".pdf"; String cmp = sourceFolder + "cmp_" + testName + ".pdf"; using (PdfDocument pdf = new PdfDocument(new PdfWriter(@out))) { pdf.AddNewPage(); iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(pdf.GetFirstPage()), new Rectangle(120, 650, 55, 80)); Div notFittingDiv = new Div().SetWidth(100).Add(new Paragraph("Paragraph in Div with Not set position")); canvas.Add(notFittingDiv); Div divWithPosition = new Div().SetFixedPosition(120, 300, 80); divWithPosition.Add(new Paragraph("Paragraph in Div with set position")); canvas.Add(divWithPosition); canvas.Close(); } NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder)); }
public virtual void CanvasWithPageEnableTaggingTest02() { String testName = "canvasWithPageEnableTaggingTest02"; String @out = destinationFolder + testName + ".pdf"; String cmp = sourceFolder + "cmp_" + testName + ".pdf"; PdfDocument pdf = new PdfDocument(new PdfWriter(@out)); pdf.SetTagged(); PdfPage page = pdf.AddNewPage(); Rectangle pageSize = page.GetPageSize(); Rectangle rectangle = new Rectangle(pageSize.GetX() + 36, pageSize.GetTop() - 80, pageSize.GetWidth() - 72 , 50); iText.Layout.Canvas canvas = new iText.Layout.Canvas(page, rectangle); // This will disable tagging and also prevent annotations addition. Created tagged document is invalid. Expected log message. canvas.EnableAutoTagging(null); canvas.Add(new Paragraph(new Link("Google link!", PdfAction.CreateURI("https://www.google.com")).SetUnderline ().SetFontColor(ColorConstants.BLUE))); canvas.Close(); pdf.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder)); }
public virtual void NestedElementWithAbsolutePositioningInCanvasTest() { NUnit.Framework.Assert.That(() => { //TODO: DEVSIX-4820 (NullPointerException on processing absolutely positioned elements in small canvas area) String testName = "nestedElementWithAbsolutePositioningInCanvas"; String @out = destinationFolder + testName + ".pdf"; String cmp = sourceFolder + "cmp_" + testName + ".pdf"; using (PdfDocument pdf = new PdfDocument(new PdfWriter(@out))) { pdf.AddNewPage(); iText.Layout.Canvas canvas = new iText.Layout.Canvas(new PdfCanvas(pdf.GetFirstPage()), new Rectangle(120, 650, 55, 80)); Div notFittingDiv = new Div().SetWidth(100).Add(new Paragraph("Paragraph in Div with Not set position")); Div divWithPosition = new Div().SetFixedPosition(50, 20, 80); divWithPosition.Add(new Paragraph("Paragraph in Div with set position")); notFittingDiv.Add(divWithPosition); canvas.Add(notFittingDiv); canvas.Close(); } NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder)); } , NUnit.Framework.Throws.InstanceOf <NullReferenceException>()) ; }