public virtual void PrimitivesIntNumberIndirectTest() { String filename = "primitivesIntNumberIndirectTest.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + filename)); for (int i = 0; i < PageCount; i++) { PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(TestArray, GeneratePdfArrayWithIntNumbers(pdfDoc, true)); page.Flush(); } pdfDoc.Close(); }
public virtual void PrimitivesBooleanTest() { String filename = "primitivesBooleanTest.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + filename)); for (int i = 0; i < PageCount; i++) { PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(TestArray, GeneratePdfArrayWithBooleans(null, false)); page.Flush(); } pdfDoc.Close(); }
public virtual void UseObjectForMultipleTimes1() { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + "useObjectForMultipleTimes1.pdf")); PdfDictionary helloWorld = (PdfDictionary) new PdfDictionary().MakeIndirect(pdfDoc); helloWorld.Put(new PdfName("Hello"), new PdfString("World")); PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(new PdfName("HelloWorld"), helloWorld); page.Flush(); pdfDoc.GetCatalog().GetPdfObject().Put(new PdfName("HelloWorld"), helloWorld); pdfDoc.Close(); ValidateUseObjectForMultipleTimesTest(destinationFolder + "useObjectForMultipleTimes1.pdf"); }
public virtual void PrimitivesBooleanIndirectTest() { String filename = "primitivesBooleanIndirectTest.pdf"; FileStream fos = new FileStream(destinationFolder + filename, FileMode.Create); PdfWriter writer = new PdfWriter(fos); PdfDocument pdfDoc = new PdfDocument(writer); for (int i = 0; i < PageCount; i++) { PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(TestArray, GeneratePdfArrayWithBooleans(pdfDoc, true)); page.Flush(); } pdfDoc.Close(); }
public virtual void SimplePagesTest() { String filename = "simplePagesTest.pdf"; int pageCount = 111; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + filename)); for (int i = 0; i < pageCount; i++) { PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(PageNum, new PdfNumber(i + 1)); page.Flush(); } pdfDoc.Close(); VerifyPagesOrder(destinationFolder + filename, pageCount); }
public virtual void CopyObject8() { String exceptionMessage = null; PdfDocument pdfDoc1; FileStream fos1 = new FileStream(destinationFolder + "copyObject6_1.pdf", FileMode.Create); PdfWriter writer1 = new PdfWriter(fos1); pdfDoc1 = new PdfDocument(writer1); try { PdfPage page1 = pdfDoc1.AddNewPage(); PdfDictionary directDict = new PdfDictionary(); PdfObject indirectDict = ((PdfDictionary) new PdfDictionary().MakeIndirect(pdfDoc1)); page1.GetPdfObject().Put(new PdfName("HelloWorldDirect"), directDict); page1.GetPdfObject().Put(new PdfName("HelloWorldIndirect"), indirectDict); indirectDict.CopyTo(null); } catch (PdfException ex) { exceptionMessage = ex.Message; } finally { pdfDoc1.Close(); } NUnit.Framework.Assert.AreEqual(exceptionMessage, PdfException.DocumentToCopyToCannotBeNull); }
public virtual void ResourcesTest2() { MemoryStream baos = new MemoryStream(); PdfWriter writer = new PdfWriter(baos); PdfDocument document = new PdfDocument(writer); PdfPage page = document.AddNewPage(); PdfExtGState egs1 = new PdfExtGState(); PdfExtGState egs2 = new PdfExtGState(); PdfResources resources = page.GetResources(); resources.AddExtGState(egs1); resources.AddExtGState(egs2); document.Close(); PdfReader reader = new PdfReader(new MemoryStream(baos.ToArray())); document = new PdfDocument(reader, new PdfWriter(new ByteArrayOutputStream())); page = document.GetPage(1); resources = page.GetResources(); ICollection <PdfName> names = resources.GetResourceNames(); NUnit.Framework.Assert.AreEqual(2, names.Count); String[] expectedNames = new String[] { "Gs1", "Gs2" }; int i = 0; foreach (PdfName name in names) { NUnit.Framework.Assert.AreEqual(expectedNames[i++], name.GetValue()); } PdfExtGState egs3 = new PdfExtGState(); PdfName n3 = resources.AddExtGState(egs3); NUnit.Framework.Assert.AreEqual("Gs3", n3.GetValue()); PdfDictionary egsResources = page.GetPdfObject().GetAsDictionary(PdfName.Resources).GetAsDictionary(PdfName .ExtGState); PdfObject e1 = egsResources.Get(new PdfName("Gs1"), false); PdfName n1 = resources.AddExtGState(e1); NUnit.Framework.Assert.AreEqual("Gs1", n1.GetValue()); PdfObject e2 = egsResources.Get(new PdfName("Gs2")); PdfName n2 = resources.AddExtGState(e2); NUnit.Framework.Assert.AreEqual("Gs2", n2.GetValue()); PdfObject e4 = (PdfObject)e2.Clone(); PdfName n4 = resources.AddExtGState(e4); NUnit.Framework.Assert.AreEqual("Gs4", n4.GetValue()); document.Close(); }
public virtual void ReversePagesTest() { String filename = "reversePagesTest.pdf"; int pageCount = 111; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + filename)); for (int i = pageCount; i > 0; i--) { PdfPage page = new PdfPage(pdfDoc, pdfDoc.GetDefaultPageSize()); pdfDoc.AddPage(1, page); page.GetPdfObject().Put(PageNum, new PdfNumber(i)); page.Flush(); } pdfDoc.Close(); VerifyPagesOrder(destinationFolder + filename, pageCount); }
public virtual void CopyObject6() { FileStream fos = new FileStream(destinationFolder + "copyObject6_1.pdf", FileMode.Create); PdfWriter writer = new PdfWriter(fos); PdfDocument pdfDoc = new PdfDocument(writer); PdfDictionary helloWorld = ((PdfDictionary) new PdfDictionary().MakeIndirect(pdfDoc)); helloWorld.Put(new PdfName("Hello"), new PdfString("World")); PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(new PdfName("HelloWorld"), helloWorld); pdfDoc.Close(); pdfDoc = new PdfDocument(new PdfReader(destinationFolder + "copyObject6_1.pdf")); helloWorld = (PdfDictionary)pdfDoc.GetPage(1).GetPdfObject().Get(new PdfName("HelloWorld")); PdfDocument pdfDoc1 = new PdfDocument(new PdfWriter(new FileStream(destinationFolder + "copyObject6_2.pdf" , FileMode.Create))); PdfPage page1 = pdfDoc1.AddNewPage(); page1.GetPdfObject().Put(new PdfName("HelloWorldCopy1"), ((PdfDictionary)helloWorld.CopyTo(pdfDoc1))); page1.GetPdfObject().Put(new PdfName("HelloWorldCopy2"), ((PdfDictionary)helloWorld.CopyTo(pdfDoc1, true)) ); page1.GetPdfObject().Put(new PdfName("HelloWorldCopy3"), ((PdfDictionary)helloWorld.CopyTo(pdfDoc1, false) )); page1.Flush(); pdfDoc.Close(); pdfDoc1.Close(); PdfReader reader = new PdfReader(destinationFolder + "copyObject6_2.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt"); PdfObject obj1 = pdfDocument.GetPage(1).GetPdfObject().Get(new PdfName("HelloWorldCopy1")); PdfIndirectReference ref1 = obj1.GetIndirectReference(); NUnit.Framework.Assert.AreEqual(6, ref1.objNr); NUnit.Framework.Assert.AreEqual(0, ref1.genNr); PdfObject obj2 = pdfDocument.GetPage(1).GetPdfObject().Get(new PdfName("HelloWorldCopy2")); PdfIndirectReference ref2 = obj2.GetIndirectReference(); NUnit.Framework.Assert.AreEqual(7, ref2.GetObjNumber()); NUnit.Framework.Assert.AreEqual(0, ref2.GetGenNumber()); PdfObject obj3 = pdfDocument.GetPage(1).GetPdfObject().Get(new PdfName("HelloWorldCopy3")); PdfIndirectReference ref3 = obj3.GetIndirectReference(); NUnit.Framework.Assert.AreEqual(7, ref3.GetObjNumber()); NUnit.Framework.Assert.AreEqual(0, ref3.GetGenNumber()); reader.Close(); }
public virtual void RandomNumberPagesTest() { String filename = "randomNumberPagesTest.pdf"; int pageCount = 3000; int[] indexes = new int[pageCount]; for (int i = 0; i < indexes.Length; i++) { indexes[i] = i + 1; } Random rnd = new Random(); for (int i_1 = indexes.Length - 1; i_1 > 0; i_1--) { int index = rnd.Next(i_1 + 1); int a = indexes[index]; indexes[index] = indexes[i_1]; indexes[i_1] = a; } FileStream fos = new FileStream(destinationFolder + filename, FileMode.Create); PdfWriter writer = new PdfWriter(fos); PdfDocument pdfDoc = new PdfDocument(writer); for (int i_2 = 0; i_2 < indexes.Length; i_2++) { PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(PageNum, new PdfNumber(indexes[i_2])); } for (int i_3 = 1; i_3 < pageCount; i_3++) { for (int j = i_3 + 1; j <= pageCount; j++) { int j_page = pdfDoc.GetPage(j).GetPdfObject().GetAsNumber(PageNum).IntValue(); int i_page = pdfDoc.GetPage(i_3).GetPdfObject().GetAsNumber(PageNum).IntValue(); if (j_page < i_page) { PdfPage page = pdfDoc.RemovePage(j); pdfDoc.AddPage(i_3 + 1, page); page = pdfDoc.RemovePage(i_3); pdfDoc.AddPage(j, page); } } NUnit.Framework.Assert.IsTrue(VerifyIntegrity(pdfDoc.GetCatalog().GetPageTree()) == -1); } pdfDoc.Close(); VerifyPagesOrder(destinationFolder + filename, pageCount); }
public virtual void PrimitivesIntNumberTest() { String filename = "primitivesIntNumberTest.pdf"; FileStream fos = new FileStream(destinationFolder + filename, FileMode.Create); PdfWriter writer = new PdfWriter(fos); PdfDocument pdfDoc = new PdfDocument(writer); for (int i = 0; i < PageCount; i++) { PdfPage page = pdfDoc.AddNewPage(); PdfArray array = GeneratePdfArrayWithIntNumbers(null, false); page.GetPdfObject().Put(TestArray, array); array.Flush(); page.Flush(); } pdfDoc.Close(); }
public virtual void UseObjectForMultipleTimes3() { FileStream fos = new FileStream(destinationFolder + "useObjectForMultipleTimes3.pdf", FileMode.Create); PdfWriter writer = new PdfWriter(fos); PdfDocument pdfDoc = new PdfDocument(writer); PdfDictionary helloWorld = ((PdfDictionary) new PdfDictionary().MakeIndirect(pdfDoc)); helloWorld.Put(new PdfName("Hello"), new PdfString("World")); PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(new PdfName("HelloWorld"), helloWorld); page.Flush(); helloWorld.Flush(); pdfDoc.GetCatalog().GetPdfObject().Put(new PdfName("HelloWorld"), helloWorld); pdfDoc.Close(); ValidateUseObjectForMultipleTimesTest(destinationFolder + "useObjectForMultipleTimes3.pdf"); }
public virtual void GetPageByDictionary() { String filename = sourceFolder + "1000PagesDocument.pdf"; PdfReader reader = new PdfReader(filename); PdfDocument pdfDoc = new PdfDocument(reader); PdfObject[] pageDictionaries = new PdfObject[] { pdfDoc.GetPdfObject(4), pdfDoc.GetPdfObject(255), pdfDoc. GetPdfObject(512), pdfDoc.GetPdfObject(1023), pdfDoc.GetPdfObject(2049), pdfDoc.GetPdfObject(3100) }; foreach (PdfObject pageObject in pageDictionaries) { PdfDictionary pageDictionary = (PdfDictionary)pageObject; NUnit.Framework.Assert.AreEqual(PdfName.Page, pageDictionary.Get(PdfName.Type)); PdfPage page = pdfDoc.GetPage(pageDictionary); NUnit.Framework.Assert.AreEqual(pageDictionary, page.GetPdfObject()); } pdfDoc.Close(); }
public virtual void RandomObjectPagesTest() { String filename = "randomObjectPagesTest.pdf"; int pageCount = 10000; int[] indexes = new int[pageCount]; for (int i = 0; i < indexes.Length; i++) { indexes[i] = i + 1; } Random rnd = new Random(); for (int i_1 = indexes.Length - 1; i_1 > 0; i_1--) { int index = rnd.Next(i_1 + 1); int a = indexes[index]; indexes[index] = indexes[i_1]; indexes[i_1] = a; } FileStream fos = new FileStream(destinationFolder + filename, FileMode.Create); PdfWriter writer = new PdfWriter(fos); PdfDocument document = new PdfDocument(writer); PdfPage[] pages = new PdfPage[pageCount]; for (int i_2 = 0; i_2 < indexes.Length; i_2++) { PdfPage page = document.AddNewPage(); page.GetPdfObject().Put(PageNum, new PdfNumber(indexes[i_2])); //page.flush(); pages[indexes[i_2] - 1] = page; } int xrefSize = document.GetXref().Size(); PdfPage testPage = document.RemovePage(1000); NUnit.Framework.Assert.IsTrue(testPage.GetPdfObject().GetIndirectReference() == null); document.AddPage(1000, testPage); NUnit.Framework.Assert.IsTrue(testPage.GetPdfObject().GetIndirectReference().GetObjNumber() < xrefSize); for (int i_3 = 0; i_3 < pages.Length; i_3++) { NUnit.Framework.Assert.AreEqual(true, document.RemovePage(pages[i_3]), "Remove page"); document.AddPage(i_3 + 1, pages[i_3]); } document.Close(); VerifyPagesOrder(destinationFolder + filename, pageCount); }
/// <summary>This method removes all outlines associated with a given page</summary> /// <param name="page"/> /// <exception cref="iText.Kernel.PdfException"/> internal virtual void RemoveOutlines(PdfPage page) { if (GetDocument().GetWriter() == null) { return; } if (HasOutlines()) { GetOutlines(false); if (pagesWithOutlines.Count > 0) { foreach (PdfOutline outline in pagesWithOutlines.Get(page.GetPdfObject())) { outline.RemoveOutline(); } } } }
public virtual void RandomNumberPagesTest() { String filename = "randomNumberPagesTest.pdf"; int pageCount = 3000; int[] indexes = new int[pageCount]; for (int i = 0; i < indexes.Length; i++) { indexes[i] = i + 1; } Random rnd = new Random(); for (int i = indexes.Length - 1; i > 0; i--) { int index = rnd.Next(i + 1); int a = indexes[index]; indexes[index] = indexes[i]; indexes[i] = a; } PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + filename)); for (int i = 0; i < indexes.Length; i++) { PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(PageNum, new PdfNumber(indexes[i])); } for (int i = 1; i < pageCount; i++) { for (int j = i + 1; j <= pageCount; j++) { int j_page = pdfDoc.GetPage(j).GetPdfObject().GetAsNumber(PageNum).IntValue(); int i_page = pdfDoc.GetPage(i).GetPdfObject().GetAsNumber(PageNum).IntValue(); if (j_page < i_page) { pdfDoc.MovePage(i, j); pdfDoc.MovePage(j, i); } } NUnit.Framework.Assert.IsTrue(VerifyIntegrity(pdfDoc.GetCatalog().GetPageTree()) == -1); } pdfDoc.Close(); VerifyPagesOrder(destinationFolder + filename, pageCount); }
public virtual void RandomObjectPagesTest() { String filename = "randomObjectPagesTest.pdf"; int pageCount = 10000; int[] indexes = new int[pageCount]; for (int i = 0; i < indexes.Length; i++) { indexes[i] = i + 1; } Random rnd = new Random(); for (int i = indexes.Length - 1; i > 0; i--) { int index = rnd.Next(i + 1); int a = indexes[index]; indexes[index] = indexes[i]; indexes[i] = a; } PdfDocument document = new PdfDocument(new PdfWriter(destinationFolder + filename)); PdfPage[] pages = new PdfPage[pageCount]; for (int i = 0; i < indexes.Length; i++) { PdfPage page = document.AddNewPage(); page.GetPdfObject().Put(PageNum, new PdfNumber(indexes[i])); //page.flush(); pages[indexes[i] - 1] = page; } int testPageXref = document.GetPage(1000).GetPdfObject().GetIndirectReference().GetObjNumber(); document.MovePage(1000, 1000); NUnit.Framework.Assert.AreEqual(testPageXref, document.GetPage(1000).GetPdfObject().GetIndirectReference() .GetObjNumber()); for (int i = 0; i < pages.Length; i++) { NUnit.Framework.Assert.IsTrue(document.MovePage(pages[i], i + 1), "Move page"); } document.Close(); VerifyPagesOrder(destinationFolder + filename, pageCount); }
public virtual void ModifyAnnotationOnlyAppendMode() { String input = sourceFolder + "100pages.pdf"; String output = destinationFolder + "modifyAnnotOnly.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(input), new PdfWriter(output), new StampingProperties() .UseAppendMode()); PdfPage page = pdfDoc.GetPage(1); PdfIndirectReference pageIndRef = page.GetPdfObject().GetIndirectReference(); PdfDictionary annotObj = page.GetAnnotations()[0].SetRectangle(new PdfArray(new Rectangle(0, 0, 300, 300)) ).SetPage(page).GetPdfObject(); PageFlushingHelper flushingHelper = new PageFlushingHelper(pdfDoc); flushingHelper.AppendModeFlush(1); // annotation is flushed NUnit.Framework.Assert.IsTrue(annotObj.IsFlushed()); // page is not flushed NUnit.Framework.Assert.IsFalse(pageIndRef.CheckState(PdfObject.FLUSHED)); // page is released NUnit.Framework.Assert.IsNull(pageIndRef.refersTo); // exception is not thrown pdfDoc.Close(); }
public virtual void RemoveFlushedPage() { String filename = "removeFlushedPage.pdf"; int pageCount = 10; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationFolder + filename)); PdfPage removedPage = pdfDoc.AddNewPage(); int removedPageObjectNumber = removedPage.GetPdfObject().GetIndirectReference().GetObjNumber(); removedPage.Flush(); pdfDoc.RemovePage(removedPage); for (int i = 0; i < pageCount; i++) { PdfPage page = pdfDoc.AddNewPage(); page.GetPdfObject().Put(PageNum, new PdfNumber(i + 1)); page.Flush(); } NUnit.Framework.Assert.IsTrue(pdfDoc.RemovePage(pdfDoc.GetPage(pageCount)), "Remove last page"); NUnit.Framework.Assert.IsFalse(pdfDoc.GetXref().Get(removedPageObjectNumber).CheckState(PdfObject.FREE), "Free reference" ); pdfDoc.Close(); VerifyPagesOrder(destinationFolder + filename, pageCount - 1); }
private static ICollection<PdfIndirectReference> GetAllUsedNonFlushedOCGs(IDictionary<PdfPage, PdfPage> page2page , PdfDictionary toOcProperties) { // NOTE: the PDF is considered to be valid and therefore the presence of OСG in OCProperties.OCGs is not checked ICollection<PdfIndirectReference> fromUsedOcgs = new LinkedHashSet<PdfIndirectReference>(); // Visit the pages in parallel to find non-flush OSGs PdfPage[] fromPages = page2page.Keys.ToArray(new PdfPage[0]); PdfPage[] toPages = page2page.Values.ToArray(new PdfPage[0]); for (int i = 0; i < toPages.Length; i++) { PdfPage fromPage = fromPages[i]; PdfPage toPage = toPages[i]; // Copy OCGs from annotations IList<PdfAnnotation> toAnnotations = toPage.GetAnnotations(); IList<PdfAnnotation> fromAnnotations = fromPage.GetAnnotations(); for (int j = 0; j < toAnnotations.Count; j++) { if (!toAnnotations[j].IsFlushed()) { PdfDictionary toAnnotDict = toAnnotations[j].GetPdfObject(); PdfDictionary fromAnnotDict = fromAnnotations[j].GetPdfObject(); PdfAnnotation toAnnot = toAnnotations[j]; PdfAnnotation fromAnnot = fromAnnotations[j]; if (!toAnnotDict.IsFlushed()) { iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromOcDict(toAnnotDict.GetAsDictionary(PdfName.OC ), fromAnnotDict.GetAsDictionary(PdfName.OC), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetNormalAppearanceObject(), fromAnnot.GetNormalAppearanceObject(), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetRolloverAppearanceObject( ), fromAnnot.GetRolloverAppearanceObject(), fromUsedOcgs, toOcProperties); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetDownAppearanceObject(), fromAnnot .GetDownAppearanceObject(), fromUsedOcgs, toOcProperties); } } } PdfDictionary toResources = toPage.GetPdfObject().GetAsDictionary(PdfName.Resources); PdfDictionary fromResources = fromPage.GetPdfObject().GetAsDictionary(PdfName.Resources); iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromResources(toResources, fromResources, fromUsedOcgs , toOcProperties); } return fromUsedOcgs; }
/// <summary> /// Insert /// <seealso>PdfPage</seealso> /// into specific one-based position. /// </summary> /// <param name="index">one-base index of the page</param> /// <param name="pdfPage"> /// /// <see cref="PdfPage"/> /// to insert. /// </param> public virtual void AddPage(int index, PdfPage pdfPage) { --index; if (index > pageRefs.Count) { throw new IndexOutOfRangeException("index"); } if (index == pageRefs.Count) { AddPage(pdfPage); return; } LoadPage(index); pdfPage.MakeIndirect(document); int parentIndex = FindPageParent(index); PdfPages parentPages = parents[parentIndex]; parentPages.AddPage(index, pdfPage); pdfPage.parentPages = parentPages; CorrectPdfPagesFromProperty(parentIndex + 1, +1); pageRefs.Add(index, pdfPage.GetPdfObject()); pages.Add(index, pdfPage); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="System.Exception"/> /// <exception cref="Org.BouncyCastle.Security.GeneralSecurityException"/> public virtual void CheckEncryptedWithCertificateDocumentAppending(String filename, X509Certificate certificate ) { String srcFileName = destinationFolder + filename; String outFileName = destinationFolder + "appended_" + filename; PdfReader reader = new PdfReader(srcFileName, new ReaderProperties().SetPublicKeySecurityParams(certificate , GetPrivateKey())); PdfDocument document = new PdfDocument(reader, new PdfWriter(outFileName), new StampingProperties().UseAppendMode ()); PdfPage newPage = document.AddNewPage(); String helloWorldStringValue = "Hello world string"; newPage.Put(PdfName.Default, new PdfString(helloWorldStringValue)); WriteTextBytesOnPageContent(newPage, "Hello world page_2!"); document.Close(); PdfReader appendedDocReader = new PdfReader(outFileName, new ReaderProperties().SetPublicKeySecurityParams (certificate, GetPrivateKey())); PdfDocument appendedDoc = new PdfDocument(appendedDocReader); PdfPage secondPage = appendedDoc.GetPage(2); PdfString helloWorldPdfString = secondPage.GetPdfObject().GetAsString(PdfName.Default); String actualHelloWorldStringValue = helloWorldPdfString != null?helloWorldPdfString.GetValue() : null; NUnit.Framework.Assert.AreEqual(actualHelloWorldStringValue, helloWorldStringValue); appendedDoc.Close(); CompareTool compareTool = new CompareTool().EnableEncryptionCompare(); compareTool.GetOutReaderProperties().SetPublicKeySecurityParams(certificate, GetPrivateKey()); compareTool.GetCmpReaderProperties().SetPublicKeySecurityParams(certificate, GetPrivateKey()); String compareResult = compareTool.CompareByContent(outFileName, sourceFolder + "cmp_appended_" + filename , destinationFolder, "diff_"); if (compareResult != null) { NUnit.Framework.Assert.Fail(compareResult); } }
public virtual void FullCompressionAppendMode() { PdfWriter writer = new PdfWriter(destinationFolder + "fullCompressionAppendMode.pdf", new WriterProperties ().SetFullCompressionMode(true).SetCompressionLevel(CompressionConstants.NO_COMPRESSION)); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "fullCompressionDoc.pdf"), writer, new StampingProperties().UseAppendMode()); PdfPage page = pdfDocument.GetPage(1); PdfStream contentStream = new PdfStream(); String contentStr = iText.IO.Util.JavaUtil.GetStringForBytes(pdfDocument.GetPage(1).GetFirstContentStream( ).GetBytes(), Encoding.ASCII); contentStream.SetData(contentStr.Replace("/F1 16", "/F1 24").GetBytes(Encoding.ASCII)); page.GetPdfObject().Put(PdfName.Contents, contentStream); page.SetModified(); pdfDocument.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "fullCompressionAppendMode.pdf" , sourceFolder + "cmp_fullCompressionAppendMode.pdf", destinationFolder, "diff_")); PdfDocument assertDoc = new PdfDocument(new PdfReader(destinationFolder + "fullCompressionAppendMode.pdf") ); NUnit.Framework.Assert.IsTrue(assertDoc.GetPdfObject(9).IsStream()); NUnit.Framework.Assert.AreEqual(1, ((PdfDictionary)assertDoc.GetPdfObject(9)).GetAsNumber(PdfName.N).IntValue ()); }
private bool FlushPage(int pageNum) { PdfPage page = pdfDoc.GetPage(pageNum); if (page.IsFlushed()) { return(false); } bool pageChanged = false; if (!release) { pdfDoc.DispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.END_PAGE, page)); InitCurrentLayers(pdfDoc); } PdfDictionary pageDict = page.GetPdfObject(); // Using PdfPage package internal methods in order to avoid PdfResources initialization: initializing PdfResources // limits processing possibilities only to cases in which resources and specific resource type dictionaries are not flushed. PdfDictionary resourcesDict = page.InitResources(false); // inits /Resources dict entry if not inherited and not created yet PdfResources resources = page.GetResources(false); if (resources != null && resources.IsModified() && !resources.IsReadOnly()) { resourcesDict = resources.GetPdfObject(); pageDict.Put(PdfName.Resources, resources.GetPdfObject()); pageDict.SetModified(); pageChanged = true; } if (!resourcesDict.IsFlushed()) { FlushDictRecursively(resourcesDict, null); FlushOrRelease(resourcesDict); } FlushDictRecursively(pageDict, pageContext); if (release) { if (!page.GetPdfObject().IsModified()) { pdfDoc.GetCatalog().GetPageTree().ReleasePage(pageNum); page.UnsetForbidRelease(); page.GetPdfObject().Release(); } } else { if (pdfDoc.IsTagged() && !pdfDoc.GetStructTreeRoot().IsFlushed()) { page.TryFlushPageTags(); } if (!pdfDoc.IsAppendMode() || page.GetPdfObject().IsModified()) { page.ReleaseInstanceFields(); page.GetPdfObject().Flush(); } else { // it's append mode pdfDoc.GetCatalog().GetPageTree().ReleasePage(pageNum); page.UnsetForbidRelease(); page.GetPdfObject().Release(); } } layersRefs.Clear(); return(pageChanged); }
/// <summary> /// Flushes to the output stream modified objects that can belong only to the given page, which makes this method /// "safe" compared to the /// <see cref="UnsafeFlushDeep(int)"/> /// . Flushed object frees the memory, but it's impossible to /// modify such objects or read data from them. This method releases all other page structure objects that are not /// modified. /// <p> /// This method is mainly designed for the append mode. It is similar to the /// <see cref="PdfPage.Flush()"/> /// , but it /// additionally releases all page objects that were not flushed. This method is ideal for small amendments of pages, /// but it makes more sense to use /// <see cref="PdfPage.Flush()"/> /// for newly created or heavily modified pages. <br /> /// This method will throw an exception for documents opened in reading mode (see /// <see cref="PageFlushingHelper"/> /// for more details on modes). It is also not advised to be used in stamping mode: even though it will indeed /// release the objects and free the memory, the released objects will definitely be re-read again on document /// closing, which would affect performance. /// </p> /// <p> /// When using this method in append mode (or in stamping mode), be careful not to try to modify the object instances /// obtained before this method call! See /// <see cref="PageFlushingHelper"/> /// for details on released and flushed objects state. /// </p> /// <p> /// This method shall be used only when it's known that the page and its inner structures processing is finished. /// This includes reading data from pages, page modification and page handling via addons/utilities. /// </p> /// </summary> /// <param name="pageNum">the page number which low level objects structure is to be flushed or released from memory. /// </param> public virtual void AppendModeFlush(int pageNum) { if (pdfDoc.GetWriter() == null) { throw new ArgumentException(PdfException.FlushingHelperFLushingModeIsNotForDocReadingMode); } PdfPage page = pdfDoc.GetPage(pageNum); if (page.IsFlushed()) { return; } page.GetDocument().DispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.END_PAGE, page)); bool pageWasModified = page.GetPdfObject().IsModified(); page.SetModified(); release = true; pageWasModified = FlushPage(pageNum) || pageWasModified; PdfArray annots = page.GetPdfObject().GetAsArray(PdfName.Annots); if (annots != null && !annots.IsFlushed()) { ArrayFlushIfModified(annots); } PdfObject thumb = page.GetPdfObject().Get(PdfName.Thumb, false); FlushIfModified(thumb); PdfObject contents = page.GetPdfObject().Get(PdfName.Contents, false); if (contents is PdfIndirectReference) { if (contents.CheckState(PdfObject.MODIFIED) && !contents.CheckState(PdfObject.FLUSHED)) { PdfObject contentsDirectObj = ((PdfIndirectReference)contents).GetRefersTo(); if (contentsDirectObj.IsArray()) { ArrayFlushIfModified((PdfArray)contentsDirectObj); } else { contentsDirectObj.Flush(); } } } else { // already checked that modified if (contents is PdfArray) { ArrayFlushIfModified((PdfArray)contents); } else { if (contents is PdfStream) { FlushIfModified(contents); } } } // Page tags flushing is supported only in PdfPage#flush and #unsafeFlushDeep: it makes sense to flush tags // completely for heavily modified or new pages. For the slightly modified pages it should be enough to release // the tag structure objects via tag structure releasing utility. if (!pageWasModified) { page.GetPdfObject().GetIndirectReference().ClearState(PdfObject.MODIFIED); pdfDoc.GetCatalog().GetPageTree().ReleasePage(pageNum); page.UnsetForbidRelease(); page.GetPdfObject().Release(); } else { // inherited and modified resources are handled in #flushPage call in the beginning of method page.ReleaseInstanceFields(); page.GetPdfObject().Flush(); } }