public virtual void CreateEmptyDocument() { FileStream fos = new FileStream(destinationFolder + "emptyDocument.pdf", FileMode.Create); PdfWriter writer = new PdfWriter(fos); PdfDocument pdfDoc = new PdfDocument(writer); pdfDoc.GetDocumentInfo().SetAuthor("Alexander Chingarev").SetCreator("iText 6").SetTitle("Empty iText 6 Document" ); PdfPage page = pdfDoc.AddNewPage(); page.Flush(); pdfDoc.Close(); PdfReader reader = new PdfReader(destinationFolder + "emptyDocument.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt"); NUnit.Framework.Assert.IsNotNull(pdfDocument.GetPage(1)); String date = pdfDocument.GetDocumentInfo().GetPdfObject().GetAsString(PdfName.CreationDate).GetValue(); DateTime cl = PdfDate.Decode(date); double diff = DateTimeUtil.GetUtcMillisFromEpoch(null) - DateTimeUtil.GetUtcMillisFromEpoch(cl); String message = "Unexpected creation date. Different from now is " + (float)diff / 1000 + "s"; NUnit.Framework.Assert.IsTrue(diff < 5000, message); reader.Close(); }
/// <exception cref="iText.Kernel.XMP.XMPException"/> internal static void AppendDocumentInfoToMetadata(PdfDocumentInfo info, XMPMeta xmpMeta) { PdfDictionary docInfo = info.GetPdfObject(); if (docInfo != null) { PdfName key; PdfObject obj; String value; foreach (PdfName pdfName in docInfo.KeySet()) { key = pdfName; obj = docInfo.Get(key); if (obj == null) { continue; } if (obj.IsString()) { value = ((PdfString)obj).ToUnicodeString(); } else { if (obj.IsName()) { value = ((PdfName)obj).GetValue(); } else { continue; } } if (PdfName.Title.Equals(key)) { xmpMeta.SetLocalizedText(XMPConst.NS_DC, PdfConst.Title, XMPConst.X_DEFAULT, XMPConst.X_DEFAULT, value); } else { if (PdfName.Author.Equals(key)) { foreach (String v in iText.IO.Util.StringUtil.Split(value, ",|;")) { if (v.Trim().Length > 0) { AppendArrayItemIfDoesNotExist(xmpMeta, XMPConst.NS_DC, PdfConst.Creator, v.Trim(), PropertyOptions.ARRAY_ORDERED ); } } } else { if (PdfName.Subject.Equals(key)) { xmpMeta.SetLocalizedText(XMPConst.NS_DC, PdfConst.Description, XMPConst.X_DEFAULT, XMPConst.X_DEFAULT, value ); } else { if (PdfName.Keywords.Equals(key)) { foreach (String v in iText.IO.Util.StringUtil.Split(value, ",|;")) { if (v.Trim().Length > 0) { AppendArrayItemIfDoesNotExist(xmpMeta, XMPConst.NS_DC, PdfConst.Subject, v.Trim(), PropertyOptions.ARRAY); } } xmpMeta.SetProperty(XMPConst.NS_PDF, PdfConst.Keywords, value); } else { if (PdfName.Creator.Equals(key)) { xmpMeta.SetProperty(XMPConst.NS_XMP, PdfConst.CreatorTool, value); } else { if (PdfName.Producer.Equals(key)) { xmpMeta.SetProperty(XMPConst.NS_PDF, PdfConst.Producer, value); } else { if (PdfName.CreationDate.Equals(key)) { xmpMeta.SetProperty(XMPConst.NS_XMP, PdfConst.CreateDate, PdfDate.GetW3CDate(value)); } else { if (PdfName.ModDate.Equals(key)) { xmpMeta.SetProperty(XMPConst.NS_XMP, PdfConst.ModifyDate, PdfDate.GetW3CDate(value)); } else { if (PdfName.Trapped.Equals(key)) { xmpMeta.SetProperty(XMPConst.NS_PDF, PdfConst.Trapped, value); } } } } } } } } } } } }