public void OnNewDocument() { this.process = this.mainForm.Process; PdfObject[] objects = this.process.Document.Internals.GetAllObjects(); this.lvObjects.Items.Clear(); for (int idx = 0; idx < objects.Length; idx++) { PdfObject obj = objects[idx]; ListViewItem item = new ListViewItem(new string[2] { PdfInternals.GetObjectID(obj).ToString(), ExplorerProcess.GetTypeName(obj) }); item.Tag = obj; this.lvObjects.Items.Add(item); } PdfPages pages = this.process.Document.Pages; this.lvPages.Items.Clear(); for (int idx = 0; idx < pages.Count; idx++) { PdfPage page = pages[idx]; ListViewItem item = new ListViewItem(new string[2] { (idx + 1).ToString(), ExplorerHelper.PageSize(page, this.mainForm.Process.IsMetric) }); //String.Format("{0:0} x {1:0} mm", XUnit.FromPoint(page.Width).Millimeter,XUnit.FromPoint(page.Height).Millimeter)}); item.Tag = page; this.lvPages.Items.Add(item); } this.process.Navigator.SetNext(this.process.Document.Info); ActivatePage("Info"); }
internal override void SetObject(PdfItem obj) { PdfDictionary dict = (PdfDictionary)obj; if (PdfInternals.GetObjectID(dict).IsEmpty) { this.tbxObjectID.Text = "«direct»"; } else { this.tbxObjectID.Text = PdfInternals.GetObjectID(dict).ToString(); } this.txtType.Text = dict.GetType().Name; this.lvKeys.Items.Clear(); this.tbxStream.Clear(); PdfName[] keys = dict.Elements.KeyNames; foreach (PdfName key in keys) { bool indirect = false; PdfItem tag = null; PdfItem value = dict.Elements[key]; if (value == null) { throw new NotImplementedException("Value is null."); } else if (value is PdfReference) { indirect = true; value = ((PdfReference)value).Value; tag = value; } else { if (value is PdfObject) { tag = value; } } string address = ""; if (indirect) { address = /*((PdfObject)value).ObjectID.ToString() +*/ "---> "; } ListViewItem item = new ListViewItem( new string[4] { key.Value, address + value.ToString(), indirect ? PdfInternals.GetObjectID((PdfObject)value).ToString() : "", value.GetType().Name }); item.Tag = tag; this.lvKeys.Items.Add(item); } FillStreamBox(); }
internal override void SetObject(PdfItem obj) { PdfArray array = (PdfArray)obj; if (PdfInternals.GetObjectID(array).IsEmpty) { this.tbxObjectID.Text = "«direct»"; } else { this.tbxObjectID.Text = PdfInternals.GetObjectID(array).ToString(); } this.txtType.Text = array.GetType().Name; this.lvItems.Items.Clear(); PdfItem[] items = array.Elements.Items; int count = items.Length; for (int idx = 0; idx < count; idx++) { bool indirect = false; PdfItem tag = null; PdfItem value = items[idx]; if (value == null) { throw new NotImplementedException("Value is null."); } else if (value is PdfReference) { indirect = true; value = ((PdfReference)value).Value; tag = value; } else { if (value is PdfObject) { tag = value; } } string address = ""; if (indirect) { address = /*((PdfObject)value).ObjectID.ToString() +*/ "---> "; } ListViewItem item = new ListViewItem( new string[4] { idx.ToString(), address + value.ToString(), indirect ? PdfInternals.GetObjectID((PdfObject)value).ToString() : "", value.GetType().Name }); item.Tag = tag; this.lvItems.Items.Add(item); } }
internal override void SetObject(PdfItem value) { PdfObject obj = (PdfObject)value; this.tbxObjectID.Text = PdfInternals.GetObjectID(obj).ToString(); this.txtType.Text = obj.GetType().Name; this.lvKeys.Items.Clear(); ListViewItem item = new ListViewItem( new string[3] { ExplorerProcess.GetTypeName(obj), value.ToString(), value.GetType().Name }); this.lvKeys.Items.Add(item); }
public PdfDocument AddAutoPrint(PdfDocument outputDocument, bool showPrintDialog = true, int numCopies = 1) { outputDocument.Info.Author = "author name"; var JSScript = string.Empty; JSScript += "var pp = this.getPrintParams(); "; if (numCopies > 0) { JSScript += "pp.NumCopies = " + numCopies.ToString() + "; "; } if (!showPrintDialog) { JSScript += "pp.interactive = pp.constants.interactionLevel.automatic; "; } JSScript += "this.print({printParams: pp}); "; var dictJS = new PdfDictionary(); dictJS.Elements["/S"] = new PdfName("/JavaScript"); dictJS.Elements["/JS"] = new PdfStringObject(outputDocument, JSScript); outputDocument.Internals.AddObject(dictJS); var dict = new PdfDictionary(); var a = new PdfArray(); dict.Elements["/Names"] = a; a.Elements.Add(new PdfString("EmbeddedJS")); a.Elements.Add(PdfInternals.GetReference(dictJS)); outputDocument.Internals.AddObject(dict); var group = new PdfDictionary(); group.Elements["/JavaScript"] = PdfInternals.GetReference(dict); outputDocument.Internals.Catalog.Elements["/Names"] = group; return(outputDocument); }
static void Main() { const string filename = "Portable Document Format.pdf"; File.Copy(Path.Combine("../../../../../PDFs/", filename), Path.Combine(Directory.GetCurrentDirectory(), filename), true); PdfDocument document = PdfReader.Open(filename); PdfDictionary dict = new PdfDictionary(document); dict.Elements["/S"] = new PdfName("/GoTo"); PdfArray array = new PdfArray(document); dict.Elements["/D"] = array; PdfReference iref = PdfInternals.GetReference(document.Pages[2]); array.Elements.Add(iref); array.Elements.Add(new PdfName("/FitV")); array.Elements.Add(new PdfInteger(-32768)); document.Internals.AddObject(dict); document.Internals.Catalog.Elements["/OpenAction"] = PdfInternals.GetReference(dict); document.Save(filename); Process.Start(filename); }
public static void Run() { // Get a fresh copy of the sample PDF file. const string filename = "Portable Document Format.pdf"; var file = Path.Combine(Directory.GetCurrentDirectory(), filename); File.Copy(Path.Combine("assets", filename), file, true); // Remove ReadOnly attribute from the copy. File.SetAttributes(file, File.GetAttributes(file) & ~FileAttributes.ReadOnly); // Read the document into memory for modification. var document = PdfReader.Open(filename); // The current version of PDFsharp doesn't support the concept of // 'actions'. Actions will come in a future version, but if you need them // now, you can have them 'handmade'. // // This sample works on PDF objects directly, therefore some knowledge of // the structure of PDF is required. // If you are not familiar with the portable document format, first read // at least chapter 3 in Adobe's PDF Reference // (http://partners.adobe.com/public/developer/pdf/index_reference.html). // If you can read German, I recommend chapter 12 of 'Die PostScript & // PDF-Bibel', a much more interesting reading than the bone-dry Adobe // books (http://www.pdflib.com/de/produkte/mehr/bibel/index.html). // // The sample task is to add an 'open action' to the document so that it // starts with the content of page 3 magnified just enough to fit the // height of the page within the window. // First we have to create a new dictionary that defines the action. var dict = new PdfDictionary(document); // According to the PDF Reference the dictionary requires two elements. // A key /S that specifies the 'GoTo' action, // and a key /D that describes the destination. // Adding a name as value of key /S is easy. dict.Elements["/S"] = new PdfName("/GoTo"); // The destination is described by an array. var array = new PdfArray(document); // Set the array as the value of key /D. // This makes the array a direct object of the dictionary. dict.Elements["/D"] = array; // Now add the elements to the array. According to the PDF Reference // there must be three for a page as the target of a 'GoTo' action. // The first element is an indirect reference to the destination page. // To add an indirect reference to the page three, we first need the // PdfReference object of that page. // (The index in the Pages collection is zero based, therefore Pages[2]) var iref = PdfInternals.GetReference(document.Pages[2]); // Add the reference to the third page as the first array element. // Adding the iref (instead of the PdfPage object itself) makes it an // indirect reference. array.Elements.Add(iref); // The second element is the name /FitV to indicate 'fit vertically'. array.Elements.Add(new PdfName("/FitV")); // /FitV requires the horizontal coordinate that will be positioned at the // left edge of the window. We set -32768 because Acrobat uses this value // to show the full page (it means 'left aligned' anyway if the window is // so small that a horizontal scroll bar is required). array.Elements.Add(new PdfInteger(-32768)); // Now that the action dictionary is complete, we can add it to the // document's object table. // Adding an object to the object table makes it an indirect object. document.Internals.AddObject(dict); // Finally we must add the action dictionary to the /OpenAction key of // the document's catalog as an indirect value. document.Internals.Catalog.Elements["/OpenAction"] = PdfInternals.GetReference(dict); // Using PDFsharp we never deal with object numbers. We simply put the // objects together and the PDFsharp framework does the rest. // Save the document... document.Save(filename); }