locked
iTextsharp Zoom level. RRS feed

  • Question

  • User-140674451 posted
    
    


    I am using iTextSharp to merge pdf with bookmarks.Mostly it works fine one issue that I am having is this, initially the merged file is loading with zoom to "fit page" as per my requirement. But if I click one of the bookmark the zoom is going to "fit width". I want it to stay at "fit page" all the time. Anyone know how to do this. 

    My code is below.

    protected void Button1_Click(object sender, EventArgs e)
        {
            byte[] buffer = PDFMerge.GetStreamPdf(new string[] { @"C:\PDFs\1.pdf", @"C:\PDFs\2.pdf", @"C:\PDFs\3.pdf" });
            if (buffer != null)
            {
                Response.Clear();
                Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Length", buffer.Length.ToString());      
                Response.BinaryWrite(buffer);
                Response.Flush();
                Response.Close();
                
            }
       }
    public class PDFMerge
    {
    
        public static byte[] GetStreamPdf(string[] pdfFiles)
        {
            byte[] buf = null;
            MemoryStream memStream = new MemoryStream();
            int PageMode = PdfWriter.PageModeUseOutlines;
            PageMode += PdfWriter.PageLayoutOneColumn;
            PdfDestination magnify = new PdfDestination(PdfDestination.FIT);
            int pageNumberToOpenTo = 1;
            bool result = false;
            int pdfCount = 0;
            //total input pdf file count
            int f = 0;
            //pointer to current input pdf file
            string fileName = string.Empty;
            //current input pdf filename
            iTextSharp.text.pdf.PdfReader reader = null;
            int pageCount = 0;
            //cureent input pdf page count
            iTextSharp.text.Document pdfDoc = null;
            //the output pdf document
            PdfWriter writer = null;
    
            PdfContentByte cb = null;
            //Declare a variable to hold the imported pages
            PdfImportedPage page = null;
            int rotation = 0;
            //Declare a font to used for the bookmarks
            iTextSharp.text.Font bookmarkFont = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLUE);
            try
            {
                pdfCount = pdfFiles.Length;
                if (pdfCount > 1)
                {
                    //Open the 1st pad using PdfReader object
                    fileName = pdfFiles[f];
                    reader = new iTextSharp.text.pdf.PdfReader(fileName);
                    //Get page count
                    pageCount = reader.NumberOfPages;
                    //Instantiate an new instance of pdf document and set its margins. This will be the output pdf.
                    //NOTE: bookmarks will be added at the 1st page of very original pdf file using its filename. The location
                    //of this bookmark will be placed at the upper left hand corner of the document. So you'll need to adjust
                    //the margin left and margin top values such that the bookmark won't overlay on the merged pdf page. The 
                    //unit used is "points" (72 points = 1 inch), thus in this example, the bookmarks' location is at 1/4 inch from
                    //left and 1/4 inch from top of the page.
                    pdfDoc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18);
                    //Instantiate a PdfWriter that listens to the pdf document
                    writer = PdfWriter.GetInstance(pdfDoc, memStream);
                    writer.CloseStream = false;
                    //Set metadata and open the document
                    {
                       
                        pdfDoc.AddCreationDate();                
                        pdfDoc.AddTitle("File");                   
                        pdfDoc.Open();
                    }
                    //Instantiate a PdfContentByte object
                    cb = writer.DirectContent;
    
                    //Now loop thru the input pdfs
                    while (f < pdfCount)
                    {
    
                        //Declare a page counter variable
                        int i = 0;
                        //Loop thru the current input pdf's pages starting at page 1
                        while (i < pageCount)
                        {
                            i += 1;
                            //Get the input page size
                            //jm pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i));
                            //Create a new page on the output document
    
                            pdfDoc.NewPage();
                            //If it is the 1st page, we add bookmarks to the page
                            if (i == 1)
                            {
                                //First create a paragraph using the filename as the heading
                                iTextSharp.text.Paragraph para = new iTextSharp.text.Paragraph(Path.GetFileName(fileName).ToUpper(), bookmarkFont);
                                //Then create a chapter from the above paragraph
                                iTextSharp.text.Chapter chpter = new iTextSharp.text.Chapter(para, f + 1);
                                //Finally add the chapter to the document
                                pdfDoc.Add(chpter);
                            }
                            //Now we get the imported page
                            page = writer.GetImportedPage(reader, i);
                            //Read the imported page's rotation
                            rotation = reader.GetPageRotation(i);
                            //Then add the imported page to the PdfContentByte object as a template based on the page's rotation
                            if (rotation == 90)
                            {
                                cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                            }
                            else if (rotation == 270)
                            {
                                cb.AddTemplate(page, 0, 1f, -1f, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30);
                            }
                            else
                            {
    
                                //cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                                cb.AddTemplate(page, PageSize.LETTER.Width / reader.GetPageSize(i).Width, 0, 0, PageSize.LETTER.Height / reader.GetPageSize(i).Height, 0, 0);
                            }
                        }
                        //Increment f and read the next input pdf file
                        f += 1;
                        if (f < pdfCount)
                        {
                            fileName = pdfFiles[f];
                            reader = new iTextSharp.text.pdf.PdfReader(fileName);
                            pageCount = reader.NumberOfPages;
                        }
                    }
                    PdfAction zoom = PdfAction.GotoLocalPage(pageNumberToOpenTo, magnify, writer);
                    writer.ViewerPreferences = PageMode;
                    writer.SetOpenAction(zoom);
                    //When all done, we close the documwent so that the pdfwriter object can write it to the output file
                    pdfDoc.Close();
                    buf = new byte[memStream.Position];
                    memStream.Position = 0;
                    memStream.Read(buf, 0, buf.Length);
    
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return buf;
        }
    
    
    
    }
    



    Thursday, April 19, 2012 7:19 PM

All replies

  • User-1669826746 posted

    I am using iTextSharp to merge pdf with bookmarks.Mostly it works fine one issue that I am having is this, initially the merged file is loading with zoom to "fit page" as per my requirement. But if I click one of the bookmark the zoom is going to "fit width". I want it to stay at "fit page" all the time. Anyone know how to do this

    I am having literally this exact same problem, can anyone help?

    Monday, May 14, 2012 11:40 AM