public virtual void CloseDocumentWithoutModificationsTest() { String fileName = destinationFolder + "xrefStmInAppendMode.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "xrefStmInWriteMode.pdf"), new PdfWriter (fileName).SetCompressionLevel(CompressionConstants.NO_COMPRESSION), new StampingProperties().UseAppendMode ()); // Clear state for document info indirect reference so that there are no modified objects // in the document due to which, the document will have only one href table. pdfDocument.GetDocumentInfo().GetPdfObject().GetIndirectReference().ClearState(PdfObject.MODIFIED); pdfDocument.Close(); PdfDocument doc = new PdfDocument(new PdfReader(fileName)); int xrefTableCounter = 0; for (int i = 1; i < doc.GetNumberOfPdfObjects(); i++) { PdfObject obj = doc.GetPdfObject(i); if (obj is PdfDictionary) { PdfDictionary objStmDict = (PdfDictionary)doc.GetPdfObject(i); PdfObject type = objStmDict.Get(PdfName.Type); if (type != null && type.Equals(PdfName.XRef)) { xrefTableCounter++; } } } NUnit.Framework.Assert.AreEqual(((PdfNumber)doc.GetTrailer().Get(PdfName.Size)).IntValue(), doc.GetNumberOfPdfObjects ()); doc.Close(); NUnit.Framework.Assert.AreEqual(1, xrefTableCounter); }
public virtual void XrefStmInAppendModeTest() { String fileName = destinationFolder + "xrefStmInAppendMode.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "xrefStmInWriteMode.pdf"), new PdfWriter (fileName).SetCompressionLevel(CompressionConstants.NO_COMPRESSION), new StampingProperties().UseAppendMode ()); pdfDocument.Close(); PdfDocument doc = new PdfDocument(new PdfReader(fileName)); int xrefTableCounter = 0; for (int i = 1; i < doc.GetNumberOfPdfObjects(); i++) { PdfObject obj = doc.GetPdfObject(i); if (obj is PdfDictionary) { PdfDictionary objStmDict = (PdfDictionary)doc.GetPdfObject(i); PdfObject type = objStmDict.Get(PdfName.Type); if (type != null && type.Equals(PdfName.XRef)) { xrefTableCounter++; } } } NUnit.Framework.Assert.AreEqual(((PdfNumber)doc.GetTrailer().Get(PdfName.Size)).IntValue(), doc.GetNumberOfPdfObjects ()); doc.Close(); NUnit.Framework.Assert.AreEqual(2, xrefTableCounter); }
public virtual void XrefStmInWriteModeTest() { String fileName = destinationFolder + "xrefStmInWriteMode.pdf"; PdfWriter writer = new PdfWriter(fileName, new WriterProperties().SetFullCompressionMode(true).SetCompressionLevel (CompressionConstants.NO_COMPRESSION)); PdfDocument pdfDocument = new PdfDocument(writer); PdfPage page = pdfDocument.AddNewPage(); PdfTextAnnotation textannot = new PdfTextAnnotation(new Rectangle(100, 600, 50, 40)); textannot.SetText(new PdfString("Text Annotation 01")).SetContents(new PdfString("Some contents...")); page.AddAnnotation(textannot); pdfDocument.Close(); PdfDocument doc = new PdfDocument(new PdfReader(fileName)); int xrefTableCounter = 0; for (int i = 1; i < doc.GetNumberOfPdfObjects(); i++) { PdfObject obj = doc.GetPdfObject(i); if (obj is PdfDictionary) { PdfDictionary objStmDict = (PdfDictionary)doc.GetPdfObject(i); PdfObject type = objStmDict.Get(PdfName.Type); if (type != null && type.Equals(PdfName.XRef)) { xrefTableCounter++; } } } NUnit.Framework.Assert.AreEqual(((PdfNumber)doc.GetTrailer().Get(PdfName.Size)).IntValue(), doc.GetNumberOfPdfObjects ()); doc.Close(); NUnit.Framework.Assert.AreEqual(1, xrefTableCounter); }
/// <summary>Flushes all modified objects which have not been flushed yet.</summary> /// <remarks>Flushes all modified objects which have not been flushed yet. Used in case incremental updates.</remarks> /// <param name="forbiddenToFlush"> /// a /// <see cref="Java.Util.Set{E}"/> /// of /// <see cref="PdfIndirectReference">references</see> /// that are forbidden to be flushed automatically. /// </param> protected internal virtual void FlushModifiedWaitingObjects(ICollection <PdfIndirectReference> forbiddenToFlush ) { PdfXrefTable xref = document.GetXref(); for (int i = 1; i < xref.Size(); i++) { PdfIndirectReference indirectReference = xref.Get(i); if (null != indirectReference && !indirectReference.IsFree() && !forbiddenToFlush.Contains(indirectReference )) { bool isModified = indirectReference.CheckState(PdfObject.MODIFIED); if (isModified) { PdfObject obj = indirectReference.GetRefersTo(false); if (obj != null) { if (!obj.Equals(objectStream)) { obj.Flush(); } } } } } if (objectStream != null && objectStream.GetSize() > 0) { objectStream.Flush(); objectStream = null; } }
internal static bool EqualContent(PdfObject obj1, PdfObject obj2) { PdfObject direct1 = obj1 != null && obj1.IsIndirectReference() ? ((PdfIndirectReference)obj1).GetRefersTo( true) : obj1; PdfObject direct2 = obj2 != null && obj2.IsIndirectReference() ? ((PdfIndirectReference)obj2).GetRefersTo( true) : obj2; return(direct1 != null && direct1.Equals(direct2)); }
/// <summary>Flushes all modified objects which have not been flushed yet.</summary> /// <remarks>Flushes all modified objects which have not been flushed yet. Used in case incremental updates.</remarks> protected internal virtual void FlushModifiedWaitingObjects() { PdfXrefTable xref = document.GetXref(); for (int i = 1; i < xref.Size(); i++) { PdfIndirectReference indirectReference = xref.Get(i); if (null != indirectReference) { PdfObject obj = indirectReference.GetRefersTo(false); if (obj != null && !obj.Equals(objectStream) && obj.IsModified()) { obj.Flush(); } } } if (objectStream != null && objectStream.GetSize() > 0) { objectStream.Flush(); objectStream = null; } }
private String GetItalicAngleValue(String inPath) { String result = ""; PdfReader pdfR = new PdfReader(inPath); PdfDocument attachmentPDF = new PdfDocument(pdfR); int max = attachmentPDF.GetNumberOfPdfObjects(); for (int i = 0; i < max; i++) { PdfObject obj = attachmentPDF.GetPdfObject(i); if (obj != null) { PdfDictionary pdfDict = (PdfDictionary)obj; PdfObject x = pdfDict.Get(PdfName.Type); if (x != null && x.Equals(PdfName.FontDescriptor)) { PdfObject italicAngle = pdfDict.Get(PdfName.ItalicAngle); result = italicAngle.ToString(); } } } attachmentPDF.Close(); return(result); }