/// <summary> /// Gets the content stream of a page as a PdfStream object. /// @since 2.1.3 (the method already existed without param compressionLevel) /// </summary> /// <param name="pageNumber">the page of which you want the stream</param> /// <param name="compressionLevel">the compression level you want to apply to the stream</param> /// <returns>a PdfStream object</returns> internal PdfStream GetFormXObject(int pageNumber, int compressionLevel) { var page = reader.GetPageNRelease(pageNumber); var contents = PdfReader.GetPdfObjectRelease(page.Get(PdfName.Contents)); var dic = new PdfDictionary(); byte[] bout = null; if (contents != null) { if (contents.IsStream()) { dic.Merge((PrStream)contents); } else { bout = reader.GetPageContent(pageNumber, File); } } else { bout = new byte[0]; } dic.Put(PdfName.Resources, PdfReader.GetPdfObjectRelease(page.Get(PdfName.Resources))); dic.Put(PdfName.TYPE, PdfName.Xobject); dic.Put(PdfName.Subtype, PdfName.Form); var impPage = (PdfImportedPage)ImportedPages[pageNumber]; dic.Put(PdfName.Bbox, new PdfRectangle(impPage.BoundingBox)); var matrix = impPage.Matrix; if (matrix == null) { dic.Put(PdfName.Matrix, Identitymatrix); } else { dic.Put(PdfName.Matrix, matrix); } dic.Put(PdfName.Formtype, One); PrStream stream; if (bout == null) { stream = new PrStream((PrStream)contents, dic); } else { stream = new PrStream(reader, bout); stream.Merge(dic); } return(stream); }
public virtual void ReadCompressedPdfTest1() { string filename = TestResourceUtils.GetResourceAsTempFile(TEST_RESOURCES_PATH, "readCompressedPdfTest1.pdf"); PdfReader rdr = new PdfReader(filename); for (int i = 1; i <= rdr.NumberOfPages; i++) { PdfDictionary p = rdr.GetPageNRelease(i); Assert.AreEqual(PdfName.PAGE.ToString(), p.GetAsName(PdfName.TYPE).ToString()); } rdr.Close(); }
internal PdfObject GetResources(int pageNumber) { PdfObject obj = PdfReader.GetPdfObjectRelease(reader.GetPageNRelease(pageNumber).Get(PdfName.RESOURCES)); return(obj); }
public void AddDocument(PdfReader reader) { if (indirectMap.ContainsKey(reader)) { throw new ArgumentException(MessageLocalization.GetComposedMessage("document.1.has.already.been.added", reader.ToString())); } if (!reader.IsOpenedWithFullPermissions) throw new BadPasswordException(MessageLocalization.GetComposedMessage("pdfreader.not.opened.with.owner.password")); if (mergeFields) { reader.ConsolidateNamedDestinations(); reader.ShuffleSubsetNames(); for (int i = 1; i <= reader.NumberOfPages; i++) { PdfDictionary page = reader.GetPageNRelease(i); if (page != null && page.Contains(PdfName.ANNOTS)) { PdfArray annots = page.GetAsArray(PdfName.ANNOTS); if (annots != null) { for (int j = 0; j < annots.Size; j++) { PdfDictionary annot = annots.GetAsDict(j); if (annot != null) annot.Put(annotId, new PdfNumber(++annotIdCnt)); } } } } fields.Add(reader.AcroFields); UpdateCalculationOrder(reader); } bool tagged = PdfStructTreeController.CheckTagged(reader); mergeFieldsInternalCall = true; for (int i = 1; i <= reader.NumberOfPages; i++) { AddPage(GetImportedPage(reader, i, tagged && this.tagged)); } mergeFieldsInternalCall = false; }
virtual public void AddDocument(PdfReader reader) { if (!document.IsOpen()) { throw new DocumentException(MessageLocalization.GetComposedMessage("the.document.is.not.open.yet.you.can.only.add.meta.information")); } if (indirectMap.ContainsKey(reader)) { throw new ArgumentException(MessageLocalization.GetComposedMessage("document.1.has.already.been.added", reader.ToString())); } if (!reader.IsOpenedWithFullPermissions) throw new BadPasswordException(MessageLocalization.GetComposedMessage("pdfreader.not.opened.with.owner.password")); if (mergeFields) { reader.ConsolidateNamedDestinations(); reader.ShuffleSubsetNames(); for (int i = 1; i <= reader.NumberOfPages; i++) { PdfDictionary page = reader.GetPageNRelease(i); if (page != null && page.Contains(PdfName.ANNOTS)) { PdfArray annots = page.GetAsArray(PdfName.ANNOTS); if (annots != null) { for (int j = 0; j < annots.Size; j++) { PdfDictionary annot = annots.GetAsDict(j); if (annot != null) annot.Put(annotId, new PdfNumber(++annotIdCnt)); } } } } AcroFields acro = reader.AcroFields; // when a document with NeedAppearances is encountered, the flag is set // in the resulting document. bool needapp = !acro.GenerateAppearances; if (needapp) needAppearances = true; fields.Add(reader.AcroFields); UpdateCalculationOrder(reader); } bool tagged = this.tagged && PdfStructTreeController.CheckTagged(reader); mergeFieldsInternalCall = true; for (int i = 1; i <= reader.NumberOfPages; i++) { AddPage(GetImportedPage(reader, i, tagged)); } mergeFieldsInternalCall = false; }
/** * Copy document fields to a destination document. * @param reader a document where fields are copied from. * @throws DocumentException * @throws IOException */ public virtual void CopyDocumentFields(PdfReader reader) { if (!document.IsOpen()) { throw new DocumentException( MessageLocalization.GetComposedMessage("the.document.is.not.open.yet.you.can.only.add.meta.information")); } if (indirectMap.ContainsKey(reader)) { throw new ArgumentException(MessageLocalization.GetComposedMessage("document.1.has.already.been.added", reader.ToString())); } if (!reader.IsOpenedWithFullPermissions) throw new BadPasswordException(MessageLocalization.GetComposedMessage("pdfreader.not.opened.with.owner.password")); if (!mergeFields) throw new ArgumentException( MessageLocalization.GetComposedMessage( "1.method.can.be.only.used.in.mergeFields.mode.please.use.addDocument", "copyDocumentFields")); indirects = new Dictionary<RefKey, IndirectReferences>(); indirectMap[reader] = indirects; reader.ConsolidateNamedDestinations(); reader.ShuffleSubsetNames(); if (tagged && PdfStructTreeController.CheckTagged(reader)) { structTreeRootReference = (PRIndirectReference) reader.Catalog.Get(PdfName.STRUCTTREEROOT); if (structTreeController != null) { if (reader != structTreeController.reader) structTreeController.SetReader(reader); } else { structTreeController = new PdfStructTreeController(reader, this); } } IList<PdfObject> annotationsToBeCopied = new List<PdfObject>(); for (int i = 1; i <= reader.NumberOfPages; i++) { PdfDictionary page = reader.GetPageNRelease(i); if (page != null && page.Contains(PdfName.ANNOTS)) { PdfArray annots = page.GetAsArray(PdfName.ANNOTS); if (annots != null && annots.Size > 0) { if (importedPages.Count < i) throw new DocumentException( MessageLocalization.GetComposedMessage("there.are.not.enough.imported.pages.for.copied.fields")); indirectMap[reader][new RefKey(reader.pageRefs.GetPageOrigRef(i))] = new IndirectReferences(pageReferences[i - 1]); for (int j = 0; j < annots.Size; j++) { PdfDictionary annot = annots.GetAsDict(j); if (annot != null) { annot.Put(annotId, new PdfNumber(++annotIdCnt)); annotationsToBeCopied.Add(annots[j]); } } } } } foreach (PdfObject annot in annotationsToBeCopied) { CopyObject(annot); } if (tagged && structTreeController != null) structTreeController.AttachStructTreeRootKids(null); AcroFields acro = reader.AcroFields; bool needapp = !acro.GenerateAppearances; if (needapp) needAppearances = true; fields.Add(acro); UpdateCalculationOrder(reader); structTreeRootReference = null; }