Click here to Skip to main content
15,946,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear Team,

I am working in pdf file manipulations using pdfclown.dll. And my question is when i create and write pdf pages dynamically, how can i identify if the current page is reached to end and how can i move next page automatically using this dll.

Please find the below sample code for your reference

C#
using org.pdfclown.documents;
using org.pdfclown.documents.contents.colorSpaces;
using org.pdfclown.documents.contents.composition;
using fonts = org.pdfclown.documents.contents.fonts;
using files = org.pdfclown.files;
using org.pdfclown.documents.contents.fonts;

using System;
using System.Drawing;
using System.IO;


namespace TypeSettingSamples
{
    class MainClass
    {
        private static readonly int Margin_X = 50;
        private static readonly int Margin_Y = 50;

        public  void Run()
        {
            string mainFilePath = @"E:\2014\oct-2014\06(MonoDevelop)\sample.pdf";
            org.pdfclown.files.File file = new files.File (mainFilePath);
            Document document = file.Document;
           
            Build(document);

        }

        private void Build(Document document)
        {
            Page page = new Page(document); 

            document.Pages.Add(page); 

            SizeF pageSize = page.Size;

            PrimitiveComposer composer = new PrimitiveComposer(page);

            BlockComposer blockComposer = new BlockComposer(composer);

            composer.BeginLocalState();

            RectangleF frame = new RectangleF(Margin_X,    Margin_Y,(float)pageSize.Width - Margin_X * 2,(float)pageSize.Height - Margin_Y * 2);

            blockComposer.Begin(frame,XAlignmentEnum.Left,YAlignmentEnum.Top);
           
            composer.SetFont(new StandardType1Font(document,StandardType1Font.FamilyEnum.Times,true,false),32);

            blockComposer.ShowText("This is Chapter 1");

            blockComposer.ShowBreak();

            blockComposer.ShowText("Down the Rabbit-Hole");
            
            blockComposer.End();
            

            frame = new RectangleF((float)blockComposer.BoundBox.X,(float)blockComposer.BoundBox.Y + blockComposer.BoundBox.Height,(float)blockComposer.BoundBox.Width,(float)pageSize.Height - Margin_Y - (blockComposer.BoundBox.Y + blockComposer.BoundBox.Height)
            );
            // Begin the body block!
            blockComposer.Begin(frame,XAlignmentEnum.Justify,YAlignmentEnum.Bottom);

            composer.SetFont(new StandardType1Font(document,StandardType1Font.FamilyEnum.Times,true,false),14);

            composer.BeginLocalState();
            //composer.SetFont(decorativeFont,28);
            blockComposer.ShowText("A");
            composer.End();
            blockComposer.ShowText("lice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversation?'");

            // Define new-paragraph first-line offset!
            SizeF breakSize = new SizeF(24,8); 

            // Begin a new paragraph!
            blockComposer.ShowBreak(breakSize);
            blockComposer.ShowText("So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.");

            // Begin a new paragraph!
            blockComposer.ShowBreak(breakSize);
            blockComposer.ShowText("There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT- POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.");
       // End the body block!

   //*Here the problem full content that i have writing here is not written properly. It closed just one page also the content has break in the middle*

      blockComposer.End();
      composer.End();
      composer.BeginLocalState();
      composer.Rotate(90,new PointF(pageSize.Width - 50,pageSize.Height - 25));
      blockComposer = new BlockComposer(composer);
      blockComposer.Begin(new RectangleF(0,0,300,50),    XAlignmentEnum.Left,YAlignmentEnum.Middle);
     composer.SetFont(new StandardType1Font(document,StandardType1Font.FamilyEnum.Times,true,false),8);
       blockComposer.ShowBreak();
       blockComposer.End();
       composer.End();
       composer.Flush();
       document.File.Save (@"E:\2014\oct-2014\07(PafeWrapping)\outputPdf.pdf", files.SerializationModeEnum.Standard);
        }
        public static void Main (string[] args)
        {
            //Console.WriteLine ("Hello World!");
            MainClass mc = new MainClass ();
            mc.Run ();
            Console.ReadKey ();
        }
    }
}
Posted
Updated 7-Oct-14 2:04am
v3
Comments
[no name] 7-Oct-14 5:51am    
PdfReader reader
= new PdfReader("http://www.chinehamchat.com/Chineham_Chat_Advertisements.pdf");
// total number of pages
int n = reader.NumberOfPages;


