从Pdf中删除出血并合并

时间:2015-10-01 11:36:17

标签: c#-4.0 pdf-generation itextsharp

   I am trying remove 3mm bleed size from pdf. by using below criteria 

enter image description here

我的源文件是Source file

我使用下面的代码修剪左右

    public void TrimLeftandRight(string sourceFilePath, string outputFilePath)
    {

        PdfReader pdfReader = new PdfReader(sourceFilePath);
        float width = (float)GetPDFwidth(sourceFilePath);
        float height = (float)GetPDFHeight(sourceFilePath);
        float widthTo_Trim = iTextSharp.text.Utilities.MillimetersToPoints(3);



        PdfRectangle rectrightside = new PdfRectangle(0, 0, width - widthTo_Trim, height);
        PdfRectangle rectLeftside = new PdfRectangle(widthTo_Trim, 0, width, height);

        //     int[] pagealignment = new int[] { 8, 1, 2, 7, 6, 3, 4, 5 };
        int[] pagealignment = new int[] { 6, 1, 2, 5, 4, 3 };
        using (var output = new FileStream(outputFilePath, FileMode.CreateNew, FileAccess.Write))
        {
            // Create a new document
            Document doc = new Document();

            // Make a copy of the document
            PdfSmartCopy smartCopy = new PdfSmartCopy(doc, output);

            // Open the newly created document
            doc.Open();

            // Loop through all pages of the source document
            for (int i = 1; i <= pdfReader.NumberOfPages; i++)
            {
                // Get a page
                var page = pdfReader.GetPageN(i);

                // Apply the rectangle filter we created

                switch (i)
                {
                    case 6:
                        page.Put(PdfName.CROPBOX, rectLeftside);
                        page.Put(PdfName.MEDIABOX, rectrightside);

                        break;
                    case 2:
                        page.Put(PdfName.MEDIABOX, rectrightside);

                        break;
                    case 4:

                        page.Put(PdfName.MEDIABOX, rectLeftside);
                        break;
                    case 1:
                        page.Put(PdfName.MEDIABOX, rectLeftside);
                        break;
                    case 5:
                        page.Put(PdfName.MEDIABOX, rectrightside);
                       // page.Put(PdfName.CROPBOX, rectLeftside);
                        break;
                    case 3:

                        page.Put(PdfName.CROPBOX, rectLeftside);
                        page.Put(PdfName.MEDIABOX, rectrightside);
                        break;

                }
                // Copy the content and insert into the new document
                var copiedPage = smartCopy.GetImportedPage(pdfReader, i);
                smartCopy.AddPage(copiedPage);
            }

            // Close the output document
            smartCopy.Close();
            doc.Close();
            doc.Dispose();

        }
    }

上面代码的输出产生 Trimmed left and right file

我使用下面的代码来合并修剪过的文件

    public void CreategateFinalOutput(string inputfile)
    {

        double widthinpoints = iTextSharp.text.Utilities.MillimetersToPoints(897);
        string onlyfilename = Path.GetFileName(inputfile);
        // string originalfilename = Server.MapPath("~/Uploads/" + onlyfilename);
        int Noofpagesinpdf = GetNoofpagesofpdf(inputfile);
        //  var a3doc = new Document(PageSize.A3.Rotate(), 0, 0, 0, 0);
        double originalwidth = GetPDFwidth(inputfile);

        float widthTo_Trim = iTextSharp.text.Utilities.MillimetersToPoints(3);

        double width = (GetPDFwidth(inputfile) * 3);
        width = widthinpoints;
        double height = GetPDFHeight(inputfile);

        var a3reader = new PdfReader(inputfile);
        var a3doc = new Document(new Rectangle((float)width, (float)height));

        var a3writer = PdfWriter.GetInstance(a3doc, new FileStream(Server.MapPath("~/RP/" + onlyfilename), FileMode.Create));

        a3doc.Open();

        var a3cb = a3writer.DirectContent;

        PdfImportedPage page;

        int totalPages = a3reader.NumberOfPages;

        //  int[] pagealignment = new int[] { 8, 1, 2, 7, 6, 3, 4, 5 };
        int[] pagealignment = new int[] { 5, 6, 1, 2, 3, 4 };
        int iteration = 1;
        for (int i = 1; i <= totalPages; i++)
        {
            a3doc.NewPage();

            var a3size = new Document(new Rectangle((float)width, (float)height));
            //new code
            int fistpage = 0;
            int secpage = 0;
            int thirdpage = 0;
            switch (iteration)
            {
                case 1:
                    fistpage = 5;
                    secpage = 6;
                    thirdpage = 1;
                    break;
                case 2:
                    fistpage = 2;
                    secpage = 3;
                    thirdpage = 4;
                    break;
            }


                    double trimwidth = iTextSharp.text.Utilities.MillimetersToPoints(3);

                    page = a3writer.GetImportedPage(a3reader, fistpage);
                    double pagewidth = page.Width;
                    a3cb.AddTemplate(page, 0, 0);

                    i++;

                    page = a3writer.GetImportedPage(a3reader, secpage);
                    double pagewidtha = page.Width;

                    a3cb.AddTemplate(page, (float)(pagewidtha), 0);

                    i++;

                    page = a3writer.GetImportedPage(a3reader, thirdpage);
                    double pagewidthaThird = page.Width;
                    // a3cb.AddTemplate(page, (int)(a3size.Width / 2), 0); //commented
                    a3cb.AddTemplate(page, (float)(pagewidthaThird + pagewidth), 0);
                    iteration++;


            a3doc.Close();
        }
    }

当我使用上面的代码合并pdf时,输出不是按照愿望 Final output 这里我们删除了第5页和第6页的边框,但是当我们合并时,边框出现了。

你可以看到它为这么大的代码下载pdfs ..道歉。将非常感谢帮助

我请求下载pdfs并检查pdf以获得更好的视图

0 个答案:

没有答案