I have a long webpage and I am making its image,
and then cutting image horizontally to multiple image to fit in A4 pdf page
i used itextsharp for PDF,
problem is image doesn't fit in A4 page, it should be in good shape while printing.
CustomPdfWriter oPdfwriter = new CustomPdfWriter(@"D:\logg");
oPdfwriter.CreateImage("http://www.codeproject.com/");
oPdfwriter.CreatePdf();
public class CustomPdfWriter
{
string TempfileName = string.Empty;
public CustomPdfWriter(string tempLocation)
{
TempfileName = tempLocation;
}
public void CreateImage(string url)
{
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = true;
browser.ScriptErrorsSuppressed = true;
browser.AllowNavigation = true;
browser.Navigate(url);
browser.Width = 1024;
browser.Height = 5680;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
public void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
{
browser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, browser.Width, browser.Height));
bitmap.Save(TempfileName + @"\img.png", System.Drawing.Imaging.ImageFormat.Png);
}
}
public System.Drawing.Rectangle SplitImage(Bitmap bitmap, int x, int y, int width, int height, int part, int sequence)
{
Bitmap originalImage = bitmap;
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(x, y, width, height);
Bitmap firstHalf = originalImage.Clone(rect, originalImage.PixelFormat);
firstHalf.Save(TempfileName + @"\img" + sequence.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
return rect;
}
public void CreatePdf()
{
string imageURL = TempfileName + @"\img.png";
System.Drawing.Bitmap bitmap = new Bitmap(imageURL);
System.Drawing.Rectangle rect1 = SplitImage(bitmap, 0, 0, 1024, 1420, 4, 1);
System.Drawing.Rectangle rect2 = SplitImage(bitmap, 0, 1420, 1024, 1420, 4, 2);
System.Drawing.Rectangle rect3 = SplitImage(bitmap, 0, 2840, 1024, 1420, 4, 3);
System.Drawing.Rectangle rect4 = SplitImage(bitmap, 0, 4260, 1024, 1420, 4, 4);
Document doc = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
string pdfFilePath = TempfileName;
PdfWriter writer = PdfAWriter.GetInstance(doc, new FileStream(pdfFilePath + @"\Default.pdf", FileMode.Create));
doc.Open();
try
{
iTextSharp.text.Image jpg1 = iTextSharp.text.Image.GetInstance(TempfileName + @"\img1.png");
iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(TempfileName + @"\img2.png");
iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(TempfileName + @"\img3.png");
iTextSharp.text.Image jpg4 = iTextSharp.text.Image.GetInstance(TempfileName + @"\img4.png");
doc.Add(jpg1);
doc.NewPage();
doc.Add(jpg2);
doc.NewPage();
doc.Add(jpg3);
doc.NewPage();
doc.Add(jpg4);
}
catch (Exception ex)
{ }
finally
{
doc.Close();
}
}
}
What I have tried:
Please see above code, and please let me know if there are better options to accomplish it.