You can get the total page like that na?
Member 9885328 7-Oct-14 5:55am    
Yes i can get the page numbers as you mentioned...
[no name] 7-Oct-14 5:57am    
Then what is the problem and what exactly your requirement is?
[no name] 7-Oct-14 5:58am    
Simply you can check right, whether the last number of page number and current page number is same or not?
Member 9885328 7-Oct-14 6:04am    
Hi i have provide sample code for your referece

string sourceFilePath = @"E:\2014\oct-2014\06(MonoDevelop)\sample.pdf";
using(File sourceFile = new File(sourceFilePath))
{
// Source page collection.
Pages sourcePages = sourceFile.Document.Pages;
// Source page count.
int sourcePagesCount = sourcePages.Count;
// First page to add.
int fromSourcePageIndex =0;
// Last page to add.
int toSourcePageIndex = 1;
// Target position.
int targetPageIndex = mainPagesCount + 1;
// Add the chosen page range to the main document!

new PageManager(mainDocument).Add(targetPageIndex,sourcePages.GetSlice(fromSourcePageIndex,toSourcePageIndex));

Page page = new Page(mainDocument);
mainDocument.Pages.Add(page);

SizeF pageSize = page.Size;

SizeF breakSize = new SizeF(24,8);

PrimitiveComposer composer = new PrimitiveComposer(page);

BlockComposer bComposer = new BlockComposer (composer);
composer.BeginLocalState ();
RectangleF frame = new RectangleF(Margin_X, Margin_Y,(float)pageSize.Width - Margin_X * 2,(float)pageSize.Height - Margin_Y * 2);
bComposer.Begin(frame,XAlignmentEnum.Left,YAlignmentEnum.Top);
composer.SetFont(new StandardType1Font(mainDocument,StandardType1Font.FamilyEnum.Times,true,false),14);

bComposer.ShowText ("Abstract");

bComposer.ShowBreak(breakSize);

bComposer.ShowText("This paper investigates the convergence of local price levels within Japan, using long-run data spanning six decades and based on a pairwise approach. The analysis investigates convergence of all binary combinations of local price levels, rather than choosing a particular city as reference. The results indicate that the null hypothesis of no convergence is rejected, but also that a substantial portion of local price levels fail to converge within Japan. Convergence is limited to some extent. The extent does not substantially change over periods. The results also show that the likelihood of convergence is influenced by population growth rates and the distance between areas. With differing population growth rates and a large distance between locations, convergence between areas is unlikely");

bComposer.ShowBreak (breakSize);
bComposer.ShowText("This paper investigates the convergence of local price levels within Japan, using long-run data spanning six decades and based on a pairwise approach. The analysis investigates convergence of all binary combinations of local price levels, rather than choosing a particular city as reference. The results indicate that the null hypothesis of no convergence is rejected, but also that a substantial portion of local price levels fail to converge within Japan. Convergence is limited to some extent. The extent does not substantially change over periods. The results also show that the likelihood of convergence is influenced by population growth rates and the distance between areas. With differing population growth rates and a large distance between locations, convergence between areas is unlikely");

bComposer.ShowBreak ();

//Here my problem if the page reached at the end the it wont go for the next page automatically writing remaing content

bComposer.End ();

composer.Flush();

mainDocument.File.Save (@"E:\2014\oct-2014\06(MonoDevelop)\output.pdf", SerializationModeEnum.Standard);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900