예제 #1
0
        public string ExtractPdfPagesFromEnd(string sourcePath, string destinationPath, int numberOfPages)
        {
            var sourceDoc = new Doc();

            sourceDoc.Read(sourcePath);
            var sourceCount = sourceDoc.PageCount;

            if (numberOfPages > sourceCount)
            {
                throw new IndexOutOfRangeException(string.Format("Unable to extract pages from {0}.\nNumber of pages in source document less than required number of pages : {1}.", sourcePath, numberOfPages));
            }

            var destinationDoc = new Doc();

            destinationDoc.MediaBox.String = sourceDoc.MediaBox.String;
            destinationDoc.Rect.String     = destinationDoc.MediaBox.String;

            for (var i = sourceCount - numberOfPages + 1; i <= sourceCount; i++)
            {
                destinationDoc.Page = destinationDoc.AddPage();
                destinationDoc.AddImageDoc(sourceDoc, i, null);
                destinationDoc.FrameRect();
            }

            destinationDoc.Save(destinationPath);
            return(destinationPath);
        }
예제 #2
0
        public void ExtractPdfPages(string sourcePath, string destinationPath, int startPageNumber, int endPageNumber)
        {
            var sourceDoc = new Doc();

            sourceDoc.Read(sourcePath);
            var sourceCount = sourceDoc.PageCount;

            if (endPageNumber > sourceCount)
            {
                throw new IndexOutOfRangeException(string.Format("Unable to extract pages from {0}.\nNumber of pages in source document less than end page number.", sourcePath));
            }

            var destinationDoc = new Doc();

            destinationDoc.MediaBox.String = sourceDoc.MediaBox.String;
            destinationDoc.Rect.String     = destinationDoc.MediaBox.String;
            destinationDoc.Rect.Magnify(0.5, 0.5);
            destinationDoc.Rect.Inset(10, 10);

            for (int i = startPageNumber; i <= endPageNumber; i++)
            {
                destinationDoc.Page = destinationDoc.AddPage();
                destinationDoc.AddImageDoc(sourceDoc, i, null);
                destinationDoc.FrameRect();
            }
            destinationDoc.Save(destinationPath);
        }
예제 #3
0
        public int MergeDocuments(List <string> documentsToMergeList, List <string> documentsToCreateAndMergeList, string outputLocation, PdfPageSizeEnum pageSizeEnum)
        {
            try
            {
                using (Doc doc1 = new Doc())
                {
                    //set the page size of the entire document
                    doc1.MediaBox.String = pageSizeEnum.ToString();

                    //Merge existing documents
                    foreach (var documentToMerge in documentsToMergeList)
                    {
                        Doc doc2 = new Doc();

                        doc2.Read(documentToMerge);

                        //add each page of the plan to the doc2. It will add it as A4 size
                        for (int i = 1; i <= doc2.PageCount; i++)
                        {
                            doc1.Page = doc1.AddPage();
                            doc1.AddImageDoc(doc2, i, null);
                            doc1.FrameRect();
                        }

                        doc2.Dispose();
                    }

                    //Create New documents and Merge
                    foreach (var documentToCreateAndMerge in documentsToCreateAndMergeList)
                    {
                        using (Doc doc2 = new Doc())
                        {
                            doc2.FontSize = 40;
                            doc2.AddText(documentToCreateAndMerge);
                            doc1.Append(doc2);
                        }
                    }

                    doc1.Form.MakeFieldsUnique(Guid.NewGuid().ToString());
                    doc1.Encryption.CanEdit      = false;
                    doc1.Encryption.CanChange    = false;
                    doc1.Encryption.CanFillForms = false;
                    doc1.Save(outputLocation);
                    return(doc1.PageCount);
                }
            }
            catch (Exception e)
            {
                var documents = string.Join(", ", (from l in documentsToMergeList select l));
                //this.Log().Error(string.Format("Error converting the following documents {0} to ouput location {1}", documents, outputLocation), e);
                //Throw the stack trace with it.
                throw;
            }
        }
