public Superpage(XPdfForm form, uint startPage, uint numPages, ISuperpageLayout layout) { subpages = new List<Subpage>(); for (uint i = 0; i < numPages; i++) { subpages.Add(new Subpage(form, startPage + i, layout.GetSubpageLayout(i))); } this.layout = layout; }
public static IList<Superpage> Layout(XPdfForm form, ISuperpageLayout layout) { var superpages = new List<Superpage>(); for (uint start = 0; start < form.PageCount; start += layout.Count) { var numPages = (uint)Math.Min(layout.Count, form.PageCount - start); superpages.Add(new Superpage(form, start, numPages, layout)); } return superpages; }
/// <summary> /// Constructor /// </summary> /// <param name="path">path to input file</param> /// <param name="outputPath">path to output file</param> public PdfA4A5Engine(string path, string outputPath) { try { fileName = Path.GetFileName(path); } catch (IOException) { ErrorMessage = ErrorMessages.IOProblem.ToString(); return; } form = XPdfForm.FromFile(fileName); this.outputPath = outputPath; pageCountWithModulo = form.PageCount + (form.PageCount % 4); }
public static void Test() { string fn = @"D:\username\Desktop\BuildingXY-Floor14.pdf"; using (PdfDocument destDocument = new PdfDocument()) { PdfPage destPage = destDocument.AddPage(); using (XGraphics gfx = XGraphics.FromPdfPage(destPage)) { using (PdfSharp.Drawing.XPdfForm sourceForm = PdfSharp.Drawing.XPdfForm.FromFile(fn)) { DrawFormWithOptions(gfx, sourceForm); } } } }
/// <summary> /// Creates a new instance of the XGraphics class from a PdfSharp.Drawing.XPdfForm object. /// </summary> public static XGraphics FromPdfForm(XPdfForm form) { if (form.gfx != null) return form.gfx; return new XGraphics(form); }
internal PdfFormXObject(PdfDocument thisDocument, PdfImportedObjectTable importedObjectTable, XPdfForm form) : base(thisDocument) { Debug.Assert(Object.ReferenceEquals(thisDocument, importedObjectTable.Owner)); Elements.SetName(Keys.Type, "/XObject"); Elements.SetName(Keys.Subtype, "/Form"); if (form.IsTemplate) { Debug.Assert(importedObjectTable == null); // TODO more initialization here??? return; } Debug.Assert(importedObjectTable != null); XPdfForm pdfForm = (XPdfForm)form; // Get import page PdfPages importPages = importedObjectTable.ExternalDocument.Pages; if (pdfForm.PageNumber < 1 || pdfForm.PageNumber > importPages.Count) PSSR.ImportPageNumberOutOfRange(pdfForm.PageNumber, importPages.Count, form.path); PdfPage importPage = importPages[pdfForm.PageNumber - 1]; // Import resources PdfItem res = importPage.Elements["/Resources"]; if (res != null) // unlikely but possible { #if true // Get root object PdfObject root; if (res is PdfReference) root = ((PdfReference)res).Value; else root = (PdfDictionary)res; root = ImportClosure(importedObjectTable, thisDocument, root); // If the root was a direct object, make it indirect. if (root.Reference == null) thisDocument.irefTable.Add(root); Debug.Assert(root.Reference != null); Elements["/Resources"] = root.Reference; #else // Get transitive closure PdfObject[] resources = importPage.Owner.Internals.GetClosure(resourcesRoot); int count = resources.Length; #if DEBUG_ for (int idx = 0; idx < count; idx++) { Debug.Assert(resources[idx].XRef != null); Debug.Assert(resources[idx].XRef.Document != null); Debug.Assert(resources[idx].Document != null); if (resources[idx].ObjectID.ObjectNumber == 12) GetType(); } #endif // 1st step. Already imported objects are reused and new ones are cloned. for (int idx = 0; idx < count; idx++) { PdfObject obj = resources[idx]; if (importedObjectTable.Contains(obj.ObjectID)) { // external object was already imported PdfReference iref = importedObjectTable[obj.ObjectID]; Debug.Assert(iref != null); Debug.Assert(iref.Value != null); Debug.Assert(iref.Document == this.Owner); // replace external object by the already clone counterpart resources[idx] = iref.Value; } else { // External object was not imported ealier and must be cloned PdfObject clone = obj.Clone(); Debug.Assert(clone.Reference == null); clone.Document = this.Owner; if (obj.Reference != null) { // add it to this (the importer) document this.Owner.irefTable.Add(clone); Debug.Assert(clone.Reference != null); // save old object identifier importedObjectTable.Add(obj.ObjectID, clone.Reference); //Debug.WriteLine("Cloned: " + obj.ObjectID.ToString()); } else { // The root object (the /Resources value) is not an indirect object Debug.Assert(idx == 0); // add it to this (the importer) document this.Owner.irefTable.Add(clone); Debug.Assert(clone.Reference != null); } // replace external object by its clone resources[idx] = clone; } } #if DEBUG_ for (int idx = 0; idx < count; idx++) { Debug.Assert(resources[idx].XRef != null); Debug.Assert(resources[idx].XRef.Document != null); Debug.Assert(resources[idx].Document != null); if (resources[idx].ObjectID.ObjectNumber == 12) GetType(); } #endif // 2nd step. Fix up indirect references that still refers to the import document. for (int idx = 0; idx < count; idx++) { PdfObject obj = resources[idx]; Debug.Assert(obj.Owner != null); FixUpObject(importedObjectTable, importedObjectTable.Owner, obj); } // Set resources key to the root of the clones Elements["/Resources"] = resources[0].Reference; #endif } // Take /Rotate into account PdfRectangle rect = importPage.Elements.GetRectangle(PdfPage.Keys.MediaBox); int rotate = importPage.Elements.GetInteger(PdfPage.Keys.Rotate); //rotate = 0; if (rotate == 0) { // Set bounding box to media box this.Elements["/BBox"] = rect; } else { // TODO: Have to adjust bounding box? (I think not, but I'm not sure -> wait for problem) this.Elements["/BBox"] = rect; // Rotate the image such that it is upright XMatrix matrix = XMatrix.Identity; double width = rect.Width; double height = rect.Height; matrix.RotateAtPrepend(-rotate, new XPoint(width / 2, height / 2)); // Translate the image such that its center lies on the center of the rotated bounding box double offset = (height - width) / 2; if (height > width) matrix.TranslatePrepend(offset, offset); else matrix.TranslatePrepend(-offset, -offset); //string item = "[" + PdfEncoders.ToString(matrix) + "]"; //Elements[Keys.Matrix] = new PdfLiteral(item); Elements.SetMatrix(Keys.Matrix, matrix); } // Preserve filter because the content keeps unmodified PdfContent content = importPage.Contents.CreateSingleContent(); #if !DEBUG content.Compressed = true; #endif PdfItem filter = content.Elements["/Filter"]; if (filter != null) this.Elements["/Filter"] = filter.Clone(); // (no cloning needed because the bytes keep untouched) this.Stream = content.Stream; // new PdfStream(bytes, this); Elements.SetInteger("/Length", content.Stream.Value.Length); }
public Subpage(XPdfForm form, uint sourcePageIndex, SubpageLayout layout) { this.form = form; this.sourcePageIndex = (int)sourcePageIndex; this.layout = layout; }