protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); Document doc = new Document(pdfDoc); PdfPage firstPage = pdfDoc.GetFirstPage(); PdfCanvas canvas = new PdfCanvas(firstPage); canvas .MoveTo(36, 700) .LineTo(72, 760) .LineTo(144, 720) .LineTo(72, 730) .ClosePathStroke(); Rectangle linkLocation = new Rectangle(36, 700, 144, 760); // Make the link destination page fit to the display PdfExplicitDestination destination = PdfExplicitDestination.CreateFit(firstPage); PdfLinkAnnotation linkAnnotation = new PdfLinkAnnotation(linkLocation) // Set highlighting type which is enabled after a click on the annotation .SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT) // Create a link to the first page of the document. .SetAction(PdfAction.CreateGoTo(destination)); PdfArray arrayOfQuadPoints = new PdfArray(new int[] { 72, 730, 144, 720, 72, 760, 36, 700 }); linkAnnotation.Put(PdfName.QuadPoints, arrayOfQuadPoints); firstPage.AddAnnotation(linkAnnotation); doc.Close(); }
public virtual void LinkTest02() { String outFileName = destinationFolder + "linkTest02.pdf"; String cmpFileName = sourceFolder + "cmp_linkTest02.pdf"; FileStream file = new FileStream(outFileName, FileMode.Create); PdfWriter writer = new PdfWriter(file); PdfDocument pdfDoc = new PdfDocument(writer); Document doc = new Document(pdfDoc); doc.Add(new AreaBreak()).Add(new AreaBreak()); PdfArray array = new PdfArray(); array.Add(doc.GetPdfDocument().GetPage(1).GetPdfObject()); array.Add(PdfName.XYZ); array.Add(new PdfNumber(36)); array.Add(new PdfNumber(100)); array.Add(new PdfNumber(1)); PdfDestination dest = PdfDestination.MakeDestination(array); PdfAction action = PdfAction.CreateGoTo(dest); Link link = new Link("TestLink", action); doc.Add(new Paragraph(link)); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
public virtual void CreatePdf(String dest) { // Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); // Initialize document Document document = new Document(pdf); Style style = new Style().SetBold().SetDestination("Top"); Text t1 = new Text("Test").AddStyle(style); document.Add(new Paragraph(t1)); Text t2 = new Text("Test").SetBorder(new SolidBorder(0.5f)); document.Add(new Paragraph(t2)); Text t3 = new Text("Test").SetBorderLeft(new SolidBorder(0.5f)).SetBackgroundColor(ColorConstants.LIGHT_GRAY); document.Add(new Paragraph(t3)); Text t4 = new Text("AWAY AGAIN").SetCharacterSpacing(10); document.Add(new Paragraph(t4)); Text t5 = new Text("AWAY AGAIN").SetWordSpacing(10); document.Add(new Paragraph(t5)); Text t6 = new Text("AWAY AGAIN").SetRelativePosition(-10, 50, 0, 0); document.Add(new Paragraph(t6)); PdfAction top = PdfAction.CreateGoTo("Top"); Text t7 = new Text("go to top").SetAction(top); document.Add(new Paragraph(t7)); document.Close(); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); int[] borders = new int[] { 0, 0, 1 }; PdfDestination pdfDestination = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(10)); Rectangle rect = new Rectangle(0, 806, 595, 36); PdfAnnotation a10 = new PdfLinkAnnotation(rect) .SetAction(PdfAction.CreateGoTo(pdfDestination)) .SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT) .SetPage(pdfDoc.GetPage(10)).SetBorder(new PdfArray(borders)) .SetColor(new PdfArray(new float[] { 0, 1, 0 })); pdfDoc.GetPage(1).AddAnnotation(a10); PdfDestination d2 = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(1)); PdfAnnotation a1 = new PdfLinkAnnotation(rect) .SetAction(PdfAction.CreateGoTo(d2)) .SetHighlightMode(PdfAnnotation.HIGHLIGHT_PUSH) .SetPage(pdfDoc.GetPage(1)) .SetBorder(new PdfArray(borders)) .SetColor(new PdfArray(new float[] { 0, 1, 0 })); pdfDoc.GetPage(10).AddAnnotation(a1); pdfDoc.Close(); }
private void ManipulatePdf(string dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document doc = new Document(pdfDoc); Table table = new Table(UnitValue.CreatePercentArray(1)); table.SetWidth(500); Cell cell = new Cell(); Paragraph p = new Paragraph(); Link link = new Link("link to top of next page", PdfAction.CreateGoTo("top")); p.Add(link); cell.Add(p); table.AddCell(cell); doc.Add(table); doc.Add(new AreaBreak()); // Creates a target that the link leads to Paragraph target = new Paragraph("top"); target.SetDestination("top"); doc.Add(target); doc.Close(); }
public virtual void CreatePdf(String dest) { // Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); // Initialize document Document document = new Document(pdf); PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN); Style style = new Style(); style.SetBackgroundColor(ColorConstants.YELLOW); document.Add(CreateNewTable().AddStyle(style).SetDestination("Top").SetWidth(300).SetHorizontalAlignment(HorizontalAlignment .CENTER)).SetHorizontalAlignment(HorizontalAlignment.CENTER); document.Add(CreateNewTable().SetBorder(new DottedBorder(5)).SetHyphenation(new HyphenationConfig("en", "uk" , 3, 3))); document.Add(CreateNewTable().SetTextAlignment(TextAlignment.CENTER)); document.Add(ListSeparatorProperties.CreateNewSeparator().SetMargin(10).SetWidth(300).SetKeepWithNext(true )); document.Add(CreateNewTable().SetKeepTogether(true).SetWidth(UnitValue.CreatePercentValue(90))); document.Add(CreateNewTable()); document.Add(CreateNewTable().SetRelativePosition(10, 10, 50, 10)); document.Add(CreateNewTable()); document.Add(new AreaBreak()); document.Add(CreateNewTable().SetFixedPosition(100, 400, 350).SetAction(PdfAction.CreateGoTo("Top"))); document.Add(new AreaBreak()); document.Add(CreateNewTable().SetBackgroundColor(ColorConstants.YELLOW).SetMarginBottom(10)); document.Add(CreateNewTable().SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetPaddingLeft(20).SetPaddingRight(50)); document.Add(CreateNewTable().SetBackgroundColor(ColorConstants.YELLOW)); document.Add(CreateNewTable().SetBackgroundColor(ColorConstants.LIGHT_GRAY)); document.Add(CreateNewTable().SetBackgroundColor(ColorConstants.YELLOW)); document.Add(CreateNewTable().SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetMargin(50).SetPadding(30)); document.Add(CreateNewTable().SetBackgroundColor(ColorConstants.YELLOW)); document.Add(CreateNewTable().SetBackgroundColor(ColorConstants.LIGHT_GRAY)); document.Close(); }
public virtual void CreatePdf(String dest) { // Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); Document document = new Document(pdf); iText.Layout.Element.Image img1 = new Image(ImageDataFactory.Create(TEST1)); img1.ScaleToFit(100, 100).SetDestination("Top"); document.Add(img1); iText.Layout.Element.Image img2 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST2)); img2.SetHeight(300); document.Add(img2); iText.Layout.Element.Image img3 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST3)); img3.ScaleToFit(100, 100); img3.SetBackgroundColor(ColorConstants.BLUE); document.Add(img3); iText.Layout.Element.Image img4 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST4)); img4.ScaleToFit(100, 100); img4.SetBackgroundColor(ColorConstants.RED); document.Add(img4); iText.Layout.Element.Image img5 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST5)); img5.ScaleToFit(50, 50); Style style = new Style(); style.SetBorderRight(new SolidBorder(2)); img5.AddStyle(style); document.Add(img5); iText.Layout.Element.Image img6 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST6)); PdfAction top = PdfAction.CreateGoTo("Top"); img6.ScaleToFit(100, 100).SetAction(top); document.Add(img6); document.Close(); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); ImageData img = ImageDataFactory.Create(IMG); float x = 10; float y = 650; float width = img.GetWidth(); float height = img.GetHeight(); PdfPage firstPage = pdfDoc.GetFirstPage(); PdfCanvas canvas = new PdfCanvas(firstPage); canvas.AddImageAt(img, x, y, false); Rectangle linkLocation = new Rectangle(x, y, width, height); // Make the link destination page fit to the display PdfExplicitDestination destination = PdfExplicitDestination.CreateFit(pdfDoc.GetLastPage()); PdfAnnotation annotation = new PdfLinkAnnotation(linkLocation) // Set highlighting type which is enabled after a click on the annotation .SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT) // Create a link to the last page of the document. .SetAction(PdfAction.CreateGoTo(destination)) .SetBorder(new PdfArray(new float[] { 0, 0, 0 })); firstPage.AddAnnotation(annotation); pdfDoc.Close(); }
private MemoryStream CreatePdfWithGoToAnnot(bool isTheSameLink, bool diffDisplayOptions) { MemoryStream stream = new MemoryStream(); PdfDocument pdfDocument = new PdfDocument(new PdfWriter(stream)); pdfDocument.AddNewPage(); pdfDocument.AddNewPage(); pdfDocument.AddNewPage(); Rectangle linkLocation = new Rectangle(523, 770, 36, 36); PdfExplicitDestination destination = PdfExplicitDestination.CreateFit(pdfDocument.GetPage(3)); PdfAnnotation annotation = new PdfLinkAnnotation(linkLocation).SetAction(PdfAction.CreateGoTo(destination) ).SetBorder(new PdfArray(new int[] { 0, 0, 1 })); pdfDocument.GetFirstPage().AddAnnotation(annotation); if (!isTheSameLink) { destination = (diffDisplayOptions) ? PdfExplicitDestination.Create(pdfDocument.GetPage(3), PdfName.XYZ, 350 , 350, 0, 0, 1) : PdfExplicitDestination.CreateFit(pdfDocument.GetPage(1)); } annotation = new PdfLinkAnnotation(linkLocation).SetAction(PdfAction.CreateGoTo(destination)).SetBorder(new PdfArray(new int[] { 0, 0, 1 })); pdfDocument.GetPage(2).AddAnnotation(annotation); pdfDocument.Close(); return(stream); }
/// <summary> /// process list as last page of creating PDF /// </summary> /// <param name="toc"></param> /// <param name="doc"></param> private static void processDocumentLinkedList(List <KeyValuePair <string, int> > toc, Document doc) { PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA); PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD); doc.Add(new AreaBreak(AreaBreakType.NEXT_PAGE)); Paragraph p = new Paragraph().SetFont(bold).Add("Obsah"); doc.Add(p); List <TabStop> tabStops = new List <TabStop>(); tabStops.Add(new TabStop(580, TabAlignment.RIGHT, new iText.Kernel.Pdf.Canvas.Draw.DottedLine())); foreach (KeyValuePair <string, int> entry in toc) { p = new iText.Layout.Element.Paragraph() .AddTabStops(tabStops) .Add(entry.Key) .Add(new Tab()) .Add(entry.Value.ToString()) .SetAction(PdfAction.CreateGoTo(PdfExplicitRemoteGoToDestination.CreateFit(entry.Value))); doc.Add(p); } }
public virtual void CreatePdf(String dest) { // Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); // Initialize document Document document = new Document(pdf); Style style = new Style(); style.SetBackgroundColor(ColorConstants.YELLOW); document.Add(CreateNewSeparator().AddStyle(style).SetDestination("Top")); document.Add(new Paragraph("test")); document.Add(CreateNewSeparator().SetWidth(300).SetHorizontalAlignment(HorizontalAlignment.CENTER)); document.Add(CreateNewSeparator().SetMargin(10).SetVerticalAlignment(VerticalAlignment.BOTTOM).SetBorder(new SolidBorder(0.5f))); document.Add(CreateNewSeparator().SetMargin(10).SetWidth(300)); document.Add(CreateNewSeparator().SetMargin(10).SetRelativePosition(10, 10, 50, 10)); document.Add(CreateNewSeparator().SetMargin(10).SetWidth(UnitValue.CreatePercentValue(50))); document.Add(CreateNewSeparator().SetMargin(10).SetWidth(50).SetAction(PdfAction.CreateGoTo("Top"))); document.Add(CreateNewSeparator().SetFixedPosition(100, 200, 350)); document.Add(new AreaBreak()); document.Add(CreateNewSeparator().SetBackgroundColor(ColorConstants.YELLOW).SetMarginBottom(10)); document.Add(CreateNewSeparator().SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetPaddingLeft(20).SetPaddingRight( 50)); document.Add(CreateNewSeparator().SetBackgroundColor(ColorConstants.YELLOW)); document.Add(CreateNewSeparator().SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetMarginBottom(50)); document.Add(CreateNewSeparator().SetBackgroundColor(ColorConstants.YELLOW)); document.Add(CreateNewSeparator().SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetMargin(50).SetPadding(30)); document.Add(CreateNewSeparator().SetBackgroundColor(ColorConstants.YELLOW)); document.Add(CreateNewSeparator().SetBackgroundColor(ColorConstants.LIGHT_GRAY)); document.Close(); }
public void ManipulatePdf(String dest) { PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN); PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD); PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document document = new Document(pdfDoc); document .SetTextAlignment(TextAlignment.JUSTIFIED) .SetFont(font) .SetFontSize(11); List <Pair <String, Pair <String, int> > > toc = new List <Pair <string, Pair <string, int> > >(); // Parse text to PDF CreatePdfWithOutlines(SRC, document, toc, bold); // Remove the main title from the table of contents list toc.RemoveAt(0); // Create table of contents document.Add(new AreaBreak()); Paragraph p = new Paragraph("Table of Contents") .SetFont(bold) .SetDestination("toc"); document.Add(p); List <TabStop> tabStops = new List <TabStop>(); tabStops.Add(new TabStop(580, TabAlignment.RIGHT, new DottedLine())); foreach (Pair <String, Pair <String, int> > entry in toc) { Pair <String, int> text = entry.Value; p = new Paragraph() .AddTabStops(tabStops) .Add(text.Key) .Add(new Tab()) .Add(text.Value.ToString()) .SetAction(PdfAction.CreateGoTo(entry.Key)); document.Add(p); } // Move the table of contents to the first page int tocPageNumber = pdfDoc.GetNumberOfPages(); pdfDoc.MovePage(tocPageNumber, 1); // Add page labels pdfDoc.GetPage(1).SetPageLabel(PageLabelNumberingStyle.UPPERCASE_LETTERS, null, 1); pdfDoc.GetPage(2).SetPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, null, 1); document.Close(); }
/// <summary> /// creates link to specific page /// </summary> /// <param name="pageIndex">page index</param> /// <param name="textLink">text link</param> /// <returns>paragraph link</returns> protected Paragraph CreateLinkToPage(int pageIndex, string textLink) { //PdfArray array = new PdfArray(); //array.Add(document.GetPdfDocument().GetPage(pageIndex).GetPdfObject()); //array.Add(PdfName.Fit); //PdfDestination dest = PdfDestination.MakeDestination(array); PdfExplicitDestination dest = PdfExplicitDestination.CreateFit(pdfDocument.GetPage(pageIndex)); Paragraph link = new Paragraph(new Link(textLink, PdfAction.CreateGoTo(dest))); return(link); }
private static void MergePdfsWithBookmarks(List <string> InFiles, string OutFile) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(OutFile)); pdfDoc.InitializeOutlines(); PdfMerger merger = new PdfMerger(pdfDoc, true, true); List <PdfOutline> listItem = new List <PdfOutline>(); InFiles.ForEach(srcFile => { PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(srcFile)); int pageCount = firstSourcePdf.GetNumberOfPages(); merger.Merge(firstSourcePdf, 1, pageCount); firstSourcePdf.GetOutlines(false).GetDestination(); firstSourcePdf.Close(); if (pageCount % 2 == 1) { pdfDoc.AddNewPage(iTextPageSize.A4); } }); PdfOutline rootOutline = pdfDoc.GetOutlines(false); listItem.AddRange(rootOutline.GetAllChildren()); rootOutline.GetAllChildren().Clear(); string parentTitle = Path.GetFileNameWithoutExtension(OutFile); PdfOutline parent = rootOutline.AddOutline(parentTitle); foreach (var item in listItem) { parent.AddOutline(item); } PdfExplicitDestination destToPage3 = PdfExplicitDestination.CreateFit(pdfDoc.GetFirstPage()); string stringDest = Guid.NewGuid().ToString(); pdfDoc.AddNamedDestination(stringDest, destToPage3.GetPdfObject()); parent.AddAction(PdfAction.CreateGoTo(new PdfStringDestination(stringDest))); if (pdfDoc.GetNumberOfPages() % 2 == 1) { pdfDoc.AddNewPage(iTextPageSize.A4); } pdfDoc.Close(); }
public virtual void RemoteGoToNotValidExplicitDestinationTest() { String cmpFile = sourceFolder + "cmp_remoteGoToNotValidExplicitDestinationTest.pdf"; String outFile = destinationFolder + "remoteGoToNotValidExplicitDestinationTest.pdf"; PdfDocument document = new PdfDocument(new PdfWriter(outFile)); document.AddNewPage(); PdfLinkAnnotation linkExplicitDest = new PdfLinkAnnotation(new Rectangle(35, 785, 160, 15)); linkExplicitDest.SetAction(PdfAction.CreateGoTo(PdfExplicitRemoteGoToDestination.CreateFit(1))); document.GetFirstPage().AddAnnotation(linkExplicitDest); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder, "diff_" )); }
private int AddTOCItem(ref Document document, List <Tuple <string, int> > tocItems, bool addAfterRemove, int locationToStartTOC, int?amountPages = null) { var pdf = document.GetPdfDocument(); int startingTOC = pdf.GetNumberOfPages(); int amountToIncrement = 0; if (amountPages != null) { amountToIncrement = amountPages.Value; } foreach (var entry in tocItems) { string destinationKey = "p" + entry.Item2 + amountToIncrement; PdfArray destinationArray = new PdfArray(); destinationArray.Add(new PdfNumber(entry.Item2 + amountToIncrement - 1)); destinationArray.Add(PdfName.XYZ); pdf.AddNamedDestination(destinationKey, destinationArray); Paragraph p = new Paragraph(); p.AddTabStops(new TabStop(540, TabAlignment.RIGHT, new DottedLine())); p.Add(entry.Item1); p.Add(new Tab()); p.Add((entry.Item2 + amountToIncrement).ToString()); p.SetProperty(Property.ACTION, PdfAction.CreateGoTo(destinationKey)); document.Add(p); } int endingTOC = pdf.GetNumberOfPages(); if (amountPages == null) { amountPages = (endingTOC - startingTOC) + 1; } for (int i = 0; i < amountPages.Value; i++) { var tocPage = pdf.GetLastPage(); pdf.RemovePage(tocPage); if (addAfterRemove && tocPage.IsFlushed() == false) { pdf.AddPage(locationToStartTOC, tocPage); } } return(amountPages.Value); }
public virtual void StructureDestination01Test() { String srcFile = sourceFolder + "customRolesMappingPdf2.pdf"; String outFile = destinationFolder + "structureDestination01Test.pdf"; String cmpFile = sourceFolder + "cmp_structureDestination01Test.pdf"; PdfDocument document = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outFile)); PdfStructElem imgElement = new PdfStructElem((PdfDictionary)document.GetPdfObject(13)); PdfStructureDestination dest = PdfStructureDestination.CreateFit(imgElement); PdfPage secondPage = document.AddNewPage(); PdfLinkAnnotation linkExplicitDest = new PdfLinkAnnotation(new Rectangle(35, 785, 160, 15)); linkExplicitDest.SetAction(PdfAction.CreateGoTo(dest)); secondPage.AddAnnotation(linkExplicitDest); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder, "diff_" )); }
public virtual void LinkTest02() { String outFileName = destinationFolder + "linkTest02.pdf"; String cmpFileName = sourceFolder + "cmp_linkTest02.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName)); Document doc = new Document(pdfDoc); doc.Add(new AreaBreak()).Add(new AreaBreak()); PdfDestination dest = PdfExplicitDestination.CreateXYZ(pdfDoc.GetPage(1), 36, 100, 1); PdfAction action = PdfAction.CreateGoTo(dest); Link link = new Link("TestLink", action); doc.Add(new Paragraph(link)); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff")); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document doc = new Document(pdfDoc); for (int i = 0; i < 10; i++) { String title = "This is title " + i; Text text = new Text(title).SetFontSize(16); text.SetNextRenderer(new TOCTextRenderer(text)); doc.Add(new Paragraph(text)); for (int j = 0; j < 50; j++) { doc.Add(new Paragraph("Line " + j + " of title " + i)); } } doc.Add(new AreaBreak()); // Create a table of contents doc.Add(new Paragraph("Table of Contents").SetFontSize(16)); foreach (KeyValuePair <String, KeyValuePair <String, int> > entry in toc) { Text text = new Text(entry.Key); KeyValuePair <String, int> value = entry.Value; text.SetAction(PdfAction.CreateGoTo(value.Key)); Paragraph p = new Paragraph(text); p.AddTabStops(new TabStop(750, TabAlignment.RIGHT, new DottedLine())); p.Add(new Tab()); text = new Text(value.Value.ToString()); text.SetAction(PdfAction.CreateGoTo(value.Key)); p.Add(text); doc.Add(p); } doc.Close(); }
public virtual void DestTest01() { String srcFile = sourceFolder + "simpleNoLinks.pdf"; String outFile = destinationFolder + "destTest01.pdf"; String cmpFile = sourceFolder + "cmp_destTest01.pdf"; PdfDocument document = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outFile)); PdfPage firstPage = document.GetPage(1); PdfLinkAnnotation linkExplicitDest = new PdfLinkAnnotation(new Rectangle(35, 785, 160, 15)); linkExplicitDest.SetAction(PdfAction.CreateGoTo(PdfExplicitDestination.CreateFit(document.GetPage(2)))); firstPage.AddAnnotation(linkExplicitDest); PdfLinkAnnotation linkStringDest = new PdfLinkAnnotation(new Rectangle(35, 760, 160, 15)); PdfExplicitDestination destToPage3 = PdfExplicitDestination.CreateFit(document.GetPage(3)); String stringDest = "thirdPageDest"; document.AddNamedDestination(stringDest, destToPage3.GetPdfObject()); linkStringDest.SetAction(PdfAction.CreateGoTo(new PdfStringDestination(stringDest))); firstPage.AddAnnotation(linkStringDest); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder, "diff_" )); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); Document doc = new Document(pdfDoc); // Make the link destination page fit to the display PdfExplicitDestination destination = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(3)); Link link = new Link( "This is a link. Click it and you'll be forwarded to another page in this document.", // Add link to the 3rd page. PdfAction.CreateGoTo(destination)); // Set highlighting type which is enabled after a click on the annotation link.GetLinkAnnotation().SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT); Paragraph p = new Paragraph(link).SetWidth(240); doc.ShowTextAligned(p, 320, 695, 1, TextAlignment.LEFT, VerticalAlignment.TOP, 0); doc.Close(); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); Rectangle linkLocation = new Rectangle(523, 770, 36, 36); int[] borders = { 0, 0, 1 }; // Make the link destination page fit to the display PdfExplicitDestination destination = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(3)); PdfAnnotation annotation = new PdfLinkAnnotation(linkLocation) // Set highlighting type which is enabled after a click on the annotation .SetHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT) // Add link to the 3rd page. .SetAction(PdfAction.CreateGoTo(destination)) .SetBorder(new PdfArray(borders)); pdfDoc.GetFirstPage().AddAnnotation(annotation); pdfDoc.Close(); }
public virtual void ActionCheck15() { String outPdf = destinationFolder + "pdfA2b_actionCheck15.pdf"; String cmpPdf = sourceFolder + "cmp/PdfA2ActionCheckTest/cmp_pdfA2b_actionCheck15.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_2B, new PdfOutputIntent("Custom", "" , "http://www.color.org", "sRGB IEC61966-2.1", @is)); doc.GetOutlines(true); PdfOutline @out = doc.GetOutlines(false); @out.AddOutline("New").AddAction(PdfAction.CreateGoTo("TestDest")); doc.AddNewPage(); doc.Close(); String result = new CompareTool().CompareByContent(outPdf, cmpPdf, destinationFolder, "diff_"); if (result != null) { NUnit.Framework.Assert.Fail(result); } }
public virtual void CreatePdf(String dest) { //Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); // Initialize document Document document = new Document(pdf); PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN); PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD); document.SetTextAlignment(TextAlignment.JUSTIFIED).SetHyphenation(new HyphenationConfig("en", "uk", 3, 3)) .SetFont(font).SetFontSize(11); StreamReader sr = File.OpenText(SRC); String name; String line; Paragraph p; bool title = true; int counter = 0; IList <Util.Pair <String, Util.Pair <String, int> > > toc = new List <Util.Pair <String, Util.Pair <String, int> > >(); while ((line = sr.ReadLine()) != null) { p = new Paragraph(line); p.SetKeepTogether(true); if (title) { name = String.Format("title{0:00}", counter++); Util.Pair <String, int> titlePage = new Util.Pair <string, int>(line, pdf.GetNumberOfPages()); p.SetFont(bold).SetFontSize(12).SetKeepWithNext(true).SetDestination(name).SetNextRenderer(new UpdatePageRenderer(p, titlePage)); title = false; document.Add(p); toc.Add(new Util.Pair <string, Util.Pair <string, int> >(name, titlePage)); } else { p.SetFirstLineIndent(36); if (String.IsNullOrEmpty(line)) { p.SetMarginBottom(12); title = true; } else { p.SetMarginBottom(0); } document.Add(p); } } document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE)); p = new Paragraph().SetFont(bold).Add("Table of Contents").SetDestination("toc"); document.Add(p); toc.RemoveAt(0); IList <TabStop> tabstops = new List <TabStop>(); tabstops.Add(new TabStop(580, TabAlignment.RIGHT, new DottedLine())); foreach (Util.Pair <String, Util.Pair <String, int> > entry in toc) { Util.Pair <String, int> text = entry.Value; p = new Paragraph().AddTabStops(tabstops).Add(text.Key).Add(new Tab()).Add(text.Value.ToString()).SetAction (PdfAction.CreateGoTo(entry.Key)); document.Add(p); } //Close document document.Close(); }
public virtual void CreatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document document = new Document(pdfDoc); document.Add(new Paragraph(new Text("The Revenant nominations list")).SetTextAlignment(TextAlignment.CENTER )); PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1)); foreach (KeyValuePair <String, int> entry in TheRevenantNominations) { //Copy page PdfPage page = firstSourcePdf.GetPage(entry.Value).CopyTo(pdfDoc); pdfDoc.AddPage(page); //Overwrite page number Text text = new Text(String.Format("Page {0}", pdfDoc.GetNumberOfPages() - 1)); text.SetBackgroundColor(ColorConstants.WHITE); document.Add(new Paragraph(text).SetFixedPosition(pdfDoc.GetNumberOfPages(), 549, 742, 100)); //Add destination String destinationKey = "p" + (pdfDoc.GetNumberOfPages() - 1); PdfArray destinationArray = new PdfArray(); destinationArray.Add(page.GetPdfObject()); destinationArray.Add(PdfName.XYZ); destinationArray.Add(new PdfNumber(0)); destinationArray.Add(new PdfNumber(page.GetMediaBox().GetHeight())); destinationArray.Add(new PdfNumber(1)); pdfDoc.AddNamedDestination(destinationKey, destinationArray); //Add TOC line with bookmark Paragraph p = new Paragraph(); p.AddTabStops(new TabStop(540, TabAlignment.RIGHT, new DottedLine())); p.Add(entry.Key); p.Add(new Tab()); p.Add((pdfDoc.GetNumberOfPages() - 1).ToString()); p.SetProperty(Property.ACTION, PdfAction.CreateGoTo(destinationKey)); document.Add(p); } firstSourcePdf.Close(); //Add the last page PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2)); PdfPage page_1 = secondSourcePdf.GetPage(1).CopyTo(pdfDoc); pdfDoc.AddPage(page_1); //Add destination PdfArray destinationArray_1 = new PdfArray(); destinationArray_1.Add(page_1.GetPdfObject()); destinationArray_1.Add(PdfName.XYZ); destinationArray_1.Add(new PdfNumber(0)); destinationArray_1.Add(new PdfNumber(page_1.GetMediaBox().GetHeight())); destinationArray_1.Add(new PdfNumber(1)); pdfDoc.AddNamedDestination("checklist", destinationArray_1); //Add TOC line with bookmark Paragraph p_1 = new Paragraph(); p_1.AddTabStops(new TabStop(540, TabAlignment.RIGHT, new DottedLine())); p_1.Add("Oscars\u00ae 2016 Movie Checklist"); p_1.Add(new Tab()); p_1.Add((pdfDoc.GetNumberOfPages() - 1).ToString()); p_1.SetProperty(Property.ACTION, PdfAction.CreateGoTo("checklist")); document.Add(p_1); secondSourcePdf.Close(); // close the document document.Close(); }
public virtual void CreatePdf(String dest) { PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); pdf.GetCatalog().SetPageLayout(PdfName.TwoColumnRight); pdf.GetCatalog().SetPageMode(PdfName.UseThumbs); PdfPage page = pdf.AddNewPage(); page.SetPageLabel(PageLabelNumberingStyle.LOWERCASE_ROMAN_NUMERALS, null); Document document = new Document(pdf); document.Add(new Paragraph().Add("Page left blank intentionally")); document.Add(new AreaBreak()); document.Add(new Paragraph().Add("Page left blank intentionally")); document.Add(new AreaBreak()); document.Add(new Paragraph().Add("Page left blank intentionally")); document.Add(new AreaBreak()); page = pdf.GetLastPage(); page.SetPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, null, 1); PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN); PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD); document.SetTextAlignment(TextAlignment.JUSTIFIED).SetHyphenation(new HyphenationConfig("en", "uk", 3, 3)) .SetFont(font).SetFontSize(11); StreamReader sr = File.OpenText(SRC); String name; String line; Paragraph p; bool title = true; int counter = 0; IList <Util.Pair <String, Util.Pair <String, int> > > toc = new List <Util.Pair <String, Util.Pair <String, int> > >(); while ((line = sr.ReadLine()) != null) { p = new Paragraph(line); p.SetKeepTogether(true); if (title) { name = String.Format("title{0:00}", counter++); p.SetFont(bold).SetFontSize(12).SetKeepWithNext(true).SetDestination(name); title = false; document.Add(p); toc.Add(new Util.Pair <string, Util.Pair <string, int> >(name, new Util.Pair <string, int>(line, pdf.GetNumberOfPages()))); } else { p.SetFirstLineIndent(36); if (String.IsNullOrEmpty(line)) { p.SetMarginBottom(12); title = true; } else { p.SetMarginBottom(0); } document.Add(p); } } document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE)); p = new Paragraph().SetFont(bold).Add("Table of Contents").SetDestination("toc"); document.Add(p); page = pdf.GetLastPage(); page.SetPageLabel(null, "TOC", 1); toc.RemoveAt(0); IList <TabStop> tabstops = new List <TabStop>(); tabstops.Add(new TabStop(580, TabAlignment.RIGHT, new DottedLine())); foreach (Util.Pair <String, Util.Pair <String, int> > entry in toc) { Util.Pair <String, int> text = entry.Value; p = new Paragraph().AddTabStops(tabstops).Add(text.Key).Add(new Tab()).Add(text.Value.ToString()).SetAction (PdfAction.CreateGoTo(entry.Key)); document.Add(p); } document.Close(); }
public virtual void CreatePdf(String dest) { // Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); // Initialize document Document document = new Document(pdf); Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth(); PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN); table.AddCell(new Cell().Add(new Paragraph("Test 1")).SetHeight(50).SetDestination("Top")); Style style = new Style(); style.SetBackgroundColor(ColorConstants.YELLOW); table.AddCell(new Cell().SetBorder(new DottedBorder(5)).Add(new Paragraph("Test 2")).AddStyle(style).SetRelativePosition( 10, 10, 50, 10)); table.AddCell(new Cell().Add(new Paragraph("Test 3")).SetVerticalAlignment(VerticalAlignment.BOTTOM)); table.AddCell(new Cell().Add(ParagraphProperties.GetNewParagraphInstance()).SetHyphenation(new HyphenationConfig ("en", "uk", 3, 3))); table.AddCell(new Cell().Add(new Paragraph("Rotated")).SetRotationAngle(Math.PI / 18).SetFont(font).SetFontSize(8).SetFontColor (ColorConstants.RED)); table.AddCell(new Cell().Add(new Paragraph("Centered")).SetTextAlignment(TextAlignment.CENTER).SetAction(PdfAction.CreateGoTo ("Top"))); table.AddCell(new Cell().Add(new Paragraph("Test 5")).SetBackgroundColor(ColorConstants.BLUE)); table.AddCell(new Cell().Add(ParagraphProperties.GetNewParagraphInstance()).SetBackgroundColor(ColorConstants.RED). SetPaddingLeft(20).SetPaddingRight(50)); table.AddCell(new Cell().Add(new Paragraph("Test 7")).SetBackgroundColor(ColorConstants.RED)); table.AddCell(new Cell().Add(new Paragraph("Test 8")).SetBackgroundColor(ColorConstants.BLUE).SetMarginBottom(10)); table.AddCell(new Cell().Add(new Paragraph("Test 9")).SetBackgroundColor(ColorConstants.BLUE)); table.AddCell(new Cell().Add(new Paragraph("Test 10")).SetBackgroundColor(ColorConstants.RED)); table.AddCell(new Cell().Add(ParagraphProperties.GetNewParagraphInstance()).SetBackgroundColor(ColorConstants.RED). SetMargin(50).SetPadding(30)); table.AddCell(new Cell().Add(new Paragraph("Test 12")).SetBackgroundColor(ColorConstants.BLUE)); document.Add(table); SolidBorder border = new SolidBorder(ColorConstants.RED, 2); Cell cell = new Cell().Add(new Paragraph("Test")).SetFixedPosition(100, 400, 350).SetBorder(border).SetBackgroundColor(ColorConstants .BLUE).SetHeight(100).SetHorizontalAlignment(HorizontalAlignment.CENTER); document.Add(cell); document.Close(); }
private static void MergePdfs(List <string> InFiles, string OutFile) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(OutFile)); pdfDoc.InitializeOutlines(); //PdfMerger merger = new PdfMerger(pdfDoc, false, false); PdfOutline rootOutline = pdfDoc.GetOutlines(true); string parentTitle = Path.GetFileNameWithoutExtension(OutFile); PdfOutline parent = rootOutline.AddOutline(parentTitle); //parent.AddAction(PdfAction.CreateGoTo( // PdfExplicitRemoteGoToDestination.CreateFit(1))); int pageNumber = 0; foreach (var srcFile in InFiles) { string title = Path.GetFileNameWithoutExtension(srcFile); PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(srcFile)); int pageCount = firstSourcePdf.GetNumberOfPages(); for (int i = 1; i < pageCount + 1; i++) { var page = firstSourcePdf.GetPage(i); int pageRotate = page.GetRotation(); if (pageRotate != 0) { page.SetRotation(0); } PdfPage newPage = firstSourcePdf.GetPage(i).CopyTo(pdfDoc); pdfDoc.AddPage(newPage); } //merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages()); //firstSourcePdf.CopyPagesTo(1, firstSourcePdf.GetNumberOfPages(), pdfDoc); //int all = pdfDoc.GetNumberOfPages(); PdfExplicitDestination dd = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(pageNumber + 1)); string tt = Guid.NewGuid().ToString(); pdfDoc.AddNamedDestination(tt, dd.GetPdfObject()); PdfOutline kid = parent.AddOutline(title); kid.AddAction(PdfAction.CreateGoTo(new PdfStringDestination(tt))); //kid.AddAction(PdfAction.CreateGoTo( //PdfExplicitRemoteGoToDestination.CreateFit(pageNumber))); var bb = parent.GetAllChildren(); pageNumber += pageCount; firstSourcePdf.Close(); if (pageCount % 2 == 1) { pdfDoc.AddNewPage(iTextPageSize.A4); pageNumber += 1; } } PdfExplicitDestination destToPage3 = PdfExplicitDestination.CreateFit(pdfDoc.GetFirstPage()); string stringDest = Guid.NewGuid().ToString(); pdfDoc.AddNamedDestination(stringDest, destToPage3.GetPdfObject()); parent.AddAction(PdfAction.CreateGoTo(new PdfStringDestination(stringDest))); if (pdfDoc.GetNumberOfPages() % 2 == 1) { pdfDoc.AddNewPage(pageNumber, iTextPageSize.A4); } pdfDoc.Close(); }
protected void ManipulatePdf(String dest) { MemoryStream baos = new MemoryStream(); PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos)); Document doc = new Document(pdfDoc); // Copier contains the additional logic to copy acroform fields to a new page. // PdfPageFormCopier uses some caching logic which can potentially improve performance // in case of the reusing of the same instance. PdfPageFormCopier formCopier = new PdfPageFormCopier(); // Copy all merging file's pages to the temporary pdf file Dictionary <String, PdfDocument> filesToMerge = InitializeFilesToMerge(); Dictionary <int, String> toc = new Dictionary <int, String>(); int page = 1; foreach (KeyValuePair <String, PdfDocument> entry in filesToMerge) { PdfDocument srcDoc = entry.Value; int numberOfPages = srcDoc.GetNumberOfPages(); toc.Add(page, entry.Key); for (int i = 1; i <= numberOfPages; i++, page++) { Text text = new Text(String.Format("Page {0}", page)); srcDoc.CopyPagesTo(i, i, pdfDoc, formCopier); // Put the destination at the very first page of each merged document if (i == 1) { text.SetDestination("p" + page); PdfOutline rootOutLine = pdfDoc.GetOutlines(false); PdfOutline outline = rootOutLine.AddOutline("p" + page); outline.AddDestination(PdfDestination.MakeDestination(new PdfString("p" + page))); } doc.Add(new Paragraph(text) .SetFixedPosition(page, 549, 810, 40) .SetMargin(0) .SetMultipliedLeading(1)); } } PdfDocument tocDoc = new PdfDocument(new PdfReader(SRC3)); tocDoc.CopyPagesTo(1, 1, pdfDoc, formCopier); tocDoc.Close(); // Create a table of contents float tocYCoordinate = 750; float tocXCoordinate = doc.GetLeftMargin(); float tocWidth = pdfDoc.GetDefaultPageSize().GetWidth() - doc.GetLeftMargin() - doc.GetRightMargin(); foreach (KeyValuePair <int, String> entry in toc) { Paragraph p = new Paragraph(); p.AddTabStops(new TabStop(500, TabAlignment.LEFT, new DashedLine())); p.Add(entry.Value); p.Add(new Tab()); p.Add(entry.Key.ToString()); p.SetAction(PdfAction.CreateGoTo("p" + entry.Key)); doc.Add(p .SetFixedPosition(pdfDoc.GetNumberOfPages(), tocXCoordinate, tocYCoordinate, tocWidth) .SetMargin(0) .SetMultipliedLeading(1)); tocYCoordinate -= 20; } foreach (PdfDocument srcDocument in filesToMerge.Values) { srcDocument.Close(); } doc.Close(); PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest)); PdfDocument srcPdfDoc = new PdfDocument(new PdfReader(new MemoryStream(baos.ToArray()), new ReaderProperties())); srcPdfDoc.InitializeOutlines(); // Create a copy order list and set the page with a table of contents as the first page int tocPageNumber = srcPdfDoc.GetNumberOfPages(); List <int> copyPagesOrderList = new List <int>(); copyPagesOrderList.Add(tocPageNumber); for (int i = 1; i < tocPageNumber; i++) { copyPagesOrderList.Add(i); } srcPdfDoc.CopyPagesTo(copyPagesOrderList, resultDoc, formCopier); srcPdfDoc.Close(); resultDoc.Close(); }
/// <summary> /// Updates the links. /// </summary> /// <param name="pdfFilePath">The PDF file path.</param> /// <param name="htmlToPdfFiles">The HTML to PDF files.</param> /// <param name="logger">The logger.</param> internal static void UpdateLinks( string pdfFilePath, IReadOnlyCollection <HtmlToPdfFile> htmlToPdfFiles, ILogger logger) { string tempFilePath = Path.GetTempFileName(); using (PdfReader pdfReader = new PdfReader(pdfFilePath)) { using (PdfWriter pdfWriter = new PdfWriter(tempFilePath)) { using (iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(pdfReader, pdfWriter)) { int pageCount = pdfDocument.GetNumberOfPages(); for (int i = 1; i <= pageCount; i++) { // get page PdfPage pdfPage = pdfDocument.GetPage(i); // get link annotations IEnumerable <PdfLinkAnnotation> linkAnnotations = pdfPage.GetAnnotations().OfType <PdfLinkAnnotation>(); foreach (PdfLinkAnnotation linkAnnotation in linkAnnotations) { // get action PdfDictionary action = linkAnnotation.GetAction(); if (action == null) { continue; } PdfName s = action.GetAsName(PdfName.S); if (s != PdfName.URI) { continue; } PdfString uriPdfString = action.GetAsString(PdfName.URI); if (!Uri.TryCreate(uriPdfString.GetValue(), UriKind.RelativeOrAbsolute, out Uri uri)) { continue; } if (!uri.IsFile) { continue; } string htmlFilePath = uri.LocalPath.ToLower(); if (!htmlToPdfFiles.Any(x => string.Compare(x.Input, htmlFilePath, StringComparison.OrdinalIgnoreCase) == 0)) { // ex. when printing PDF from TOC.html by itself logger.LogDebug($"Could not find '{htmlFilePath}'. Referenced in '{pdfFilePath}' on page {i}."); continue; } HtmlToPdfFile linkedHtmlToPdfFile = htmlToPdfFiles.Single(x => x.Input == htmlFilePath); int linkedPageNumber = linkedHtmlToPdfFile.OutputPdfFilePageNumber; PdfPage linkedPage; try { // http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfDestination.html linkedPage = pdfDocument.GetPage(linkedPageNumber); } catch (Exception ex) { throw new PdfPageNotFoundException(linkedPageNumber, linkedHtmlToPdfFile.Input, ex); } float top = linkedPage.GetPageSize().GetTop(); PdfExplicitDestination destination = PdfExplicitDestination.CreateFitH(linkedPage, top); PdfAction newAction = PdfAction.CreateGoTo(destination); linkAnnotation.SetAction(newAction); } } } } } File.Delete(pdfFilePath); File.Move(tempFilePath, pdfFilePath); }