예제 #4
0
        public byte[] ExtractPages(byte[] documentBytes, int startingPageNumber, int pageCount)
        {
            byte[] extractedDocBytes;
            using (var srcDoc = new Doc())
            {
                srcDoc.Read(documentBytes);
                int srcPagesID = srcDoc.GetInfoInt(srcDoc.Root, "Pages");  //get the root page id
                int srcDocRot  = srcDoc.GetInfoInt(srcPagesID, "/Rotate"); //get the rotation of root setting

                using (var extractedPages = new Doc())
                {
                    for (var i = startingPageNumber; i < startingPageNumber + pageCount; i++)
                    {
                        srcDoc.PageNumber = i; //turn original file to current page

                        extractedPages.MediaBox.String = srcDoc.MediaBox.String;

                        extractedPages.AddPage();
                        extractedPages.PageNumber  = i - startingPageNumber + 1;
                        extractedPages.Rect.String = extractedPages.MediaBox.String;//doc2.Rect.String;

                        extractedPages.AddImageDoc(srcDoc, i, null);

                        //get the rotation of current page by page Id
                        int srcPageRot = srcDoc.GetInfoInt(srcDoc.Page, "/Rotate");

                        //rotate the page if any rotation exist
                        if (srcDocRot != 0)
                        {
                            extractedPages.SetInfo(extractedPages.Page, "/Rotate", srcDocRot);
                        }

                        if (srcPageRot != 0)
                        {
                            extractedPages.SetInfo(extractedPages.Page, "/Rotate", srcPageRot);
                        }
                    }
                    extractedDocBytes = extractedPages.GetData();
                    extractedPages.Clear();
                }

                srcDoc.Clear();
            }

            return(extractedDocBytes);
        }
예제 #5
0
        public IEnumerable <byte[]> SplitPdfPages(byte[] originalPdfBytes)
        {
            var pdfFiles = new List <byte[]>();

            using (var doc = new Doc())
            {
                doc.Read(originalPdfBytes);

                int srcPagesID = doc.GetInfoInt(doc.Root, "Pages");
                int srcDocRot  = doc.GetInfoInt(srcPagesID, "/Rotate");

                for (var i = 1; i <= doc.PageCount; i++)
                {
                    var singlePagePdf = new Doc();
                    doc.PageNumber = i;

                    singlePagePdf.Rect.String = singlePagePdf.MediaBox.String = doc.MediaBox.String;
                    singlePagePdf.AddPage();
                    singlePagePdf.AddImageDoc(doc, i, null);
                    singlePagePdf.FrameRect();

                    int srcPageRot = doc.GetInfoInt(doc.Page, "/Rotate");

                    if (srcDocRot != 0)
                    {
                        singlePagePdf.SetInfo(singlePagePdf.Page, "/Rotate", srcDocRot);
                    }

                    if (srcPageRot != 0)
                    {
                        singlePagePdf.SetInfo(singlePagePdf.Page, "/Rotate", srcPageRot);
                    }

                    var singlePagePdfBytes = singlePagePdf.GetData();
                    pdfFiles.Add(singlePagePdfBytes);

                    singlePagePdf.Clear();
                }
                doc.Clear();
            }

            return(pdfFiles);
        }
