private void TestFile(String filename, String objectid, String expectedImageFormat) { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + filename)); try { PdfResources resources = pdfDocument.GetPage(1).GetResources(); PdfDictionary xobjects = resources.GetResource(PdfName.XObject); PdfObject obj = xobjects.Get(new PdfName(objectid)); if (obj == null) { throw new ArgumentException("Reference " + objectid + " not found - Available keys are " + xobjects.KeySet ()); } PdfImageXObject img = new PdfImageXObject((PdfStream)(obj.IsIndirectReference() ? ((PdfIndirectReference)obj ).GetRefersTo() : obj)); NUnit.Framework.Assert.AreEqual(expectedImageFormat, img.IdentifyImageFileExtension()); byte[] result = img.GetImageBytes(true); byte[] cmpBytes = File.ReadAllBytes(Path.Combine(sourceFolder, filename.JSubstring(0, filename.Length - 4) + "." + expectedImageFormat)); if (img.IdentifyImageFileExtension().Equals("tif")) { CompareTiffImages(cmpBytes, result); } else { NUnit.Framework.Assert.AreEqual(cmpBytes, result); } } finally { pdfDocument.Close(); } }
private void TestSingleImage(String imgName) { String @out = destinationFolder + imgName.JSubstring(0, imgName.Length - 4) + ".pdf"; String cmp = sourceFolder + "cmp_" + imgName.JSubstring(0, imgName.Length - 4) + ".pdf"; ImageData img = ImageDataFactory.Create(sourceFolder + imgName); PdfDocument pdfDocument = new PdfDocument(new PdfWriter(@out)); PdfImageXObject imageXObject = new PdfImageXObject(img); new PdfCanvas(pdfDocument.AddNewPage(new PageSize(img.GetWidth(), img.GetHeight()))).AddXObject(imageXObject , 0, 0, img.GetWidth()); pdfDocument.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder, "diff_")); }
public ImagePdfBytesInfo(PdfImageXObject imageXObject) { pngColorType = -1; bpc = imageXObject.GetPdfObject().GetAsNumber(PdfName.BitsPerComponent).IntValue(); pngBitDepth = bpc; palette = null; icc = null; stride = 0; width = (int)imageXObject.GetWidth(); height = (int)imageXObject.GetHeight(); colorspace = imageXObject.GetPdfObject().Get(PdfName.ColorSpace); decode = imageXObject.GetPdfObject().GetAsArray(PdfName.Decode); FindColorspace(colorspace, true); }
public virtual void AddFlushedImageXObjectToCanvas() { String filename = destinationFolder + "addFlushedImageXObjectToCanvas.pdf"; String cmpfile = sourceFolder + "cmp_addFlushedImageXObjectToCanvas.pdf"; String image = sourceFolder + "image.png"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filename)); PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(image)); // flushing pdf object directly imageXObject.GetPdfObject().MakeIndirect(pdfDoc).Flush(); PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage()); canvas.AddXObject(imageXObject, 50, 500, 200); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpfile, destinationFolder)); }
private void ConvertAndCompare(String outFilename, String cmpFilename, String imageFilename) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFilename)); PdfDocument cmpDoc = new PdfDocument(new PdfReader(cmpFilename)); PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(imageFilename)); PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage()); canvas.AddXObject(imageXObject, 50, 500, 346); pdfDoc.Close(); PdfDocument outDoc = new PdfDocument(new PdfReader(outFilename)); PdfStream outStream = outDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName ("Im1")); PdfStream cmpStream = cmpDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName ("Im1")); NUnit.Framework.Assert.IsNull(new CompareTool().CompareStreamsStructure(outStream, cmpStream)); cmpDoc.Close(); outDoc.Close(); }
public virtual void CompareColorspacesTest() { String[] imgFiles = new String[] { "adobe.png", "anon.gif", "anon.jpg", "anon.png", "gamma.png", "odd.png" , "rec709.jpg", "srgb.jpg", "srgb.png" }; String @out = destinationFolder + "compareColorspacesTest.pdf"; String cmp = sourceFolder + "cmp_compareColorspacesTest.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfWriter(@out)); PdfCanvas canvas = new PdfCanvas(pdfDocument.AddNewPage()); canvas.BeginText().MoveText(40, 730).SetFontAndSize(PdfFontFactory.CreateFont(), 12).ShowText("The images below are in row and expected to form four continuous lines of constant colors." ).EndText(); for (int i = 0; i < imgFiles.Length; i++) { String imgFile = imgFiles[i]; PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "compare_colorspaces/" + imgFile)); canvas.AddXObject(imageXObject, 50 + i * 40, 550, 40); } pdfDocument.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder, "diff_")); }
/// <summary> /// Calculates a rectangle with the specified coordinates and height, and the width is /// calculated in such a way that the original proportions of the xObject do not change. /// </summary> /// <remarks> /// Calculates a rectangle with the specified coordinates and height, and the width is /// calculated in such a way that the original proportions of the xObject do not change. /// <para /> /// To calculate the original width and height of the xObject, the BBox and Matrix fields /// are used. For mor information see paragraph 8.10.1 in ISO-32000-1. /// </remarks> /// <param name="xObject">the xObject for which we are calculating the rectangle</param> /// <param name="x">the x-coordinate of the lower-left corner of the rectangle</param> /// <param name="y">the y-coordinate of the lower-left corner of the rectangle</param> /// <param name="height">the height of the rectangle</param> /// <returns>the rectangle with specified coordinates and height</returns> public static Rectangle CalculateProportionallyFitRectangleWithHeight(iText.Kernel.Pdf.Xobject.PdfXObject xObject, float x, float y, float height) { if (xObject is PdfFormXObject) { PdfFormXObject formXObject = (PdfFormXObject)xObject; Rectangle bBox = PdfFormXObject.CalculateBBoxMultipliedByMatrix(formXObject); return(new Rectangle(x, y, (height / bBox.GetHeight()) * bBox.GetWidth(), height)); } else { if (xObject is PdfImageXObject) { PdfImageXObject imageXObject = (PdfImageXObject)xObject; return(new Rectangle(x, y, (height / imageXObject.GetHeight()) * imageXObject.GetWidth(), height)); } else { throw new ArgumentException("PdfFormXObject or PdfImageXObject expected."); } } }
public virtual void ImageBinaryTransparencySameColorTest() { // See http://stackoverflow.com/questions/39119776/itext-binary-transparency-bug String outFile = destinationFolder + "imageBinaryTransparencySameColorTest.pdf"; String cmpFile = sourceFolder + "cmp_imageBinaryTransparencySameColorTest.pdf"; ImageData bkgnd = ImageDataFactory.Create(sourceFolder + "itext.jpg"); PdfImageXObject image = new PdfImageXObject(bkgnd); PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile)); PdfCanvas canvas = new PdfCanvas(pdfDocument.AddNewPage()); PdfPage firstPage = pdfDocument.GetFirstPage(); canvas.AddXObject(image, firstPage.GetPageSize()); canvas .BeginText() .SetTextMatrix(36, 790) .SetFontAndSize(PdfFontFactory.CreateFont(), 12) .ShowText("Invisible image (both opaque and non opaque pixels have the same color)") .EndText(); canvas.AddXObject(new PdfImageXObject( ImageDataFactory.Create(CreateBinaryTransparentAWTImage(Color.Black, false, null), null)), 36, 580); PdfDocument cmpDoc = new PdfDocument(new PdfReader(cmpFile)); // In general case this code will probably will fail, however in this particular case we know the structure of the pdf PdfStream outStream = firstPage.GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName("Im1")); PdfStream cmpStream = cmpDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName("Im1")); NUnit.Framework.Assert.IsNull(new CompareTool().CompareStreamsStructure(outStream, cmpStream)); cmpDoc.Close(); pdfDocument.Close(); PrintOutputPdfNameAndDir(outFile); }