コード例 #1
0
        /// <summary>
        /// Get page count
        /// </summary>
        /// <param name="pdf">Pdf</param>
        /// <returns>Page count</returns>
        public int GetPageCount(byte[] pdf)
        {
            using (var stream = new MemoryStream(pdf))
            {
                using (var pdfInstance = GdPictureHelper.GetPDFInstance())
                {
                    pdfInstance.LoadFromStream(stream);
                    var count = pdfInstance.GetPageCount();

                    pdfInstance?.CloseDocument();

                    return(count);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Split pdf by page range
        /// </summary>
        /// <param name="pdf">Pdf to split</param>
        /// <param name="ranges">List of page ranges</param>
        /// <returns>List of splitted pdfs</returns>
        public IList <PdfSplitResult> Split(byte[] pdf, IList <PageNumberRange> ranges)
        {
            var result = new List <PdfSplitResult>();

            using (var stream = new MemoryStream(pdf))
            {
                using (var pdfInstance = GdPictureHelper.GetPDFInstance())
                {
                    pdfInstance.LoadFromStream(stream);

                    if (ranges == null || ranges.Count == 0)
                    {
                        return new List <PdfSplitResult> {
                        }
                    }
                    ;

                    foreach (var range in ranges.Where(x => x.PageCount > 0 && x.StartPageNumber + (x.PageCount - 1) <= pdfInstance.GetPageCount()))
                    {
                        using (var newPdf = GdPictureHelper.GetPDFInstance())
                        {
                            newPdf.NewPDF();
                            for (int i = 0; i < range.PageCount; i++)
                            {
                                newPdf.ClonePage(pdfInstance, range.StartPageNumber + i);
                            }

                            using (var targetStream = new MemoryStream())
                            {
                                newPdf.SaveToStream(targetStream);
                                targetStream.Position = 0;
                                result.Add(new PdfSplitResult
                                {
                                    Pdf         = targetStream.ToArray(),
                                    PageCount   = range.PageCount,
                                    Barcode     = range.Barcode,
                                    BarcodeType = range.BarcodeType
                                });
                            }
                        }
                    }
                }
            }
            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Split pdf by page number
        /// </summary>
        /// <param name="pdf">Pdf to split</param>
        /// <param name="ranges">List of page number</param>
        /// <returns>Splitted pdfs</returns>
        public PdfSplitResult SplitByPageNumbers(byte[] pdf, IList <int> pageNumbers)
        {
            var result = new PdfSplitResult();

            using (var stream = new MemoryStream(pdf))
            {
                using (var pdfInstance = GdPictureHelper.GetPDFInstance())
                {
                    pdfInstance.LoadFromStream(stream);

                    if (pageNumbers == null || pageNumbers.Count == 0)
                    {
                        return new PdfSplitResult {
                                   Pdf = pdf, PageCount = pdfInstance.GetPageCount()
                        }
                    }
                    ;

                    using (var newPdf = GdPictureHelper.GetPDFInstance())
                    {
                        newPdf.NewPDF();

                        foreach (var page in pageNumbers)
                        {
                            newPdf.ClonePage(pdfInstance, page);
                        }

                        using (var targetStream = new MemoryStream())
                        {
                            newPdf.SaveToStream(targetStream);
                            targetStream.Position = 0;
                            result = new PdfSplitResult {
                                Pdf = targetStream.ToArray(), PageCount = pageNumbers.Count
                            };
                        }
                    }
                }
            }
            return(result);
        }
    }
コード例 #4
0
        /// <summary>
        /// Adds a text annotation
        /// </summary>
        /// <param name="pdf">Pdf</param>
        /// <param name="pageNumber">Page number on which the annotation will be placed</param>
        /// <param name="annotation">The annotation which will be added</param>
        /// <returns>The modified Pdf-blob</returns>
        public byte[] AddTextAnnotation(byte[] pdf, int pageNumber, PdfAnnotation annotation)
        {
            using (var stream = new MemoryStream(pdf))
            {
                using (var pdfInstance = GdPictureHelper.GetPDFInstance())
                {
                    pdfInstance.LoadFromStream(stream);
                    pdfInstance.SelectPage(pageNumber);
                    pdfInstance.SetOrigin(PdfOrigin.PdfOriginTopLeft);
                    pdfInstance.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter);
                    pdfInstance.AddFreeTextAnnotation(annotation.Left, annotation.Top + annotation.Height, annotation.Width, annotation.Height, annotation.HasBorder, "", "", annotation.Content, annotation.FontName, annotation.FontSize, annotation.FontColor.R, annotation.FontColor.G, annotation.FontColor.B, annotation.BackgroundColor.R, annotation.BackgroundColor.G, annotation.BackgroundColor.B, annotation.Opacity);

                    using (var targetStream = new MemoryStream())
                    {
                        pdfInstance.SaveToStream(targetStream);
                        targetStream.Position = 0;

                        pdfInstance?.CloseDocument();
                        return(targetStream.ToArray());
                    }
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Adds an empty page to the given pdf.
        /// </summary>
        /// <param name="pdf">Pdf</param>
        /// <param name="pageNumber">Index of the new page, starting with 1 which will insert an empty page as the first page</param>
        /// <returns>The resulting Pdf-blob</returns>
        public byte[] AddEmptyPage(byte[] pdf, int pageNumber)
        {
            using (var stream = new MemoryStream(pdf))
            {
                using (var pdfInstance = GdPictureHelper.GetPDFInstance())
                {
                    pdfInstance.LoadFromStream(stream);
                    pdfInstance.SelectPage(1);
                    float pageWidth  = pdfInstance.GetPageWidth();
                    float pageHeight = pdfInstance.GetPageHeight();

                    pdfInstance.InsertPage(pageWidth, pageHeight, pageNumber);

                    using (var targetStream = new MemoryStream())
                    {
                        pdfInstance.SaveToStream(targetStream);
                        targetStream.Position = 0;

                        pdfInstance?.CloseDocument();
                        return(targetStream.ToArray());
                    }
                }
            }
        }