예제 #6
0
        public List <string> Split(string filepath, int splitPageLength)
        {
            var listOfFiles = new List <string>();

            using (var originalDoc = new Doc())
            {
                originalDoc.Read(filepath);

                // Split into seperate certificates
                for (var i = 1; i <= originalDoc.PageCount; i += splitPageLength)
                {
                    int numberOfPagesToExtract;
                    if (i + splitPageLength - 1 > originalDoc.PageCount)
                    {
                        numberOfPagesToExtract = originalDoc.PageCount - i + 1;
                    }
                    else
                    {
                        numberOfPagesToExtract = splitPageLength;
                    }

                    using (var extractedDoc = new Doc())
                    {
                        for (var j = 0; j < numberOfPagesToExtract; j++)
                        {
                            originalDoc.PageNumber       = i + j;
                            extractedDoc.MediaBox.String = originalDoc.MediaBox.String;
                            extractedDoc.AddPage();
                            extractedDoc.PageNumber  = j + 1;
                            extractedDoc.Rect.String = extractedDoc.MediaBox.String;
                            extractedDoc.AddImageDoc(originalDoc, i + j, null);
                        }

                        var splitFilePath = Path.GetTempPath() + Guid.NewGuid() + ".pdf";
                        extractedDoc.Save(splitFilePath);
                        listOfFiles.Add(splitFilePath);
                    }
                }
            }

            return(listOfFiles);
        }
        protected Doc AssessmentSummaryToPdfDoc(PdfRenderSettings settings = null)
        {
            var dp = DistrictParms.LoadDistrictParms();
            if (settings == null) settings = new PdfRenderSettings();

            StringWriter sw = new StringWriter();
            HtmlTextWriter w = new HtmlTextWriter(sw);
            summaryContent.Attributes["style"] = "font-family: Sans-Serif, Arial;font-weight: bold;position: relative;font-size: .8em;";
            summaryContent.RenderControl(w);
            string result_html = sw.GetStringBuilder().ToString();

            int topOffset = settings.HeaderHeight > 0 ? settings.HeaderHeight : 0;
            int bottomOffset = settings.FooterHeight > 0 ? settings.FooterHeight : 0;

            Doc doc = new Doc();
            doc.HtmlOptions.HideBackground = true;
            doc.HtmlOptions.PageCacheEnabled = false;
            doc.HtmlOptions.UseScript = true;
            doc.HtmlOptions.Timeout = 36000;
            doc.HtmlOptions.BreakZoneSize = 100;    // I experiemented with this being 99% instead of 100%, but you end up with passages getting cut off in unflattering ways. This may lead to more blank space... but I think it's the lessor of evils
            doc.HtmlOptions.ImageQuality = 70;

            doc.MediaBox.String = "0 0 " + settings.PageWidth + " " + settings.PageHeight;
            doc.Rect.String = settings.LeftMargin + " " + (0 + bottomOffset).ToString() + " " + (settings.PageWidth - settings.RightMargin).ToString() + " " + (settings.PageHeight - topOffset).ToString();
            doc.HtmlOptions.AddTags = true;
            doc.SetInfo(0, "ApplyOnLoadScriptOnceOnly", "1");

            List<int> forms = new List<int>();

            int theID = doc.AddImageHtml(result_html);

            Thinkgate.Base.Classes.Assessment.ChainPDFItems(doc, theID, forms, settings, dp.PdfPrintPageLimit); 

            if (settings.HeaderHeight > 0 && !String.IsNullOrEmpty(settings.HeaderText))
            {
                /*HttpServerUtility Server = HttpContext.Current.Server;
                headerText = Server.HtmlDecode(headerText);*/
                Doc headerDoc = new Doc();

                headerDoc.MediaBox.String = settings.LeftMargin + " " + (settings.PageHeight - settings.HeaderHeight).ToString() + " " + (settings.PageWidth - settings.RightMargin).ToString() + " " + settings.PageHeight;	//LEFT, BOTTOM,WIDTH, HEIGHT
                headerDoc.Rect.String = settings.LeftMargin + " " + (settings.PageHeight - settings.HeaderHeight).ToString() + " " + (settings.PageWidth - settings.RightMargin).ToString() + " " + settings.PageHeight;	//LEFT, BOTTOM,WIDTH, HEIGHT
                headerDoc.VPos = 0.5;
                int headerID = headerDoc.AddImageHtml(settings.HeaderText);

                if (!String.IsNullOrEmpty(settings.HeaderText))
                {
                    int form_ref = 0;
                    for (int i = 1; i <= doc.PageCount; i++)
                    {
                        if (form_ref < forms.Count && forms[form_ref] == i)
                        {
                            form_ref++;
                        }
                        else
                        {
                            if (i > 1 || settings.ShowHeaderOnFirstPage)
                            {
                                doc.PageNumber = i;
                                doc.Rect.String = settings.LeftMargin + " " + (settings.PageHeight - settings.HeaderHeight).ToString() + " " + (settings.PageWidth - settings.RightMargin).ToString() + " " + settings.PageHeight;	//LEFT, BOTTOM,WIDTH, HEIGHT
                                doc.VPos = 0.5;

                                theID = doc.AddImageDoc(headerDoc, 1, null);
                                theID = doc.AddImageToChain(theID);
                            }
                        }
                    }
                }
            }

            for (int i = 1; i <= doc.PageCount; i++)
            {
                doc.PageNumber = i;
                doc.Flatten();
            }
            return doc;
        }
        protected Doc ItemAnalysisToPdfDoc(PdfRenderSettings settings = null)
        {
            if (settings == null) settings = new PdfRenderSettings();

            StringWriter sw = new StringWriter();
            HtmlTextWriter w = new HtmlTextWriter(sw);
            reportHeaderDiv.Controls.Add(LoadPDFHeaderInfo());
            barGraphLevelsContainerDiv.Controls.Add(LoadBarGraphLevels());
            barGraphPDFContainerDiv.Controls.Add(LoadBarGraphs());
            contentDiv.RenderControl(w);
            string result_html = sw.GetStringBuilder().ToString();

            int topOffset = settings.HeaderHeight > 0 ? settings.HeaderHeight : 0;
            int bottomOffset = settings.FooterHeight > 0 ? settings.FooterHeight : 0;

            Doc doc = new Doc();
            doc.HtmlOptions.HideBackground = true;
            doc.HtmlOptions.PageCacheEnabled = false;
            doc.HtmlOptions.UseScript = true;
            doc.HtmlOptions.Timeout = 36000;
            doc.HtmlOptions.BreakZoneSize = 100;    // I experiemented with this being 99% instead of 100%, but you end up with passages getting cut off in unflattering ways. This may lead to more blank space... but I think it's the lessor of evils
            doc.HtmlOptions.ImageQuality = 70;

            doc.MediaBox.String = "0 0 " + settings.PageWidth + " " + settings.PageHeight;
            doc.Rect.String = settings.LeftMargin + " " + (0 + bottomOffset).ToString() + " " + (settings.PageWidth - settings.RightMargin).ToString() + " " + (settings.PageHeight - topOffset).ToString();
            doc.HtmlOptions.AddTags = true;
            doc.SetInfo(0, "ApplyOnLoadScriptOnceOnly", "1");

            List<int> forms = new List<int>();

            int theID = doc.AddImageHtml(result_html);

            while (true)
            {
                if (!doc.Chainable(theID))
                    break;
                doc.Page = doc.AddPage();
                theID = doc.AddImageToChain(theID);
                string[] tagIds = doc.HtmlOptions.GetTagIDs(theID);
                if (tagIds.Length > 0 && tagIds[0] == "test_header")
                    forms.Add(doc.PageNumber);					// By using GetTagIDs to find if a test header ended up on this page, we can determine whether the page needs a header

                if (settings.PossibleForcedBreaks)
                {		// only want to take the performance hit if there's a change we're forcing breaks
                    if (String.IsNullOrEmpty(doc.GetText("Text")))
                    {		// WSH Found situation where after I added page break always for multi-form, one test that was already breaking properly, added an extra page that was blank between forms. Almost like some amount of HTML had been put there, even though it wasn't any real text. By checking to make sure there is some actual text on page, we can avoid that problem
                        doc.Delete(doc.Page);
                    }
                }
            }

            if (settings.HeaderHeight > 0 && !String.IsNullOrEmpty(settings.HeaderText))
            {
                /*HttpServerUtility Server = HttpContext.Current.Server;
                headerText = Server.HtmlDecode(headerText);*/
                Doc headerDoc = new Doc();

                headerDoc.MediaBox.String = settings.LeftMargin + " " + (settings.PageHeight - settings.HeaderHeight).ToString() + " " + (settings.PageWidth - settings.RightMargin).ToString() + " " + settings.PageHeight;	//LEFT, BOTTOM,WIDTH, HEIGHT
                headerDoc.Rect.String = settings.LeftMargin + " " + (settings.PageHeight - settings.HeaderHeight).ToString() + " " + (settings.PageWidth - settings.RightMargin).ToString() + " " + settings.PageHeight;	//LEFT, BOTTOM,WIDTH, HEIGHT
                headerDoc.VPos = 0.5;
                int headerID = headerDoc.AddImageHtml(settings.HeaderText);

                if (!String.IsNullOrEmpty(settings.HeaderText))
                {
                    int form_ref = 0;
                    for (int i = 1; i <= doc.PageCount; i++)
                    {
                        if (form_ref < forms.Count && forms[form_ref] == i)
                        {
                            form_ref++;
                        }
                        else
                        {
                            if (i > 1 || settings.ShowHeaderOnFirstPage)
                            {
                                doc.PageNumber = i;
                                doc.Rect.String = settings.LeftMargin + " " + (settings.PageHeight - settings.HeaderHeight).ToString() + " " + (settings.PageWidth - settings.RightMargin).ToString() + " " + settings.PageHeight;	//LEFT, BOTTOM,WIDTH, HEIGHT
                                doc.VPos = 0.5;

                                theID = doc.AddImageDoc(headerDoc, 1, null);
                                theID = doc.AddImageToChain(theID);
                            }
                        }
                    }
                }
            }

            for (int i = 1; i <= doc.PageCount; i++)
            {
                doc.PageNumber = i;
                doc.Flatten();
            }
            return doc;
        }