public void GenPdfDailyBatteryAllCust(string TempFolderPath, string ImageFileName, string ReportPeriod, string ProcessDate) { //this creates a new destination to send the action to when the document is opened. The zoom in this instance is set to 0.75f (75%). Again I use doc.PageSize.Height to set the y coordinate to the top of the page. PdfDestination.XYZ is a specific PdfDestination Type that allows us to set the location and zoom. PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, _doc.PageSize.Height, 1.00f); _doc.Open(); string imageURL_VinSplash = Path.Combine(TempFolderPath, "VinCoverPage.png"); iTextSharp.text.Image jpg_VinSplash = iTextSharp.text.Image.GetInstance(imageURL_VinSplash); //Resize image depend upon your need jpg_VinSplash.ScalePercent(65f); //Give space before image jpg_VinSplash.SpacingBefore = 10f; //Give some space after the image jpg_VinSplash.SpacingAfter = 1f; jpg_VinSplash.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinSplash); PdfPTable table = GenerateBatteryAllCustDashboard(ProcessDate); _doc.Add(table); GenerateBatteryChartByCustVin(TempFolderPath, ProcessDate); PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, _writer); // //set the open action for our writer object _writer.SetOpenAction(action); }
public INamedDestination CreateNamedInstanceFor(Dictionary<string, object> iDestination) { // TODO: Either verify that these settings are okay by default, or make them configuration injectable. var uniqueIdentifier = Guid.NewGuid().ToString().Replace("-", string.Empty); var page = 0; var typeParam = -1.0f; var type = 0; if (iDestination.ContainsKey("Title")) { var title = _nameProvider.GetName(iDestination["Title"].ToString()); if (!string.IsNullOrEmpty(title)) { uniqueIdentifier = title; } } if (iDestination.ContainsKey("Page")) { var matches = Regex.Match(iDestination["Page"].ToString(), @"(?'page'\d*) (?'type'\w*) (?'typeParam'\d*)"); if (!string.IsNullOrEmpty(matches.Groups["page"].Value)) { page = Int32.Parse(matches.Groups["page"].Value); } if (!string.IsNullOrEmpty(matches.Groups["type"].Value)) { type = GetPDFViewType(matches.Groups["type"].Value); } if (!string.IsNullOrEmpty(matches.Groups["typeParam"].Value)) { typeParam = float.Parse(matches.Groups["typeParam"].Value); } } var destination = new PdfDestination(type, typeParam); return new NamedDestinationImpl(uniqueIdentifier, page, destination); }
// --------------------------------------------------------------------------- /** * Manipulates a PDF file src with the file dest as result * @param src the original PDF */ public byte[] ManipulatePdf(byte[] src) { // Create the reader PdfReader reader = new PdfReader(src); int n = reader.NumberOfPages; using (MemoryStream ms = new MemoryStream()) { // Create the stamper using (PdfStamper stamper = new PdfStamper(reader, ms)) { // Make a list with all the possible actions actions = new List<PdfAction>(); PdfDestination d; for (int i = 0; i < n; ) { d = new PdfDestination(PdfDestination.FIT); actions.Add(PdfAction.GotoLocalPage(++i, d, stamper.Writer)); } // Add a navigation table to every page PdfContentByte canvas; for (int i = 0; i < n; ) { canvas = stamper.GetOverContent(++i); CreateNavigationTable(i, n).WriteSelectedRows(0, -1, 696, 36, canvas); } } return ms.ToArray(); } }
public void ManipulatePdf(String src, String dest) { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)); Rectangle linkLocation = new Rectangle(523, 770, 559, 806); PdfDestination destination = new PdfDestination(PdfDestination.FIT); PdfAnnotation link = PdfAnnotation.CreateLink(stamper.Writer, linkLocation, PdfAnnotation.HIGHLIGHT_INVERT, 3, destination); stamper.AddAnnotation(link, 1); stamper.Close(); }
public void AddNavigationTest() { String src = srcFolder + "primes.pdf"; String dest = outFolder + "primes_links.pdf"; PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)); PdfDestination d = new PdfDestination(PdfDestination.FIT); Rectangle rect = new Rectangle(0, 806, 595, 842); PdfAnnotation a10 = PdfAnnotation.CreateLink(stamper.Writer, rect, PdfAnnotation.HIGHLIGHT_INVERT, 2, d); stamper.AddAnnotation(a10, 1); PdfAnnotation a1 = PdfAnnotation.CreateLink(stamper.Writer, rect, PdfAnnotation.HIGHLIGHT_PUSH, 1, d); stamper.AddAnnotation(a1, 2); stamper.Close(); CompareTool compareTool = new CompareTool(); String errorMessage = compareTool.CompareByContent(dest, srcFolder + "cmp_primes_links.pdf", outFolder, "diff_"); if (errorMessage != null) { Assert.Fail(errorMessage); } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 Image img = Image.GetInstance(IMG_BOX); document.Add(img); List list = new List(List.UNORDERED, 20); PdfDestination dest = new PdfDestination(PdfDestination.FIT); dest.AddFirst(new PdfNumber(1)); IEnumerable<Movie> box = PojoFactory.GetMovies(1) .Concat(PojoFactory.GetMovies(4)) ; foreach (Movie movie in box) { if (movie.Year > 1960) { PdfFileSpecification fs = PdfFileSpecification.FileEmbedded( writer, null, String.Format("kubrick_{0}.pdf", movie.Imdb), CreateMoviePage(movie) ); fs.AddDescription(movie.Title, false); writer.AddFileAttachment(fs); ListItem item = new ListItem(movie.MovieTitle); PdfTargetDictionary target = new PdfTargetDictionary(true); target.EmbeddedFileName = movie.Title; PdfAction action = PdfAction.GotoEmbedded(null, target, dest, true); Chunk chunk = new Chunk(" (see info)"); chunk.SetAction(action); item.Add(chunk); list.Add(item); } } document.Add(list); } }
// --------------------------------------------------------------------------- /** * Creates the PDF. * @return the bytes of a PDF file. */ public byte[] CreateMoviePage(Movie movie) { using (MemoryStream ms = new MemoryStream()) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter.GetInstance(document, ms); // step 3 document.Open(); // step 4 Paragraph p = new Paragraph( movie.MovieTitle, FontFactory.GetFont( BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 16 ) ); document.Add(p); document.Add(Chunk.NEWLINE); PdfPTable table = new PdfPTable(WIDTHS); table.AddCell(Image.GetInstance( String.Format(RESOURCE, movie.Imdb) )); PdfPCell cell = new PdfPCell(); cell.AddElement(new Paragraph("Year: " + movie.Year.ToString())); cell.AddElement(new Paragraph("Duration: " + movie.Duration.ToString())); table.AddCell(cell); document.Add(table); PdfDestination dest = new PdfDestination(PdfDestination.FIT); dest.AddFirst(new PdfNumber(1)); PdfTargetDictionary target = new PdfTargetDictionary(false); Chunk chunk = new Chunk("Go to original document"); PdfAction action = PdfAction.GotoEmbedded(null, target, dest, false); chunk.SetAction(action); document.Add(chunk); } return ms.ToArray(); } }
/** * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>. * * @param element the element to add * @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not. * @throws DocumentException when a document isn't open yet, or has been closed */ public override bool Add(IElement element) { if (writer != null && writer.IsPaused()) { return false; } switch (element.Type) { // Information (headers) case Element.HEADER: info.Addkey(((Meta)element).Name, ((Meta)element).Content); break; case Element.TITLE: info.AddTitle(((Meta)element).Content); break; case Element.SUBJECT: info.AddSubject(((Meta)element).Content); break; case Element.KEYWORDS: info.AddKeywords(((Meta)element).Content); break; case Element.AUTHOR: info.AddAuthor(((Meta)element).Content); break; case Element.CREATOR: info.AddCreator(((Meta)element).Content); break; case Element.PRODUCER: // you can not change the name of the producer info.AddProducer(); break; case Element.CREATIONDATE: // you can not set the creation date, only reset it info.AddCreationDate(); break; // content (text) case Element.CHUNK: { // if there isn't a current line available, we make one if (line == null) { CarriageReturn(); } // we cast the element to a chunk PdfChunk chunk = new PdfChunk((Chunk) element, anchorAction); // we try to add the chunk to the line, until we succeed { PdfChunk overflow; while ((overflow = line.Add(chunk)) != null) { CarriageReturn(); chunk = overflow; chunk.TrimFirstSpace(); } } pageEmpty = false; if (chunk.IsAttribute(Chunk.NEWPAGE)) { NewPage(); } break; } case Element.ANCHOR: { Anchor anchor = (Anchor) element; String url = anchor.Reference; leading = anchor.Leading; if (url != null) { anchorAction = new PdfAction(url); } // we process the element element.Process(this); anchorAction = null; break; } case Element.ANNOTATION: { if (line == null) { CarriageReturn(); } Annotation annot = (Annotation) element; Rectangle rect = new Rectangle(0, 0); if (line != null) rect = new Rectangle(annot.GetLlx(IndentRight - line.WidthLeft), annot.GetLly(IndentTop - currentHeight), annot.GetUrx(IndentRight - line.WidthLeft + 20), annot.GetUry(IndentTop - currentHeight - 20)); PdfAnnotation an = PdfAnnotationsImp.ConvertAnnotation(writer, annot, rect); annotationsImp.AddPlainAnnotation(an); pageEmpty = false; break; } case Element.PHRASE: { // we cast the element to a phrase and set the leading of the document leading = ((Phrase) element).Leading; // we process the element element.Process(this); break; } case Element.PARAGRAPH: { // we cast the element to a paragraph Paragraph paragraph = (Paragraph) element; AddSpacing(paragraph.SpacingBefore, leading, paragraph.Font); // we adjust the parameters of the document alignment = paragraph.Alignment; leading = paragraph.Leading; CarriageReturn(); // we don't want to make orphans/widows if (currentHeight + line.Height + leading > IndentTop - IndentBottom) { NewPage(); } indentLeft += paragraph.IndentationLeft; indentRight += paragraph.IndentationRight; CarriageReturn(); paraIndent += paragraph.IndentationLeft; IPdfPageEvent pageEvent = writer.PageEvent; if (pageEvent != null && isParagraph) pageEvent.OnParagraph(writer, this, IndentTop - currentHeight); // if a paragraph has to be kept together, we wrap it in a table object if (paragraph.KeepTogether) { PdfPTable table = new PdfPTable(1); table.WidthPercentage = 100f; PdfPCell cell = new PdfPCell(); cell.AddElement(paragraph); cell.Border = Rectangle.NO_BORDER; table.AddCell(cell); this.Add(table); } else { paraIndent += paragraph.IndentationLeft; line.SetExtraIndent(paragraph.FirstLineIndent); element.Process(this); paraIndent -= paragraph.IndentationLeft; } AddSpacing(paragraph.SpacingAfter, paragraph.Leading, paragraph.Font); if (pageEvent != null && isParagraph) pageEvent.OnParagraphEnd(writer, this, IndentTop - currentHeight); alignment = Element.ALIGN_LEFT; indentLeft -= paragraph.IndentationLeft; indentRight -= paragraph.IndentationRight; CarriageReturn(); break; } case Element.SECTION: case Element.CHAPTER: { // Chapters and Sections only differ in their constructor // so we cast both to a Section Section section = (Section) element; bool hasTitle = section.Title != null; // if the section is a chapter, we begin a new page if (section.TriggerNewPage) { NewPage(); } // otherwise, we begin a new line else { NewLine(); } if (hasTitle) { float fith = IndentTop - currentHeight; int rotation = pageSize.Rotation; if (rotation == 90 || rotation == 180) fith = pageSize.Height - fith; PdfDestination destination = new PdfDestination(PdfDestination.FITH, fith); while (currentOutline.Level >= section.Depth) { currentOutline = currentOutline.Parent; } PdfOutline outline = new PdfOutline(currentOutline, destination, section.GetBookmarkTitle(), section.BookmarkOpen); currentOutline = outline; } // some values are set CarriageReturn(); indentLeft += section.IndentationLeft; indentRight += section.IndentationRight; sectionIndentL += section.IndentationLeft; sectionIndentR += section.IndentationRight; IPdfPageEvent pageEvent = writer.PageEvent; if (pageEvent != null) if (element.Type == Element.CHAPTER) pageEvent.OnChapter(writer, this, IndentTop - currentHeight, section.Title); else pageEvent.OnSection(writer, this, IndentTop - currentHeight, section.Depth, section.Title); // the title of the section (if any has to be printed) if (hasTitle) { isParagraph = false; Add(section.Title); isParagraph = true; } indentLeft += section.Indentation; sectionIndentL += section.Indentation; // we process the section element.Process(this); // some parameters are set back to normal again indentLeft -= section.IndentationLeft + section.Indentation; indentRight -= section.IndentationRight; sectionIndentL -= section.IndentationLeft + section.Indentation; sectionIndentR -= section.IndentationRight; if (pageEvent != null) if (element.Type == Element.CHAPTER) pageEvent.OnChapterEnd(writer, this, IndentTop - currentHeight); else pageEvent.OnSectionEnd(writer, this, IndentTop - currentHeight); break; } case Element.LIST: { // we cast the element to a List List list = (List) element; if (list.Alignindent) { list.NormalizeIndentation(); } // we adjust the document listIndentLeft += list.IndentationLeft; indentRight += list.IndentationRight; // we process the items in the list element.Process(this); // some parameters are set back to normal again listIndentLeft -= list.IndentationLeft; indentRight -= list.IndentationRight; break; } case Element.LISTITEM: { // we cast the element to a ListItem ListItem listItem = (ListItem) element; AddSpacing(listItem.SpacingBefore, leading, listItem.Font); // we adjust the document alignment = listItem.Alignment; listIndentLeft += listItem.IndentationLeft; indentRight += listItem.IndentationRight; leading = listItem.Leading; CarriageReturn(); // we prepare the current line to be able to show us the listsymbol line.ListItem = listItem; // we process the item element.Process(this); AddSpacing(listItem.SpacingAfter, listItem.Leading, listItem.Font); // if the last line is justified, it should be aligned to the left if (line.HasToBeJustified()) { line.ResetAlignment(); } // some parameters are set back to normal again CarriageReturn(); listIndentLeft -= listItem.IndentationLeft; indentRight -= listItem.IndentationRight; break; } case Element.RECTANGLE: { Rectangle rectangle = (Rectangle) element; graphics.Rectangle(rectangle); pageEmpty = false; break; } case Element.PTABLE: { PdfPTable ptable = (PdfPTable)element; if (ptable.Size <= ptable.HeaderRows) break; //nothing to do // before every table, we add a new line and flush all lines indentLeft -= paraIndent + sectionIndentL; indentRight -= sectionIndentR; EnsureNewLine(); FlushLines(); indentLeft += paraIndent + sectionIndentL; indentRight += sectionIndentR; AddPTable(ptable); pageEmpty = false; NewLine(); break; } case Element.MULTI_COLUMN_TEXT: { EnsureNewLine(); FlushLines(); MultiColumnText multiText = (MultiColumnText) element; float height = multiText.Write(writer.DirectContent, this, IndentTop - currentHeight); currentHeight += height; text.MoveText(0, -1f* height); pageEmpty = false; break; } case Element.TABLE : { PdfTable table; if (element is PdfTable) { // Already pre-rendered table = (PdfTable)element; table.UpdateRowAdditions(); } else if (element is SimpleTable) { PdfPTable ptable = ((SimpleTable)element).CreatePdfPTable(); if (ptable.Size <= ptable.HeaderRows) break; //nothing to do // before every table, we add a new line and flush all lines EnsureNewLine(); FlushLines(); AddPTable(ptable); pageEmpty = false; break; } else if (element is Table) { try { PdfPTable ptable = ((Table)element).CreatePdfPTable(); if (ptable.Size <= ptable.HeaderRows) break; //nothing to do // before every table, we add a new line and flush all lines EnsureNewLine(); FlushLines(); AddPTable(ptable); pageEmpty = false; break; } catch (BadElementException) { // constructing the PdfTable // Before the table, add a blank line using offset or default leading float offset = ((Table)element).Offset; if (float.IsNaN(offset)) offset = leading; CarriageReturn(); lines.Add(new PdfLine(IndentLeft, IndentRight, alignment, offset)); currentHeight += offset; table = GetPdfTable((Table)element, false); } } else { return false; } Add(table, false); break; } case Element.JPEG: case Element.IMGRAW: case Element.IMGTEMPLATE: { //carriageReturn(); suggestion by Marc Campforts Add((Image) element); break; } case Element.MARKED: { MarkedObject mo; if (element is MarkedSection) { mo = ((MarkedSection)element).Title; if (mo != null) { mo.Process(this); } } mo = (MarkedObject)element; mo.Process(this); break; } default: return false; } lastElementType = element.Type; return true; }
// [C5] named objects: named destinations, javascript, embedded files /** * Adds named destinations in bulk. * Valid keys and values of the map can be found in the map * that is created by SimpleNamedDestination. * @param map a map with strings as keys for the names, * and structured strings as values for the destinations * @param page_offset number of pages that has to be added to * the page numbers in the destinations (useful if you * use this method in combination with PdfCopy). * @since iText 5.0 */ public void AddNamedDestinations(IDictionary<String, String> map, int page_offset) { int page; String dest; PdfDestination destination; foreach (KeyValuePair<string,string> entry in map) { dest = entry.Value; page = int.Parse(dest.Substring(0, dest.IndexOf(" "))); destination = new PdfDestination(dest.Substring(dest.IndexOf(" ") + 1)); AddNamedDestination(entry.Key, page + page_offset, destination); } }
/** * The local destination to where a local goto with the same * name will jump to. * @param name the name of this local destination * @param destination the <CODE>PdfDestination</CODE> with the jump coordinates * @return <CODE>true</CODE> if the local destination was added, * <CODE>false</CODE> if a local destination with the same name * already existed */ internal bool LocalDestination(String name, PdfDestination destination) { Destination dest; if (localDestinations.ContainsKey(name)) dest = localDestinations[name]; else dest = new Destination(); if (dest.destination != null) return false; dest.destination = destination; localDestinations[name] = dest; if (!destination.HasPage()) destination.AddPage(writer.CurrentPage); return true; }
/** * Constructs a <CODE>PdfOutline</CODE>. * <P> * This is the constructor for an <CODE>outline entry</CODE>. The open mode is * <CODE>true</CODE>. * * @param parent the parent of this outline item * @param destination the destination for this outline item * @param title the title of this outline item */ public PdfOutline(PdfOutline parent, PdfDestination destination, Paragraph title) : this(parent, destination, title, true) { }
/** * Constructs a <CODE>PdfOutline</CODE>. * <P> * This is the constructor for an <CODE>outline entry</CODE>. The open mode is * <CODE>true</CODE>. * * @param parent the parent of this outline item * @param destination the destination for this outline item * @param title the title of this outline item */ public PdfOutline(PdfOutline parent, PdfDestination destination, PdfString title) : this(parent, destination, title, true) { }
/** * The local destination to where a local goto with the same * name will jump. * @param name the name of this local destination * @param destination the <CODE>PdfDestination</CODE> with the jump coordinates * @return <CODE>true</CODE> if the local destination was added, * <CODE>false</CODE> if a local destination with the same name * already exists */ public bool LocalDestination(string name, PdfDestination destination) { return pdf.LocalDestination(name, destination); }
public void PdfNamedDestinationsOverflow() { Document document = new Document(); PdfAWriter writer = PdfAWriter.GetInstance(document, new FileStream(OUT + "pdfNamedDestinationsOverflow.pdf", FileMode.Create), PdfAConformanceLevel.PDF_A_1A); writer.CreateXmpMetadata(); writer.SetTagged(); document.Open(); document.AddLanguage("en-US"); Font font = FontFactory.GetFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 12); document.Add(new Paragraph("Hello World", font)); FileStream iccProfileFileStream = File.Open(RESOURCES + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read, FileShare.Read); ICC_Profile icc = ICC_Profile.GetInstance(iccProfileFileStream); iccProfileFileStream.Close(); writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc); PdfDocument pdf = writer.PdfDocument; for (int i = 0; i < 8200; i++) { PdfDestination dest = new PdfDestination(PdfDestination.FITV); pdf.LocalDestination("action" + i, dest); } document.Close(); }
public NamedDestinationImpl(string name, int page, PdfDestination destination) { Name = name; Page = page; Destination = destination; }
public override void OnParagraph(PdfWriter writer, Document document, float position) { PdfContentByte cb = writer.DirectContent; PdfDestination destination = new PdfDestination(PdfDestination.FITH, position); new PdfOutline(cb.RootOutline, destination, TITLE); }
/** * Constructs a <CODE>PdfOutline</CODE>. * <P> * This is the constructor for an <CODE>outline entry</CODE>. * * @param parent the parent of this outline item * @param destination the destination for this outline item * @param title the title of this outline item * @param open <CODE>true</CODE> if the children are visible */ public PdfOutline(PdfOutline parent, PdfDestination destination, PdfString title, bool open) : this(parent, destination, title.ToString(), true) { }
private void GenPdfWeekly(string TempFolderPath, string ImageFileName, string ReportPeriod) { //this creates a new destination to send the action to when the document is opened. The zoom in this instance is set to 0.75f (75%). Again I use doc.PageSize.Height to set the y coordinate to the top of the page. PdfDestination.XYZ is a specific PdfDestination Type that allows us to set the location and zoom. PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, _doc.PageSize.Height, 1.00f); _doc.Open(); //string imageURL_VinSplash = @ImageFileName; //iTextSharp.text.Image jpg_VinSplash = iTextSharp.text.Image.GetInstance(imageURL_VinSplash); string imageURL_VinSplash = Path.Combine(TempFolderPath, "VinCoverPage.png"); iTextSharp.text.Image jpg_VinSplash = iTextSharp.text.Image.GetInstance(imageURL_VinSplash); //Resize image depend upon your need jpg_VinSplash.ScalePercent(65f); //Give space before image jpg_VinSplash.SpacingBefore = 10f; //Give some space after the image jpg_VinSplash.SpacingAfter = 1f; jpg_VinSplash.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinSplash); _doc.NewPage(); string imageURL_VinDash = Path.Combine(TempFolderPath, "VinDashboard.png"); iTextSharp.text.Image jpg_VinDash = iTextSharp.text.Image.GetInstance(imageURL_VinDash); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinDash.ScalePercent(75f); //Give space before image jpg_VinDash.SpacingBefore = 10f; //Give some space after the image jpg_VinDash.SpacingAfter = 1f; jpg_VinDash.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinDash); _doc.NewPage(); string imageURL_VinHist = Path.Combine(TempFolderPath, "VinSpeedHist.png"); iTextSharp.text.Image jpg_VinHist = iTextSharp.text.Image.GetInstance(imageURL_VinHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinHist.ScalePercent(85f); //Give space before image jpg_VinHist.SpacingBefore = 10f; //Give some space after the image jpg_VinHist.SpacingAfter = 1f; jpg_VinHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinHist); string imageURL_VinEffHist = Path.Combine(TempFolderPath, "VinEfficiencyHist.png"); iTextSharp.text.Image jpg_VinEffHist = iTextSharp.text.Image.GetInstance(imageURL_VinEffHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinEffHist.ScalePercent(85f); //Give space before image jpg_VinEffHist.SpacingBefore = 10f; //Give some space after the image jpg_VinEffHist.SpacingAfter = 1f; jpg_VinEffHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinEffHist); string imageURL_VinAccelPedalHist = Path.Combine(TempFolderPath, "VinAccelPedalHist.png"); iTextSharp.text.Image jpg_VinAccelPedalHist = iTextSharp.text.Image.GetInstance(imageURL_VinAccelPedalHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinAccelPedalHist.ScalePercent(85f); //Give space before image jpg_VinAccelPedalHist.SpacingBefore = 10f; //Give some space after the image jpg_VinAccelPedalHist.SpacingAfter = 1f; jpg_VinAccelPedalHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinAccelPedalHist); string imageURL_VinBrakePedalHist = Path.Combine(TempFolderPath, "VinBrakePedalHist.png"); iTextSharp.text.Image jpg_VinBrakePedalHist = iTextSharp.text.Image.GetInstance(imageURL_VinBrakePedalHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinBrakePedalHist.ScalePercent(85f); //Give space before image jpg_VinBrakePedalHist.SpacingBefore = 10f; //Give some space after the image jpg_VinBrakePedalHist.SpacingAfter = 1f; jpg_VinBrakePedalHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinBrakePedalHist); string imageURL_VinChargingHist = Path.Combine(TempFolderPath, "VinChargingHist.png"); iTextSharp.text.Image jpg_VinChargingHist = iTextSharp.text.Image.GetInstance(imageURL_VinChargingHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinChargingHist.ScalePercent(85f); //Give space before image jpg_VinChargingHist.SpacingBefore = 10f; //Give some space after the image jpg_VinChargingHist.SpacingAfter = 1f; jpg_VinChargingHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinChargingHist); string imageURL_VinDCDCConverterPowerHist = Path.Combine(TempFolderPath, "VinDCDCConverterPowerHist.png"); iTextSharp.text.Image jpg_VinDCDCConverterPowerHist = iTextSharp.text.Image.GetInstance(imageURL_VinDCDCConverterPowerHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinDCDCConverterPowerHist.ScalePercent(85f); //Give space before image jpg_VinDCDCConverterPowerHist.SpacingBefore = 10f; //Give some space after the image jpg_VinDCDCConverterPowerHist.SpacingAfter = 1f; jpg_VinDCDCConverterPowerHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinDCDCConverterPowerHist); string imageURL_VinPECoolantTempHist = Path.Combine(TempFolderPath, "VinPECoolantTempHist.png"); iTextSharp.text.Image jpg_VinPECoolantTempHist = iTextSharp.text.Image.GetInstance(imageURL_VinPECoolantTempHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinPECoolantTempHist.ScalePercent(85f); //Give space before image jpg_VinPECoolantTempHist.SpacingBefore = 10f; //Give some space after the image jpg_VinPECoolantTempHist.SpacingAfter = 1f; jpg_VinPECoolantTempHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinPECoolantTempHist); string imageURL_VinChgEnergyHist = Path.Combine(TempFolderPath, "VinChgEnergyHist.png"); iTextSharp.text.Image jpg_VinChgEnergyHist = iTextSharp.text.Image.GetInstance(imageURL_VinChgEnergyHist); jpg_VinChgEnergyHist.ScalePercent(85f); //Give space before image jpg_VinChgEnergyHist.SpacingBefore = 10f; //Give some space after the image jpg_VinChgEnergyHist.SpacingAfter = 1f; jpg_VinChgEnergyHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinChgEnergyHist); string imageURL_VinVoltageHist = Path.Combine(TempFolderPath, "VinVoltageHist.png"); iTextSharp.text.Image jpg_VinVoltageHist = iTextSharp.text.Image.GetInstance(imageURL_VinVoltageHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinVoltageHist.ScalePercent(85f); //Give space before image jpg_VinVoltageHist.SpacingBefore = 10f; //Give some space after the image jpg_VinVoltageHist.SpacingAfter = 1f; jpg_VinVoltageHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinVoltageHist); string imageURL_VinBatteryTempMaxHist = Path.Combine(TempFolderPath, "VinBatteryTempMaxHist.png"); iTextSharp.text.Image jpg_VinBatteryTempMaxHist = iTextSharp.text.Image.GetInstance(imageURL_VinBatteryTempMaxHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinBatteryTempMaxHist.ScalePercent(85f); //Give space before image jpg_VinBatteryTempMaxHist.SpacingBefore = 10f; //Give some space after the image jpg_VinBatteryTempMaxHist.SpacingAfter = 1f; jpg_VinBatteryTempMaxHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinBatteryTempMaxHist); string imageURL_VinStatorTempHist = Path.Combine(TempFolderPath, "VinStatorTempHist.png"); iTextSharp.text.Image jpg_VinStatorTempHist = iTextSharp.text.Image.GetInstance(imageURL_VinStatorTempHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinStatorTempHist.ScalePercent(85f); //Give space before image jpg_VinStatorTempHist.SpacingBefore = 10f; //Give some space after the image jpg_VinStatorTempHist.SpacingAfter = 1f; jpg_VinStatorTempHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinStatorTempHist); string imageURL_VinTractionMotorRotorTempHist = Path.Combine(TempFolderPath, "VinTractionMotorRotorTempHist.png"); iTextSharp.text.Image jpg_VinTractionMotorRotorTempHist = iTextSharp.text.Image.GetInstance(imageURL_VinTractionMotorRotorTempHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinTractionMotorRotorTempHist.ScalePercent(85f); //Give space before image jpg_VinTractionMotorRotorTempHist.SpacingBefore = 10f; //Give some space after the image jpg_VinTractionMotorRotorTempHist.SpacingAfter = 1f; jpg_VinTractionMotorRotorTempHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinTractionMotorRotorTempHist); string imageURL_VinTractionMotorInverterTempHist = Path.Combine(TempFolderPath, "VinTractionMotorInverterTempHist.png"); iTextSharp.text.Image jpg_VinTractionMotorInverterTempHist = iTextSharp.text.Image.GetInstance(imageURL_VinTractionMotorInverterTempHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinTractionMotorInverterTempHist.ScalePercent(85f); //Give space before image jpg_VinTractionMotorInverterTempHist.SpacingBefore = 10f; //Give some space after the image jpg_VinTractionMotorInverterTempHist.SpacingAfter = 1f; jpg_VinTractionMotorInverterTempHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinTractionMotorInverterTempHist); string imageURL_VinTractionMotorIGBTTempHist = Path.Combine(TempFolderPath, "VinTractionMotorIGBTTempHist.png"); iTextSharp.text.Image jpg_VinTractionMotorIGBTTempHist = iTextSharp.text.Image.GetInstance(imageURL_VinTractionMotorIGBTTempHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinTractionMotorIGBTTempHist.ScalePercent(85f); //Give space before image jpg_VinTractionMotorIGBTTempHist.SpacingBefore = 10f; //Give some space after the image jpg_VinTractionMotorIGBTTempHist.SpacingAfter = 1f; jpg_VinTractionMotorIGBTTempHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinTractionMotorIGBTTempHist); string imageURL_VinRadiatorFanSpeedHist = Path.Combine(TempFolderPath, "VinRadiatorFanSpeedHist.png"); iTextSharp.text.Image jpg_VinRadiatorFanSpeedHist = iTextSharp.text.Image.GetInstance(imageURL_VinRadiatorFanSpeedHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinRadiatorFanSpeedHist.ScalePercent(85f); //Give space before image jpg_VinRadiatorFanSpeedHist.SpacingBefore = 10f; //Give some space after the image jpg_VinRadiatorFanSpeedHist.SpacingAfter = 1f; jpg_VinRadiatorFanSpeedHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinRadiatorFanSpeedHist); string imageURL_VinBatteryCoolantPumpHist = Path.Combine(TempFolderPath, "VinBatteryCoolantPumpHist.png"); iTextSharp.text.Image jpg_VinBatteryCoolantPumpHist = iTextSharp.text.Image.GetInstance(imageURL_VinBatteryCoolantPumpHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinBatteryCoolantPumpHist.ScalePercent(85f); //Give space before image jpg_VinBatteryCoolantPumpHist.SpacingBefore = 10f; //Give some space after the image jpg_VinBatteryCoolantPumpHist.SpacingAfter = 1f; jpg_VinBatteryCoolantPumpHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinBatteryCoolantPumpHist); string imageURL_VinMinAirPressureHist = Path.Combine(TempFolderPath, "VinMinAirPressureHist.png"); iTextSharp.text.Image jpg_VinMinAirPressureHist = iTextSharp.text.Image.GetInstance(imageURL_VinMinAirPressureHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinMinAirPressureHist.ScalePercent(85f); //Give space before image jpg_VinMinAirPressureHist.SpacingBefore = 10f; //Give some space after the image jpg_VinMinAirPressureHist.SpacingAfter = 1f; jpg_VinMinAirPressureHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinMinAirPressureHist); string imageURL_VinTransTempHist = Path.Combine(TempFolderPath, "VinTransTempHist.png"); iTextSharp.text.Image jpg_VinTransTempHist = iTextSharp.text.Image.GetInstance(imageURL_VinTransTempHist); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinTransTempHist.ScalePercent(85f); //Give space before image jpg_VinTransTempHist.SpacingBefore = 10f; //Give some space after the image jpg_VinTransTempHist.SpacingAfter = 1f; jpg_VinTransTempHist.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinTransTempHist); PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, _writer); // //set the open action for our writer object _writer.SetOpenAction(action); }
private void GenPdfDailyBattery(string TempFolderPath, string ImageFileName, string ReportPeriod) { //this creates a new destination to send the action to when the document is opened. The zoom in this instance is set to 0.75f (75%). Again I use doc.PageSize.Height to set the y coordinate to the top of the page. PdfDestination.XYZ is a specific PdfDestination Type that allows us to set the location and zoom. PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, _doc.PageSize.Height, 1.00f); _doc.Open(); //string imageURL_VinSplash = @ImageFileName; //iTextSharp.text.Image jpg_VinSplash = iTextSharp.text.Image.GetInstance(imageURL_VinSplash); string imageURL_VinSplash = Path.Combine(TempFolderPath, "VinCoverPage.png"); iTextSharp.text.Image jpg_VinSplash = iTextSharp.text.Image.GetInstance(imageURL_VinSplash); //Resize image depend upon your need jpg_VinSplash.ScalePercent(75f); //Give space before image jpg_VinSplash.SpacingBefore = 10f; //Give some space after the image jpg_VinSplash.SpacingAfter = 1f; jpg_VinSplash.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinSplash); _doc.NewPage(); string imageURL_VinDash = Path.Combine(TempFolderPath, "VinDashboard.png"); iTextSharp.text.Image jpg_VinDash = iTextSharp.text.Image.GetInstance(imageURL_VinDash); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_VinDash.ScalePercent(60f); //Give space before image jpg_VinDash.SpacingBefore = 10f; //Give some space after the image jpg_VinDash.SpacingAfter = 1f; jpg_VinDash.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_VinDash); string imageURL_SoCByPacks_Min = Path.Combine(TempFolderPath, "SoCByPacks_Min.png"); iTextSharp.text.Image jpg_SoCByPacks_Min = iTextSharp.text.Image.GetInstance(imageURL_SoCByPacks_Min); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_SoCByPacks_Min.ScalePercent(60f); //Give space before image jpg_SoCByPacks_Min.SpacingBefore = 10f; //Give some space after the image jpg_SoCByPacks_Min.SpacingAfter = 1f; jpg_SoCByPacks_Min.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_SoCByPacks_Min); string imageURL_SoCByPacks_Max = Path.Combine(TempFolderPath, "SoCByPacks_Max.png"); iTextSharp.text.Image jpg_SoCByPacks_Max = iTextSharp.text.Image.GetInstance(imageURL_SoCByPacks_Max); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_SoCByPacks_Max.ScalePercent(60f); //Give space before image jpg_SoCByPacks_Max.SpacingBefore = 10f; //Give some space after the image jpg_SoCByPacks_Max.SpacingAfter = 1f; jpg_SoCByPacks_Max.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_SoCByPacks_Max); string imageURL_CurrentByPacks = Path.Combine(TempFolderPath, "CurrentByPacks.png"); iTextSharp.text.Image jpg_CurrentByPacks = iTextSharp.text.Image.GetInstance(imageURL_CurrentByPacks); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_CurrentByPacks.ScalePercent(60f); //Give space before image jpg_CurrentByPacks.SpacingBefore = 10f; //Give some space after the image jpg_CurrentByPacks.SpacingAfter = 1f; jpg_CurrentByPacks.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_CurrentByPacks); string imageURL_TMaxByPacks_Max = Path.Combine(TempFolderPath, "TMaxByPacks.png"); iTextSharp.text.Image jpg_TMaxByPacks_Max = iTextSharp.text.Image.GetInstance(imageURL_TMaxByPacks_Max); //Resize image depend upon your need //jpg.ScaleToFit(chartWidth, chartHeight); // jpg_VinHist.ScaleToFit(600f, 250f); jpg_TMaxByPacks_Max.ScalePercent(60f); //Give space before image jpg_TMaxByPacks_Max.SpacingBefore = 10f; //Give some space after the image jpg_TMaxByPacks_Max.SpacingAfter = 1f; jpg_TMaxByPacks_Max.Alignment = Element.ALIGN_CENTER; _doc.Add(jpg_TMaxByPacks_Max); PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, _writer); // //set the open action for our writer object _writer.SetOpenAction(action); }
/** * Adds one named destination. * @param name the name for the destination * @param page the page number where you want to jump to * @param dest an explicit destination * @since iText 5.0 */ virtual public void AddNamedDestination(String name, int page, PdfDestination dest) { PdfDestination d = new PdfDestination(dest); d.AddPage(GetPageReference(page)); pdf.LocalDestination(name, d); }
/// <summary> /// 新しいアウトラインを登録する /// </summary> /// <param name="level">階層構造のレベル(1以上)</param> /// <param name="title">見出し文字列</param> /// <param name="cb">PDFデータ</param> /// <returns>生成されたアウトラインのパス</returns> public void AppendOutline(int level, UString title, PdfContentByte cb) { AppendOutlineNode(level, title); var path = Path(_currentNode); var destination = new PdfDestination(PdfDestination.INDIRECT); var added = cb.LocalDestination(path, destination); }
/** Creates a GoTo action to an internal page. * @param page the page to go. First page is 1 * @param dest the destination for the page * @param writer the writer for this action * @return a GoTo action */ public static PdfAction GotoLocalPage(int page, PdfDestination dest, PdfWriter writer) { PdfIndirectReference piref = writer.GetPageReference(page); PdfDestination d = new PdfDestination(dest); d.AddPage(piref); PdfAction action = new PdfAction(); action.Put(PdfName.S, PdfName.GOTO); action.Put(PdfName.D, d); return action; }
public override void Write(PdfWriter writer, Document doc) { PdfDestination destination = new PdfDestination(PdfDestination.XYZ, 20, writer.GetVerticalPosition(false), 0); IDictionary<String, Object> memory = context.GetMemory(); HeaderNode tree = null; if (memory.ContainsKey(HtmlPipelineContext.BOOKMARK_TREE)) tree = (HeaderNode)memory[HtmlPipelineContext.BOOKMARK_TREE]; int level = header.GetLevel(tag); if (null == tree) { // first h tag encounter tree = new HeaderNode(0, writer.RootOutline, null); } else { // calculate parent int lastLevel = tree.Level; if (lastLevel == level) { tree = tree.Parent; } else if (lastLevel > level) { while (lastLevel >= level) { lastLevel = tree.Parent.Level; tree = tree.Parent; } } } if (LOGGER.IsLogging(Level.TRACE)) { LOGGER.Trace(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.ADD_HEADER), title.ToString())); } HeaderNode node = new HeaderNode(level, new PdfOutline(tree.Outline, destination, title), tree); memory[HtmlPipelineContext.BOOKMARK_TREE] = node; }
/** * Constructs a <CODE>PdfOutline</CODE>. * <P> * This is the constructor for an <CODE>outline entry</CODE>. * * @param parent the parent of this outline item * @param destination the destination for this outline item * @param title the title of this outline item * @param open <CODE>true</CODE> if the children are visible */ public PdfOutline(PdfOutline parent, PdfDestination destination, string title, bool open) : base() { this.destination = destination; InitOutline(parent, title, open); }
public static PdfAnnotation CreateLink(PdfWriter writer, Rectangle rect, PdfName highlight, int page, PdfDestination dest) { PdfAnnotation annot = CreateLink(writer, rect, highlight); PdfIndirectReference piref = writer.GetPageReference(page); dest.AddPage(piref); annot.Put(PdfName.DEST, dest); return(annot); }
/** * The local destination to where a local goto with the same * name will jump to. * @param name the name of this local destination * @param destination the <CODE>PdfDestination</CODE> with the jump coordinates * @return <CODE>true</CODE> if the local destination was added, * <CODE>false</CODE> if a local destination with the same name * already existed */ internal bool LocalDestination(String name, PdfDestination destination) { Object[] obj = (Object[])localDestinations[name]; if (obj == null) obj = new Object[3]; if (obj[2] != null) return false; obj[2] = destination; localDestinations[name] = obj; if (!destination.HasPage()) destination.AddPage(writer.CurrentPage); return true; }
/** * Constructs a <CODE>PdfOutline</CODE>. * <P> * This is the constructor for an <CODE>outline entry</CODE>. * * @param parent the parent of this outline item * @param destination the destination for this outline item * @param title the title of this outline item * @param open <CODE>true</CODE> if the children are visible */ public PdfOutline(PdfOutline parent, PdfDestination destination, Paragraph title, bool open) : base() { StringBuilder buf = new StringBuilder(); foreach (Chunk chunk in title.Chunks) { buf.Append(chunk.Content); } this.destination = destination; InitOutline(parent, buf.ToString(), open); }
public static PdfAnnotation CreateLink(PdfWriter writer, Rectangle rect, PdfName highlight, int page, PdfDestination dest) { PdfAnnotation annot = CreateLink(writer, rect, highlight); PdfIndirectReference piref = writer.GetPageReference(page); dest.AddPage(piref); annot.Put(PdfName.DEST, dest); return annot; }
/** * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>. * * @param element the element to add * @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not. * @throws DocumentException when a document isn't open yet, or has been closed */ public override bool Add(IElement element) { if (writer != null && writer.IsPaused()) { return false; } if (element.Type != Element.DIV) { FlushFloatingElements(); } switch (element.Type) { // Information (headers) case Element.HEADER: info.Addkey(((Meta)element).Name, ((Meta)element).Content); break; case Element.TITLE: info.AddTitle(((Meta)element).Content); break; case Element.SUBJECT: info.AddSubject(((Meta)element).Content); break; case Element.KEYWORDS: info.AddKeywords(((Meta)element).Content); break; case Element.AUTHOR: info.AddAuthor(((Meta)element).Content); break; case Element.CREATOR: info.AddCreator(((Meta)element).Content); break; case Element.LANGUAGE: SetLanguage(((Meta)element).Content); break; case Element.PRODUCER: // you can not change the name of the producer info.AddProducer(); break; case Element.CREATIONDATE: // you can not set the creation date, only reset it info.AddCreationDate(); break; // content (text) case Element.CHUNK: { // if there isn't a current line available, we make one if (line == null) { CarriageReturn(); } // we cast the element to a chunk PdfChunk chunk = new PdfChunk((Chunk) element, anchorAction, tabSettings); // we try to add the chunk to the line, until we succeed { PdfChunk overflow; while ((overflow = line.Add(chunk)) != null) { CarriageReturn(); bool newlineSplit = chunk.IsNewlineSplit(); chunk = overflow; if (!newlineSplit) chunk.TrimFirstSpace(); } } pageEmpty = false; if (chunk.IsAttribute(Chunk.NEWPAGE)) { NewPage(); } break; } case Element.ANCHOR: { Anchor anchor = (Anchor) element; String url = anchor.Reference; leading = anchor.Leading; PushLeading(); if (url != null) { anchorAction = new PdfAction(url); } // we process the element element.Process(this); anchorAction = null; PopLeading(); break; } case Element.ANNOTATION: { if (line == null) { CarriageReturn(); } Annotation annot = (Annotation) element; Rectangle rect = new Rectangle(0, 0); if (line != null) rect = new Rectangle(annot.GetLlx(IndentRight - line.WidthLeft), annot.GetUry(IndentTop - currentHeight - 20), annot.GetUrx(IndentRight - line.WidthLeft + 20), annot.GetLly(IndentTop - currentHeight)); PdfAnnotation an = PdfAnnotationsImp.ConvertAnnotation(writer, annot, rect); annotationsImp.AddPlainAnnotation(an); pageEmpty = false; break; } case Element.PHRASE: { TabSettings backupTabSettings = tabSettings; if (((Phrase)element).TabSettings != null) tabSettings = ((Phrase)element).TabSettings; // we cast the element to a phrase and set the leading of the document leading = ((Phrase) element).TotalLeading; PushLeading(); // we process the element element.Process(this); tabSettings = backupTabSettings; PopLeading(); break; } case Element.PARAGRAPH: { TabSettings backupTabSettings = tabSettings; if (((Phrase)element).TabSettings != null) tabSettings = ((Phrase)element).TabSettings; // we cast the element to a paragraph Paragraph paragraph = (Paragraph) element; if (IsTagged(writer)) { FlushLines(); text.OpenMCBlock(paragraph); } AddSpacing(paragraph.SpacingBefore, leading, paragraph.Font); // we adjust the parameters of the document alignment = paragraph.Alignment; leading = paragraph.TotalLeading; PushLeading(); CarriageReturn(); // we don't want to make orphans/widows if (currentHeight + line.Height + leading > IndentTop - IndentBottom) { NewPage(); } indentation.indentLeft += paragraph.IndentationLeft; indentation.indentRight += paragraph.IndentationRight; CarriageReturn(); IPdfPageEvent pageEvent = writer.PageEvent; if (pageEvent != null && !isSectionTitle) pageEvent.OnParagraph(writer, this, IndentTop - currentHeight); // if a paragraph has to be kept together, we wrap it in a table object if (paragraph.KeepTogether) { CarriageReturn(); PdfPTable table = new PdfPTable(1); table.KeepTogether = paragraph.KeepTogether; table.WidthPercentage = 100f; PdfPCell cell = new PdfPCell(); cell.AddElement(paragraph); cell.Border = Rectangle.NO_BORDER; cell.Padding = 0; table.AddCell(cell); indentation.indentLeft -= paragraph.IndentationLeft; indentation.indentRight -= paragraph.IndentationRight; this.Add(table); indentation.indentLeft += paragraph.IndentationLeft; indentation.indentRight += paragraph.IndentationRight; } else { line.SetExtraIndent(paragraph.FirstLineIndent); element.Process(this); CarriageReturn(); AddSpacing(paragraph.SpacingAfter, paragraph.TotalLeading, paragraph.Font); } if (pageEvent != null && !isSectionTitle) pageEvent.OnParagraphEnd(writer, this, IndentTop - currentHeight); alignment = Element.ALIGN_LEFT; indentation.indentLeft -= paragraph.IndentationLeft; indentation.indentRight -= paragraph.IndentationRight; CarriageReturn(); tabSettings = backupTabSettings; PopLeading(); if (IsTagged(writer)) { FlushLines(); text.CloseMCBlock(paragraph); } break; } case Element.SECTION: case Element.CHAPTER: { // Chapters and Sections only differ in their constructor // so we cast both to a Section Section section = (Section) element; IPdfPageEvent pageEvent = writer.PageEvent; bool hasTitle = section.NotAddedYet && section.Title != null; // if the section is a chapter, we begin a new page if (section.TriggerNewPage) { NewPage(); } if (hasTitle) { float fith = IndentTop - currentHeight; int rotation = pageSize.Rotation; if (rotation == 90 || rotation == 180) fith = pageSize.Height - fith; PdfDestination destination = new PdfDestination(PdfDestination.FITH, fith); while (currentOutline.Level >= section.Depth) { currentOutline = currentOutline.Parent; } PdfOutline outline = new PdfOutline(currentOutline, destination, section.GetBookmarkTitle(), section.BookmarkOpen); currentOutline = outline; } // some values are set CarriageReturn(); indentation.sectionIndentLeft += section.IndentationLeft; indentation.sectionIndentRight += section.IndentationRight; if (section.NotAddedYet && pageEvent != null) if (element.Type == Element.CHAPTER) pageEvent.OnChapter(writer, this, IndentTop - currentHeight, section.Title); else pageEvent.OnSection(writer, this, IndentTop - currentHeight, section.Depth, section.Title); // the title of the section (if any has to be printed) if (hasTitle) { isSectionTitle = true; Add(section.Title); isSectionTitle = false; } indentation.sectionIndentLeft += section.Indentation; // we process the section element.Process(this); // some parameters are set back to normal again indentation.sectionIndentLeft -= (section.IndentationLeft + section.Indentation); indentation.sectionIndentRight -= section.IndentationRight; if (section.ElementComplete && pageEvent != null) if (element.Type == Element.CHAPTER) pageEvent.OnChapterEnd(writer, this, IndentTop - currentHeight); else pageEvent.OnSectionEnd(writer, this, IndentTop - currentHeight); break; } case Element.LIST: { // we cast the element to a List List list = (List) element; if (IsTagged(writer)) { FlushLines(); text.OpenMCBlock(list); } if (list.Alignindent) { list.NormalizeIndentation(); } // we adjust the document indentation.listIndentLeft += list.IndentationLeft; indentation.indentRight += list.IndentationRight; // we process the items in the list element.Process(this); // some parameters are set back to normal again indentation.listIndentLeft -= list.IndentationLeft; indentation.indentRight -= list.IndentationRight; CarriageReturn(); if (IsTagged(writer)) { FlushLines(); text.CloseMCBlock(list); } break; } case Element.LISTITEM: { // we cast the element to a ListItem ListItem listItem = (ListItem) element; if (IsTagged(writer)) { FlushLines(); text.OpenMCBlock(listItem); } AddSpacing(listItem.SpacingBefore, leading, listItem.Font); // we adjust the document alignment = listItem.Alignment; indentation.listIndentLeft += listItem.IndentationLeft; indentation.indentRight += listItem.IndentationRight; leading = listItem.TotalLeading; PushLeading(); CarriageReturn(); // we prepare the current line to be able to show us the listsymbol line.ListItem = listItem; // we process the item element.Process(this); AddSpacing(listItem.SpacingAfter, listItem.TotalLeading, listItem.Font); // if the last line is justified, it should be aligned to the left if (line.HasToBeJustified()) { line.ResetAlignment(); } // some parameters are set back to normal again CarriageReturn(); indentation.listIndentLeft -= listItem.IndentationLeft; indentation.indentRight -= listItem.IndentationRight; PopLeading(); if (IsTagged(writer)) { FlushLines(); text.CloseMCBlock(listItem.ListBody); text.CloseMCBlock(listItem); } break; } case Element.RECTANGLE: { Rectangle rectangle = (Rectangle) element; graphics.Rectangle(rectangle); pageEmpty = false; break; } case Element.PTABLE: { PdfPTable ptable = (PdfPTable)element; if (ptable.Size <= ptable.HeaderRows) break; //nothing to do // before every table, we add a new line and flush all lines EnsureNewLine(); FlushLines(); AddPTable(ptable); pageEmpty = false; NewLine(); break; } case Element.JPEG: case Element.JPEG2000: case Element.JBIG2: case Element.IMGRAW: case Element.IMGTEMPLATE: { //carriageReturn(); suggestion by Marc Campforts if(IsTagged(writer)) { FlushLines(); text.OpenMCBlock((Image)element); } Add((Image)element); if(IsTagged(writer)) { FlushLines(); text.CloseMCBlock((Image)element); } break; } case Element.YMARK: { IDrawInterface zh = (IDrawInterface)element; zh.Draw(graphics, IndentLeft, IndentBottom, IndentRight, IndentTop, IndentTop - currentHeight - (leadingStack.Count > 0 ? leading : 0)); pageEmpty = false; break; } case Element.MARKED: { MarkedObject mo; if (element is MarkedSection) { mo = ((MarkedSection)element).Title; if (mo != null) { mo.Process(this); } } mo = (MarkedObject)element; mo.Process(this); break; } case Element.WRITABLE_DIRECT: if (null != writer) { ((IWriterOperation)element).Write(writer, this); } break; case Element.DIV: EnsureNewLine(); FlushLines(); AddDiv((PdfDiv)element); pageEmpty = false; //newLine(); break; default: return false; } lastElementType = element.Type; return true; }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 PdfCollection collection = new PdfCollection(PdfCollection.HIDDEN); PdfCollectionSchema schema = _collectionSchema(); collection.Schema = schema; PdfCollectionSort sort = new PdfCollectionSort(KEYS); collection.Sort = sort; writer.Collection = collection; PdfCollectionItem collectionitem = new PdfCollectionItem(schema); PdfFileSpecification fs = PdfFileSpecification.FileEmbedded( writer, IMG_KUBRICK, "kubrick.jpg", null ); fs.AddDescription("Stanley Kubrick", false); collectionitem.AddItem(TYPE_FIELD, "JPEG"); fs.AddCollectionItem(collectionitem); writer.AddFileAttachment(fs); Image img = Image.GetInstance(IMG_BOX); document.Add(img); List list = new List(List.UNORDERED, 20); PdfDestination dest = new PdfDestination(PdfDestination.FIT); dest.AddFirst(new PdfNumber(1)); PdfTargetDictionary intermediate; PdfTargetDictionary target; Chunk chunk; ListItem item; PdfAction action = null; IEnumerable<Movie> box = PojoFactory.GetMovies(1) .Concat(PojoFactory.GetMovies(4)) ; StringBuilder sb = new StringBuilder(); foreach (Movie movie in box) { if (movie.Year > 1960) { sb.AppendLine(String.Format( "{0};{1};{2}", movie.MovieTitle, movie.Year, movie.Duration )); item = new ListItem(movie.MovieTitle); if (!"0278736".Equals(movie.Imdb)) { target = new PdfTargetDictionary(true); target.EmbeddedFileName = movie.Title; intermediate = new PdfTargetDictionary(true); intermediate.FileAttachmentPage = 1; intermediate.FileAttachmentIndex = 1; intermediate.AdditionalPath = target; action = PdfAction.GotoEmbedded(null, intermediate, dest, true); chunk = new Chunk(" (see info)"); chunk.SetAction(action); item.Add(chunk); } list.Add(item); } } document.Add(list); fs = PdfFileSpecification.FileEmbedded( writer, null, "kubrick.txt", Encoding.UTF8.GetBytes(sb.ToString()) ); fs.AddDescription("Kubrick box: the movies", false); collectionitem.AddItem(TYPE_FIELD, "TXT"); fs.AddCollectionItem(collectionitem); writer.AddFileAttachment(fs); PdfPTable table = new PdfPTable(1); table.SpacingAfter = 10; PdfPCell cell = new PdfPCell(new Phrase("All movies by Kubrick")); cell.Border = PdfPCell.NO_BORDER; fs = PdfFileSpecification.FileEmbedded( writer, null, KubrickMovies.FILENAME, Utility.PdfBytes(new KubrickMovies()) //new KubrickMovies().createPdf() ); collectionitem.AddItem(TYPE_FIELD, "PDF"); fs.AddCollectionItem(collectionitem); target = new PdfTargetDictionary(true); target.FileAttachmentPagename = "movies"; target.FileAttachmentName = "The movies of Stanley Kubrick"; cell.CellEvent = new PdfActionEvent( writer, PdfAction.GotoEmbedded(null, target, dest, true) ); cell.CellEvent = new FileAttachmentEvent( writer, fs, "The movies of Stanley Kubrick" ); cell.CellEvent = new LocalDestinationEvent(writer, "movies"); table.AddCell(cell); writer.AddFileAttachment(fs); cell = new PdfPCell(new Phrase("Kubrick DVDs")); cell.Border = PdfPCell.NO_BORDER; fs = PdfFileSpecification.FileEmbedded( writer, null, KubrickDvds.RESULT, new KubrickDvds().CreatePdf() ); collectionitem.AddItem(TYPE_FIELD, "PDF"); fs.AddCollectionItem(collectionitem); cell.CellEvent = new FileAttachmentEvent(writer, fs, "Kubrick DVDs"); table.AddCell(cell); writer.AddFileAttachment(fs); cell = new PdfPCell(new Phrase("Kubrick documentary")); cell.Border = PdfPCell.NO_BORDER; fs = PdfFileSpecification.FileEmbedded( writer, null, KubrickDocumentary.RESULT, new KubrickDocumentary().CreatePdf() ); collectionitem.AddItem(TYPE_FIELD, "PDF"); fs.AddCollectionItem(collectionitem); cell.CellEvent = new FileAttachmentEvent( writer, fs, "Kubrick Documentary" ); table.AddCell(cell); writer.AddFileAttachment(fs); document.NewPage(); document.Add(table); } }
/** * Adds one named destination. * @param name the name for the destination * @param page the page number where you want to jump to * @param dest an explicit destination * @since iText 5.0 */ public void AddNamedDestination(String name, int page, PdfDestination dest) { dest.AddPage(GetPageReference(page)); pdf.LocalDestination(name, dest); }