internal PdfName AddProperty(PdfName name, PdfIndirectReference reference) { name = TranslateName(name); propertyDictionary.Put(name, reference); return(name); }
/** * Creates a new layer. * @param name the name of the layer * @param writer the writer */ public PdfLayer(String name, PdfWriter writer) : base(PdfName.OCG) { Name = name; refi = writer.PdfIndirectReference; writer.RegisterLayer(this); }
internal PdfName AddShading(PdfName name, PdfIndirectReference reference) { name = TranslateName(name); shadingDictionary.Put(name, reference); return(name); }
internal PdfName AddExtGState(PdfName name, PdfIndirectReference reference) { name = TranslateName(name); extGStateDictionary.Put(name, reference); return(name); }
internal PdfName AddXObject(PdfName name, PdfIndirectReference reference) { name = TranslateName(name); xObjectDictionary.Put(name, reference); return(name); }
internal PdfName AddColor(PdfName name, PdfIndirectReference reference) { name = TranslateName(name); colorDictionary.Put(name, reference); return(name); }
/**Creates a Rendition action * @param file * @param fs * @param mimeType * @param ref * @return a Media Clip action * @throws IOException */ public static PdfAction Rendition(String file, PdfFileSpecification fs, String mimeType, PdfIndirectReference refi) { PdfAction js = new PdfAction(); js.Put(PdfName.S, PdfName.RENDITION); js.Put(PdfName.R, new PdfRendition(file, fs, mimeType)); js.Put(new PdfName("OP"), new PdfNumber(0)); js.Put(new PdfName("AN"), refi); return(js); }
/** * Constructs a new <CODE>PdfAction</CODE> of Subtype GoTo. * @param destination the destination to go to */ internal PdfAction(PdfIndirectReference destination) { Put(PdfName.S, PdfName.GOTO); Put(PdfName.D, destination); }
internal void AddPage(PdfIndirectReference page) { pages.Add(page); }
// returns the top parent to include in the catalog internal PdfIndirectReference WritePageTree() { if (pages.Count == 0) { throw new IOException("The document has no pages."); } int leaf = 1; ArrayList tParents = parents; ArrayList tPages = pages; ArrayList nextParents = new ArrayList(); while (true) { leaf *= leafSize; int stdCount = leafSize; int rightCount = tPages.Count % leafSize; if (rightCount == 0) { rightCount = leafSize; } for (int p = 0; p < tParents.Count; ++p) { int count; int thisLeaf = leaf; if (p == tParents.Count - 1) { count = rightCount; thisLeaf = pages.Count % leaf; if (thisLeaf == 0) { thisLeaf = leaf; } } else { count = stdCount; } PdfDictionary top = new PdfDictionary(PdfName.PAGES); top.Put(PdfName.COUNT, new PdfNumber(thisLeaf)); PdfArray kids = new PdfArray(); ArrayList intern = kids.ArrayList; intern.AddRange(tPages.GetRange(p * stdCount, count)); top.Put(PdfName.KIDS, kids); if (tParents.Count > 1) { if ((p % leafSize) == 0) { nextParents.Add(writer.PdfIndirectReference); } top.Put(PdfName.PARENT, (PdfIndirectReference)nextParents[p / leafSize]); } else { top.Put(PdfName.ITXT, new PdfString(Document.Release)); } writer.AddToBody(top, (PdfIndirectReference)tParents[p]); } if (tParents.Count == 1) { topParent = (PdfIndirectReference)tParents[0]; return(topParent); } tPages = tParents; tParents = nextParents; nextParents = new ArrayList(); } }
// constructor /** * Constructs a <CODE>PdfImage</CODE>-object. * * @param image the <CODE>Image</CODE>-object * @param name the <CODE>PdfName</CODE> for this image * @throws BadPdfFormatException on error */ public PdfImage(Image image, String name, PdfIndirectReference maskRef) { this.name = new PdfName(name); Put(PdfName.TYPE, PdfName.XOBJECT); Put(PdfName.SUBTYPE, PdfName.IMAGE); Put(PdfName.WIDTH, new PdfNumber(image.Width)); Put(PdfName.HEIGHT, new PdfNumber(image.Height)); if (image.Layer != null) { Put(PdfName.OC, image.Layer.Ref); } if (image.IsMask() && (image.Bpc == 1 || image.Bpc > 0xff)) { Put(PdfName.IMAGEMASK, PdfBoolean.PDFTRUE); } if (maskRef != null) { if (image.Smask) { Put(PdfName.SMASK, maskRef); } else { Put(PdfName.MASK, maskRef); } } if (image.IsMask() && image.Inverted) { Put(PdfName.DECODE, new PdfLiteral("[1 0]")); } if (image.Interpolation) { Put(PdfName.INTERPOLATE, PdfBoolean.PDFTRUE); } Stream isp = null; try { // Raw Image data if (image.IsImgRaw()) { // will also have the CCITT parameters int colorspace = image.Colorspace; int[] transparency = image.Transparency; if (transparency != null && !image.IsMask() && maskRef == null) { String s = "["; for (int k = 0; k < transparency.Length; ++k) { s += transparency[k] + " "; } s += "]"; Put(PdfName.MASK, new PdfLiteral(s)); } bytes = image.RawData; Put(PdfName.LENGTH, new PdfNumber(bytes.Length)); int bpc = image.Bpc; if (bpc > 0xff) { if (!image.IsMask()) { Put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); } Put(PdfName.BITSPERCOMPONENT, new PdfNumber(1)); Put(PdfName.FILTER, PdfName.CCITTFAXDECODE); int k = bpc - Image.CCITTG3_1D; PdfDictionary decodeparms = new PdfDictionary(); if (k != 0) { decodeparms.Put(PdfName.K, new PdfNumber(k)); } if ((colorspace & Image.CCITT_BLACKIS1) != 0) { decodeparms.Put(PdfName.BLACKIS1, PdfBoolean.PDFTRUE); } if ((colorspace & Image.CCITT_ENCODEDBYTEALIGN) != 0) { decodeparms.Put(PdfName.ENCODEDBYTEALIGN, PdfBoolean.PDFTRUE); } if ((colorspace & Image.CCITT_ENDOFLINE) != 0) { decodeparms.Put(PdfName.ENDOFLINE, PdfBoolean.PDFTRUE); } if ((colorspace & Image.CCITT_ENDOFBLOCK) != 0) { decodeparms.Put(PdfName.ENDOFBLOCK, PdfBoolean.PDFFALSE); } decodeparms.Put(PdfName.COLUMNS, new PdfNumber(image.Width)); decodeparms.Put(PdfName.ROWS, new PdfNumber(image.Height)); Put(PdfName.DECODEPARMS, decodeparms); } else { switch (colorspace) { case 1: Put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); if (image.Inverted) { Put(PdfName.DECODE, new PdfLiteral("[1 0]")); } break; case 3: Put(PdfName.COLORSPACE, PdfName.DEVICERGB); if (image.Inverted) { Put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0]")); } break; case 4: default: Put(PdfName.COLORSPACE, PdfName.DEVICECMYK); if (image.Inverted) { Put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0 1 0]")); } break; } PdfDictionary additional = image.Additional; if (additional != null) { Merge(additional); } if (image.IsMask() && (image.Bpc == 1 || image.Bpc > 8)) { Remove(PdfName.COLORSPACE); } Put(PdfName.BITSPERCOMPONENT, new PdfNumber(image.Bpc)); if (image.Deflated) { Put(PdfName.FILTER, PdfName.FLATEDECODE); } else { FlateCompress(image.CompressionLevel); } } return; } // GIF, JPEG or PNG String errorID; if (image.RawData == null) { isp = WebRequest.Create(image.Url).GetResponse().GetResponseStream(); errorID = image.Url.ToString(); } else { isp = new MemoryStream(image.RawData); errorID = "Byte array"; } switch (image.Type) { case Image.JPEG: Put(PdfName.FILTER, PdfName.DCTDECODE); switch (image.Colorspace) { case 1: Put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); break; case 3: Put(PdfName.COLORSPACE, PdfName.DEVICERGB); break; default: Put(PdfName.COLORSPACE, PdfName.DEVICECMYK); if (image.Inverted) { Put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0 1 0]")); } break; } Put(PdfName.BITSPERCOMPONENT, new PdfNumber(8)); if (image.RawData != null) { bytes = image.RawData; Put(PdfName.LENGTH, new PdfNumber(bytes.Length)); return; } streamBytes = new MemoryStream(); TransferBytes(isp, streamBytes, -1); break; case Image.JPEG2000: Put(PdfName.FILTER, PdfName.JPXDECODE); if (image.Colorspace > 0) { switch (image.Colorspace) { case 1: Put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); break; case 3: Put(PdfName.COLORSPACE, PdfName.DEVICERGB); break; default: Put(PdfName.COLORSPACE, PdfName.DEVICECMYK); break; } Put(PdfName.BITSPERCOMPONENT, new PdfNumber(image.Bpc)); } if (image.RawData != null) { bytes = image.RawData; Put(PdfName.LENGTH, new PdfNumber(bytes.Length)); return; } streamBytes = new MemoryStream(); TransferBytes(isp, streamBytes, -1); break; case Image.JBIG2: Put(PdfName.FILTER, PdfName.JBIG2DECODE); Put(PdfName.COLORSPACE, PdfName.DEVICEGRAY); Put(PdfName.BITSPERCOMPONENT, new PdfNumber(1)); if (image.RawData != null) { bytes = image.RawData; Put(PdfName.LENGTH, new PdfNumber(bytes.Length)); return; } streamBytes = new MemoryStream(); TransferBytes(isp, streamBytes, -1); break; default: throw new IOException(errorID + " is an unknown Image format."); } Put(PdfName.LENGTH, new PdfNumber(streamBytes.Length)); } finally { if (isp != null) { try{ isp.Close(); } catch { // empty on purpose } } } }