public virtual void ImageRelativePositionTest() { String outFileName = destinationFolder + "imageRelativePositionTest.pdf"; String cmpFileName = sourceFolder + "cmp_imageRelativePositionTest.pdf"; PdfWriter writer = new PdfWriter(outFileName); PdfDocument pdfDoc = new PdfDocument(writer); Document doc = new Document(pdfDoc); PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "Desert.jpg")); iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject, 100).SetRelativePosition(30, 30 , 0, 0); Paragraph p = new Paragraph(); p.SetBorder(new SolidBorder(ColorConstants.GREEN, 5)); p.Add(image); image.SetBorder(new SolidBorder(ColorConstants.BLUE, 5)); doc.Add(p); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
public virtual void ImageTest02() { String outFileName = destinationFolder + "imageTest02.pdf"; String cmpFileName = sourceFolder + "cmp_imageTest02.pdf"; PdfWriter writer = new PdfWriter(outFileName); PdfDocument pdfDoc = new PdfDocument(writer); Document doc = new Document(pdfDoc); PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.CreateJpeg(UrlUtil.ToURL(sourceFolder + "Desert.jpg" ))); iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject, 100); Paragraph p = new Paragraph(); p.Add(new Text("before image")); p.Add(image); p.Add(new Text("after image")); doc.Add(p); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
public virtual void HandleEvent(Event currentEvent) { if (MainWindow.activeWorkspace.LogoImg == null) { return; } PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent; PdfDocument pdf = docEvent.GetDocument(); PdfPage page = docEvent.GetPage(); Rectangle pageSize = page.GetPageSize().ApplyMargins(10, 30, 0, 30, false); Canvas canvas = new Canvas(new PdfCanvas(page), pdf, pageSize); canvas.SetHorizontalAlignment(HorizontalAlignment.LEFT); var logo = new Image(ImageDataFactory.Create(MainWindow.activeWorkspace.LogoImg)); canvas.Add(logo.SetMaxHeight(50)); canvas.Close(); }
public virtual void ImageTest06() { String outFileName = destinationFolder + "imageTest06.pdf"; String cmpFileName = sourceFolder + "cmp_imageTest06.pdf"; PdfWriter writer = new PdfWriter(outFileName); PdfDocument pdfDoc = new PdfDocument(writer); Document doc = new Document(pdfDoc); PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "Desert.jpg")); iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject, 100); doc.Add(new Paragraph(new Text("First Line"))); Paragraph p = new Paragraph(); p.Add(image); image.SetMarginLeft(100).SetMarginTop(100); doc.Add(p); doc.Add(new Paragraph(new Text("Second Line"))); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
public virtual void CreatePdf(String dest) { //Initialize PDFA document with output intent PdfADocument pdf = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent ("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(INTENT, FileMode.Open, FileAccess.Read ))); Document document = new Document(pdf); //Fonts need to be embedded PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.WINANSI, true); Paragraph p = new Paragraph(); p.SetFont(font); p.Add(new Text("The quick brown ")); iText.Layout.Element.Image foxImage = new Image(ImageDataFactory.Create(FOX)); p.Add(foxImage); p.Add(" jumps over the lazy "); iText.Layout.Element.Image dogImage = new iText.Layout.Element.Image(ImageDataFactory.Create(DOG)); p.Add(dogImage); document.Add(p); document.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_")); }
public virtual void FlushingTest01() { String outPdf = destinationFolder + "pdfA1b_flushingTest01.pdf"; String cmpPdf = sourceFolder + "cmp/PdfAFlushingTest/cmp_pdfA1b_flushingTest01.pdf"; PdfWriter writer = new PdfWriter(outPdf); Stream @is = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read); PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent("Custom", "" , "http://www.color.org", "sRGB IEC61966-2.1", @is)); PdfCanvas canvas = new PdfCanvas(doc.AddNewPage()); PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "Desert.jpg")); imageXObject.MakeIndirect(doc); canvas.AddXObject(imageXObject, new Rectangle(30, 300, 300, 300)); imageXObject.Flush(); if (imageXObject.IsFlushed()) { NUnit.Framework.Assert.Fail("Flushing of unchecked objects shall be forbidden."); } doc.Close(); CompareResult(outPdf, cmpPdf); }
public virtual void AlignedInlineContentOverflowHiddenTest02() { String outFileName = destinationFolder + "alignedInlineContentOverflowHiddenTest02.pdf"; String cmpFileName = sourceFolder + "cmp_alignedInlineContentOverflowHiddenTest02.pdf"; String imgPath = sourceFolder + "itis.jpg"; PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName)); Document document = new Document(pdfDocument); iText.Layout.Element.Image img = new iText.Layout.Element.Image(ImageDataFactory.Create(imgPath)); Paragraph p = new Paragraph().SetTextAlignment(TextAlignment.CENTER).SetHeight(150f).SetWidth(150f).SetBorder (new SolidBorder(5f)); p.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.HIDDEN); p.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.HIDDEN); img.SetProperty(Property.OVERFLOW_X, OverflowPropertyValue.VISIBLE); img.SetProperty(Property.OVERFLOW_Y, OverflowPropertyValue.VISIBLE); document.Add(p.Add(img)); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
public virtual void TestScaleHeightMode() { String testName = "testScaleHeightMode"; String path = PdfHelper.GetDefaultImagePath(); String pdfPath = PdfHelper.GetTargetDirectory() + testName + ".pdf"; FileInfo file = new FileInfo(path); float pageWidthPt = 400f; float pageHeightPt = 400f; Rectangle pageSize = new Rectangle(pageWidthPt, pageHeightPt); OcrPdfCreatorProperties properties = new OcrPdfCreatorProperties(); properties.SetScaleMode(ScaleMode.SCALE_HEIGHT); properties.SetPageSize(pageSize); PdfHelper.CreatePdf(pdfPath, file, properties); Rectangle rect = GetImageBBoxRectangleFromPdf(pdfPath); ImageData originalImageData = ImageDataFactory.Create(file.FullName); NUnit.Framework.Assert.AreEqual(pageWidthPt, rect.GetWidth(), DELTA); NUnit.Framework.Assert.AreEqual(originalImageData.GetWidth() / originalImageData.GetHeight(), rect.GetWidth () / rect.GetHeight(), DELTA); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); PageSize pageSize = PageSize.A4.Rotate(); Document doc = new Document(pdfDoc, pageSize); ImageData image = ImageDataFactory.Create(IMAGE); ImageData mask = ConvertToBlackAndWhitePng(MASK); mask.MakeMask(); image.SetImageMask(mask); /* For soft image masks grayscale 8 bit images are usually used. As described in * the ConvertToBlackAndWhitePng, for image masks actual image colorspace doesn't * matter, only image pixel values are important. Grayscale images are just * convenient for this purpose. * Here we adjust SMask image dictionary, in order to accommodate to the hacks * that we've performed in ConvertToBlackAndWhitePng method. There we've created * an image in indexed color space and by default C# will add transparency to the * image. Both these properties are forbidden for images that serve as masks. * That's why we explicitly override colorspace to /DeviceGray (which corresponds * to grayscale) and we just erase own transparency (/Mask) from the dictionary * of mask-image. */ PdfImageXObject imageXObject = new PdfImageXObject(image); PdfStream imageXObjectStream = imageXObject.GetPdfObject().GetAsStream(PdfName.SMask); imageXObjectStream.Put(PdfName.ColorSpace, PdfName.DeviceGray); // Remove a redundant submask imageXObjectStream.Remove(PdfName.Mask); Image img = new Image(imageXObject); img.ScaleAbsolute(pageSize.GetWidth(), pageSize.GetHeight()); img.SetFixedPosition(0, 0); doc.Add(img); doc.Close(); }
static void GetPDF(List <string> imgurls, string bookname, string authorname) { bookname = MakeValidFileName(bookname).Trim(); authorname = MakeValidFileName(authorname).Trim(); Console.WriteLine("Downloading: " + bookname); string filename = bookname + " - " + authorname + ".pdf"; if (!File.Exists(filename)) { using (var client = new WebClient()) { int pagenum = 1; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(bookname + " - " + authorname + ".pdf")); foreach (var img in imgurls) { Console.Write("\rPage: " + (pagenum)); byte[] data = client.DownloadData(img); Image image = new Image(ImageDataFactory.Create(data)); Document doc = new Document(pdfDoc, new PageSize(image.GetImageWidth(), image.GetImageHeight())); image = new Image(ImageDataFactory.Create(data)); pdfDoc.AddNewPage(new PageSize(image.GetImageWidth(), image.GetImageHeight())); image.SetFixedPosition(pagenum, 0, 0); doc.Add(image); pagenum++; } pdfDoc.Close(); Console.WriteLine("\nFinished: " + bookname + " - " + authorname); } } else { Console.WriteLine(filename + " - Already exists!"); } }
private void ManipulatePdf(String dest) { PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H); Stream fileStream = new FileStream("../../../resources/data/sRGB_CS_profile.icm", FileMode.Open, FileAccess.Read); PdfADocument pdfDoc = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_1A, new PdfOutputIntent("Custom", "", null, "sRGB IEC61966-2.1", fileStream)); pdfDoc.GetCatalog().SetLang(new PdfString("nl-nl")); pdfDoc.SetTagged(); Document doc = new Document(pdfDoc); doc.SetMargins(MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM, MARGIN_OF_ONE_CM); PdfDocumentInfo info = pdfDoc.GetDocumentInfo(); info .SetTitle("title") .SetAuthor("Author") .SetSubject("Subject") .SetCreator("Creator") .SetKeywords("Metadata, iText, PDF") .SetCreator("My program using iText") .AddCreationDate(); Paragraph element = new Paragraph("Hello World").SetFont(font).SetFontSize(10); doc.Add(element); Image logoImage = new Image(ImageDataFactory.Create(LOGO)); logoImage.GetAccessibilityProperties().SetAlternateDescription("Logo"); doc.Add(logoImage); doc.Close(); }
private static void AddFloatingImagesInDivs(Document document, UnitValue width, ClearPropertyValue?clearValue ) { IList <FloatBlockTest.ImageProperties> imagePropertiesList = new List <FloatBlockTest.ImageProperties>(); imagePropertiesList.Add(new FloatBlockTest.ImageProperties(FloatPropertyValue.LEFT, clearValue, null, width )); imagePropertiesList.Add(new FloatBlockTest.ImageProperties(FloatPropertyValue.RIGHT, clearValue, null, width )); imagePropertiesList.Add(new FloatBlockTest.ImageProperties(FloatPropertyValue.NONE, clearValue, HorizontalAlignment .CENTER, width)); imagePropertiesList.Add(new FloatBlockTest.ImageProperties(FloatPropertyValue.LEFT, clearValue, null, width )); document.Add(new Paragraph("Four images followed by two paragraphs. All images are wrapped in Divs.\n" + "All images specify WIDTH = " + width)); for (int i = 0; i < imagePropertiesList.Count; i++) { document.Add(new Paragraph("Image " + (i + 1) + ": " + imagePropertiesList[i])); } for (int i = 0; i < imagePropertiesList.Count; i++) { iText.Layout.Element.Image image = new iText.Layout.Element.Image(ImageDataFactory.Create(MessageFormatUtil .Format(sourceFolder + "{0}.png", i + 1))); image.SetBorder(new SolidBorder(1f)); image.SetWidth(width); Div div = new Div(); div.SetProperty(Property.CLEAR, clearValue); div.SetProperty(Property.FLOAT, imagePropertiesList[i].floatPropertyValue); div.Add(image); document.Add(div); } document.Add(new Paragraph("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor..." )); document.Add(new Paragraph("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex " + "ea commodo consequat. Duis aute irure dolor in reprehenderit in " + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur" + " sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt" + " mollit anim id est laborum.\n" + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod" + " tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " + "commodo consequat. Duis aute irure dolor in ......")); }
public virtual void ImagesWithDifferentDepth() { String outFileName = destinationFolder + "transparencyTest01.pdf"; String cmpFileName = sourceFolder + "cmp_transparencyTest01.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName, new WriterProperties().SetCompressionLevel (CompressionConstants.NO_COMPRESSION))); PdfPage page = pdfDocument.AddNewPage(PageSize.A3); PdfCanvas canvas = new PdfCanvas(page); canvas.SetFillColor(ColorConstants.LIGHT_GRAY).Fill(); canvas.Rectangle(80, 0, 700, 1200).Fill(); canvas.SaveState().BeginText().MoveText(116, 1150).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts. HELVETICA), 14).SetFillColor(ColorConstants.MAGENTA).ShowText("8 bit depth PNG").EndText().RestoreState (); ImageData img = ImageDataFactory.Create(sourceFolder + "manualTransparency_8bit.png"); canvas.AddImage(img, 100, 780, 200, false); canvas.SaveState().BeginText().MoveText(316, 1150).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts. HELVETICA), 14).SetFillColor(ColorConstants.MAGENTA).ShowText("24 bit depth PNG").EndText().RestoreState (); img = ImageDataFactory.Create(sourceFolder + "manualTransparency_24bit.png"); canvas.AddImage(img, 300, 780, 200, false); canvas.SaveState().BeginText().MoveText(516, 1150).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts. HELVETICA), 14).SetFillColor(ColorConstants.MAGENTA).ShowText("32 bit depth PNG").EndText().RestoreState (); img = ImageDataFactory.Create(sourceFolder + "manualTransparency_32bit.png"); canvas.AddImage(img, 500, 780, 200, false); canvas.SaveState().BeginText().MoveText(116, 650).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA ), 16).SetFillColor(ColorConstants.MAGENTA).ShowText("GIF image ").EndText().RestoreState(); img = ImageDataFactory.Create(sourceFolder + "manualTransparency_gif.gif"); canvas.AddImage(img, 100, 300, 200, false); canvas.SaveState().BeginText().MoveText(316, 650).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA ), 16).SetFillColor(ColorConstants.MAGENTA).ShowText("TIF image ").EndText().RestoreState(); img = ImageDataFactory.Create(sourceFolder + "manualTransparency_tif.tif"); canvas.AddImage(img, 300, 300, 200, false); canvas.Release(); pdfDocument.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff_")); }
protected void SignDocumentSignature(string filePath, ElectronicSignatureInfoDTO signatureInfo) { PdfSigner pdfSigner = new PdfSigner(new PdfReader(SRC), new FileStream(filePath, FileMode.Create), new StampingProperties()); pdfSigner.SetCertificationLevel(PdfSigner.CERTIFIED_NO_CHANGES_ALLOWED); // Set the name indicating the field to be signed. // The field can already be present in the document but shall not be signed pdfSigner.SetFieldName("signature"); ImageData clientSignatureImage = ImageDataFactory.Create(IMAGE_PATH); // If you create new signature field (or use SetFieldName(System.String) with // the name that doesn't exist in the document or don't specify it at all) then // the signature is invisible by default. PdfSignatureAppearance signatureAppearance = pdfSigner.GetSignatureAppearance(); signatureAppearance.SetRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC); signatureAppearance.SetReason(""); signatureAppearance.SetLocationCaption(""); signatureAppearance.SetSignatureGraphic(clientSignatureImage); signatureAppearance.SetPageNumber(signatureInfo.PageNumber); signatureAppearance.SetPageRect(new Rectangle(signatureInfo.Left, signatureInfo.Bottom, 25, 25)); char[] password = "******".ToCharArray(); IExternalSignature pks = GetPrivateKeySignature(CERT_PATH, password); X509Certificate[] chain = GetCertificateChain(CERT_PATH, password); OCSPVerifier ocspVerifier = new OCSPVerifier(null, null); OcspClientBouncyCastle ocspClient = new OcspClientBouncyCastle(ocspVerifier); List <ICrlClient> crlClients = new List <ICrlClient>(new[] { new CrlClientOnline() }); // Sign the document using the detached mode, CMS or CAdES equivalent. // This method closes the underlying pdf document, so the instance // of PdfSigner cannot be used after this method call pdfSigner.SignDetached(pks, chain, crlClients, ocspClient, null, 0, PdfSigner.CryptoStandard.CMS); }
public void Sign3(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); signer.SetFieldName(name); // Set a custom text and background image appearance.SetLayer2Text("This document was signed by Bruno Specimen"); appearance.SetImage(ImageDataFactory.Create(IMG)); appearance.SetImageScale(1); PrivateKeySignature pks = new PrivateKeySignature(pk, digestAlgorithm); signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public virtual void ImageTest20() { String outFileName = destinationFolder + "imageTest20.pdf"; String cmpFileName = sourceFolder + "cmp_imageTest20.pdf"; PdfWriter writer = new PdfWriter(outFileName); PdfDocument pdfDoc = new PdfDocument(writer); Document doc = new Document(pdfDoc); iText.Layout.Element.Image image = new iText.Layout.Element.Image(ImageDataFactory.Create(sourceFolder + "Desert.jpg" )); image.SetAutoScaleWidth(true); Div container = new Div(); container.SetBorder(new SolidBorder(1f)); container.SetWidth(UnitValue.CreatePercentValue(60f)); container.SetHeight(UnitValue.CreatePointValue(300f)); container.Add(image); doc.Add(container); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
private void SetLogo(PdfFormField toSet, iText.Kernel.Pdf.PdfDocument pdfDoc, string filename, int pagina) { var b = toSet as PdfButtonFormField; var afmetingen = b.GetWidgets().SelectMany(f => f.GetRectangle()).ToArray(); var x = (int)Convert.ToDouble(afmetingen[0].ToString().Replace(".", ",")); if (x < 10) { x = 100; } var y = (int)Convert.ToDouble(afmetingen[1].ToString().Replace(".", ",")); var wWidth = (int)Convert.ToDouble(afmetingen[2].ToString().Replace(".", ",")); var pageWidth = (int)pdfDoc.GetPage(1).GetPageSizeWithRotation().GetWidth(); if (wWidth > pageWidth - 20) { wWidth = pageWidth - 20; } var wHeight = (int)Convert.ToDouble(afmetingen[3].ToString().Replace(".", ",")); if (pagina == 1) { wHeight -= 10; } ImageData img = ImageDataFactory.Create(filename); var pdfImage = new iText.Layout.Element.Image(img); var scaled = pdfImage.ScaleToFit(wWidth, wHeight - y); var scaledWidth = scaled.GetImageScaledWidth(); var scaledHeight = scaled.GetImageScaledHeight(); Document d = new Document(pdfDoc); var berekendeX = (x + wWidth - scaledWidth) / 2; var berekendeY = (y + wHeight - scaledHeight) / 2; scaled.SetFixedPosition(pagina, berekendeX, berekendeY); d.Add(scaled); b.SetValue(""); }
//private static string RandNumber(int n) //{ // string result = ""; // var v = new Random(); // for (int i = 0; i < n; i++) // { // var seed = v.Next(0, 9); // result += seed.ToString(); // } // return result; //} //private static string GetTmpFilename(string filePath) //{ // string newFile = RandNumber(8) + ".pdf"; // string dest = Path.GetDirectoryName(filePath); // return Path.Combine(dest, newFile); //} private static string ImgToPdf(string filePath) { if (!File.Exists(filePath)) { Console.WriteLine("文件" + filePath + "不存在"); return(null); } string dest = GetDestFilename(filePath); PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Image image = new Image(ImageDataFactory.Create(filePath)); pdfDoc.AddNewPage(iTextPageSize.A4); Document doc = new Document(pdfDoc, iTextPageSize.A4); doc.Add(image); doc.Close(); return(dest); }
public virtual void InlineImagesTest02() { String filename = "inlineImages02.pdf"; PdfDocument document = new PdfDocument(new PdfWriter(destinationFolder + filename)); PdfPage page = document.AddNewPage(); PdfCanvas canvas = new PdfCanvas(page); Stream stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "Desert.jpg")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamUtil.TransferBytes(stream, baos); canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 700, 100, true); stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "bulb.gif")); baos = new ByteArrayOutputStream(); StreamUtil.TransferBytes(stream, baos); canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 600, 100, true); stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "smpl.bmp")); baos = new ByteArrayOutputStream(); StreamUtil.TransferBytes(stream, baos); canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 500, 100, true); stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "itext.png")); baos = new ByteArrayOutputStream(); StreamUtil.TransferBytes(stream, baos); canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 460, 100, true); stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "0047478.jpg")); baos = new ByteArrayOutputStream(); StreamUtil.TransferBytes(stream, baos); canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 300, 100, true); stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "map.jp2")); baos = new ByteArrayOutputStream(); StreamUtil.TransferBytes(stream, baos); canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 200, 100, true); stream = UrlUtil.OpenStream(UrlUtil.ToURL(sourceFolder + "amb.jb2")); baos = new ByteArrayOutputStream(); StreamUtil.TransferBytes(stream, baos); canvas.AddImage(ImageDataFactory.Create(baos.ToArray()), 36, 30, 100, true); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder + "cmp_" + filename, destinationFolder)); }
/// <exception cref="System.UriFormatException"/> private void AddContentToDocInCustomNs(PdfDocument pdfDocument, PdfNamespace defaultNamespace, PdfNamespace xhtmlNs, PdfNamespace html4Ns, String hnRole, Document document) { Paragraph h1P = new Paragraph("Header level 1"); h1P.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.h1); Paragraph helloWorldPara = new Paragraph("Hello World from iText7"); helloWorldPara.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.p); iText.Layout.Element.Image img = new iText.Layout.Element.Image(ImageDataFactory.Create(sourceFolder + imageName )).SetWidth(100); img.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.img); document.Add(h1P); document.Add(helloWorldPara); document.Add(img); pdfDocument.GetTagStructureContext().GetAutoTaggingPointer().SetNamespaceForNewTags(defaultNamespace); 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(new ListItem("list item")); document.Add(list); Paragraph center = new Paragraph("centered text").SetTextAlignment(TextAlignment.CENTER); center.GetAccessibilityProperties().SetRole(LayoutTaggingPdf2Test.HtmlRoles.center); center.GetAccessibilityProperties().SetNamespace(html4Ns); document.Add(center); Paragraph h11Para = new Paragraph("Heading level 11"); h11Para.GetAccessibilityProperties().SetRole(hnRole); document.Add(h11Para); if (defaultNamespace == null) { Text i = new Text("italic text"); i.GetAccessibilityProperties().SetRole("I"); Paragraph pi = new Paragraph(i.SetItalic()); document.Add(pi); } }
public virtual void ImageTest04() { String outFileName = destinationFolder + "imageTest04.pdf"; String cmpFileName = sourceFolder + "cmp_imageTest04.pdf"; FileStream file = new FileStream(outFileName, FileMode.Create); PdfWriter writer = new PdfWriter(file); PdfDocument pdfDoc = new PdfDocument(writer); Document doc = new Document(pdfDoc); PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "Desert.jpg")); iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject, 100); Paragraph p = new Paragraph(); p.Add(new Text("before image")); p.Add(image); image.SetRotationAngle(Math.PI / 6); p.Add(new Text("after image")); doc.Add(p); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
private void saveButton_Click(object sender, EventArgs e) { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { string filePath = saveFileDialog1.FileName; using (PdfWriter writer = new PdfWriter(filePath)) { using (PdfDocument pdf = new PdfDocument(writer)) { Document document = new Document(pdf); var memoryStream = new MemoryStream(); chart1.SaveImage(memoryStream, ChartImageFormat.Png); ImageData imageData = ImageDataFactory.Create(memoryStream.GetBuffer()); Image img = new Image(imageData); document.Add(img); document.Close(); } } } }
private static void AddContentToPage(PdfPage pdfPage, PdfFont font, PdfImageXObject xObject) { PdfCanvas canvas = AddBasicContent(pdfPage, font); canvas.SaveState().Rectangle(250, 500, 100, 100).Fill().RestoreState(); PdfFont courier = PdfFontFactory.CreateFont(StandardFonts.COURIER); courier.MakeIndirect(pdfPage.GetDocument()); canvas.SaveState().BeginText().MoveText(36, 650).SetFontAndSize(courier, 16).ShowText("Hello Courier!").EndText ().RestoreState(); canvas.SaveState().Circle(100, 400, 25).Fill().RestoreState(); canvas.SaveState().RoundRectangle(100, 650, 100, 100, 10).Fill().RestoreState(); canvas.SaveState().SetLineWidth(10).RoundRectangle(250, 650, 100, 100, 10).Stroke().RestoreState(); canvas.SaveState().SetLineWidth(5).Arc(400, 650, 550, 750, 0, 180).Stroke().RestoreState(); canvas.SaveState().SetLineWidth(5).MoveTo(400, 550).CurveTo(500, 570, 450, 450, 550, 550).Stroke().RestoreState (); canvas.AddXObject(xObject, 100, 500, 400); PdfImageXObject xObject2 = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "itext.png")); xObject2.MakeIndirect(pdfPage.GetDocument()); canvas.AddXObject(xObject2, 100, 300, 400); }
public virtual void Png_imageTransparancy_8bitDepthImage() { String outFileName = destinationFolder + "png_imageTransparancy_8bitDepthImage.pdf"; String cmpFileName = sourceFolder + "cmp_png_imageTransparancy_8bitDepthImage.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName, new WriterProperties().SetCompressionLevel (CompressionConstants.NO_COMPRESSION))); PdfPage page = pdfDocument.AddNewPage(PageSize.A4); PdfCanvas canvas = new PdfCanvas(page); canvas.SetFillColor(ColorConstants.LIGHT_GRAY).Fill(); canvas.Rectangle(80, 0, PageSize.A4.GetWidth() - 80, PageSize.A4.GetHeight()).Fill(); canvas.SaveState().BeginText().MoveText(116, 800).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA ), 14).SetFillColor(ColorConstants.MAGENTA).ShowText("8 bit depth PNG").MoveText(0, -20).ShowText("This image should not have a black rectangle as background" ).EndText().RestoreState(); ImageData img = ImageDataFactory.Create(sourceFolder + "manualTransparency_8bit.png"); canvas.AddImage(img, 100, 450, 200, false); canvas.Release(); pdfDocument.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff_")); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); PageSize pageSize = PageSize.A4.Rotate(); Document doc = new Document(pdfDoc, pageSize); ImageData image = ImageDataFactory.Create(IMAGE); PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage()); canvas.SaveState(); PdfExtGState state = new PdfExtGState().SetFillOpacity(0.6f); canvas.SetExtGState(state); Rectangle rect = new Rectangle(0, 0, pageSize.GetWidth(), pageSize.GetHeight()); canvas.AddImageFittedIntoRectangle(image, rect, false); canvas.RestoreState(); doc.Add(new Paragraph("Berlin!")); doc.Close(); }
public virtual void ImageInRotatedBlockTest02() { String outFileName = destinationFolder + "imageInRotatedBlockTest02.pdf"; String cmpFileName = sourceFolder + "cmp_imageInRotatedBlockTest02.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName)); Document doc = new Document(pdfDocument); iText.Layout.Element.Image image = new iText.Layout.Element.Image(ImageDataFactory.Create(sourceFolder + "Desert.jpg" )); image.SetWidth(200); Div div = new Div(); div.SetHeight(100); div.SetRotationAngle(Math.PI / 2); div.SetBorder(new SolidBorder(ColorConstants.BLUE, 1)); div.Add(image); doc.Add(div); doc.Add(new Paragraph("Hello!!!").SetBackgroundColor(ColorConstants.RED)); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
public virtual void ImageResizedParentWithHardMaskTest() { String outFileName = destinationFolder + "imageResizedParentWithHardMask.pdf"; String cmpFileName = sourceFolder + "cmp_imageResizedParentWithHardMask.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName)); ImageData mask = ImageDataFactory.Create(sourceFolder + "hardMask.png"); mask.MakeMask(); ImageData img1 = ImageDataFactory.Create(sourceFolder + "sRGBImageBig.png"); img1.SetImageMask(mask); ImageData img2 = ImageDataFactory.Create(sourceFolder + "sRGBImage.png"); img2.SetImageMask(mask); PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage()); canvas.AddImageAt(img1, 30, 500, false); canvas.AddImageAt(img2, 430, 500, false); pdfDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder )); }
public virtual void BlockAlignmentTest02() { String outFileName = destinationFolder + "blockAlignmentTest02.pdf"; String cmpFileName = sourceFolder + "cmp_blockAlignmentTest02.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName)); Document document = new Document(pdfDocument); Div div = new Div(); PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.CreateJpeg(UrlUtil.ToURL(sourceFolder + "Desert.jpg" ))); iText.Layout.Element.Image image1 = new iText.Layout.Element.Image(xObject, 100).SetHorizontalAlignment(HorizontalAlignment .RIGHT); iText.Layout.Element.Image image2 = new iText.Layout.Element.Image(xObject, 100).SetHorizontalAlignment(HorizontalAlignment .CENTER); iText.Layout.Element.Image image3 = new iText.Layout.Element.Image(xObject, 100).SetHorizontalAlignment(HorizontalAlignment .LEFT); div.Add(image1).Add(image2).Add(image3); document.Add(div); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
public virtual void CreateDocumentFromImages1() { String destinationDocument = destinationFolder + "documentFromImages1.pdf"; FileStream fos = new FileStream(destinationDocument, FileMode.Create); PdfWriter writer = new PdfWriter(fos); PdfDocument document = new PdfDocument(writer); PdfImageXObject[] images = new PdfImageXObject[4]; for (int i = 0; i < 4; i++) { images[i] = new PdfImageXObject(ImageDataFactory.Create(PdfXObjectTest.images[i])); images[i].SetLayer(new PdfLayer("layer" + i, document)); if (i % 2 == 0) { images[i].Flush(); } } for (int i_1 = 0; i_1 < 4; i_1++) { PdfPage page = document.AddNewPage(); PdfCanvas canvas = new PdfCanvas(page); canvas.AddXObject(images[i_1], PageSize.Default); page.Flush(); } PdfPage page_1 = document.AddNewPage(); PdfCanvas canvas_1 = new PdfCanvas(page_1); canvas_1.AddXObject(images[0], 0, 0, 200); canvas_1.AddXObject(images[1], 300, 0, 200); canvas_1.AddXObject(images[2], 0, 300, 200); canvas_1.AddXObject(images[3], 300, 300, 200); canvas_1.Release(); page_1.Flush(); document.Close(); NUnit.Framework.Assert.IsTrue(new FileInfo(destinationDocument).Length < 20 * 1024 * 1024); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationDocument, sourceFolder + "cmp_documentFromImages1.pdf" , destinationFolder, "diff_")); }