Archive for July 19th, 2007

Using the iTextSharp library in Delphi .NET

from http://www.noliturbare.com/iTextDelphi.php


This example, taken from from the iText tutorial,
demonstrates how easy the iTextSharp library can be used in Delphi .NET,
using the free (!) Explorer Edition of Turbo Delphi .NET, to create or manipulate PDF’s.
Note that there is also a difference between the original example and the Delpi one:
this C# example is a command line application and the Delphi application is a Windows application.
When run, this simple application creates a PDF file containing just the text “Hello World”.


C# Hello World example


using System;
using System.IO;
using iTextSharp.text;

using iTextSharp.text.pdf;

namespace iTextSharp.tutorial.Chap01
{
public class Chap0101
{
public Chap0101()
{
Console.WriteLine("Chapter 1 example 1: Hello World");
// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:-->

// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add a paragraph to the document
document.Add(new Paragraph("Hello World"));
}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document

document.Close();
}
}
}

Turbo Delphi .NET Hello World example


{Create a new VCL Forms Application. Add a reference to the iTextSharp.dll. Add a button to the form and apply the following code to the button's onclick event}

uses
Windows, Messages, SysUtils, ...., iTextSharp.text, iTextSharp.text.pdf;
....
....

procedure TForm1.Button1Click(Sender: TObject);
var
MyDoc : iTextSharp.text.Document;
Writer : iTextSharp.text.pdf.PdfWriter;
begin
// step 1: creation of a document-object
MyDoc := Document.Create;
try
// step 2: we create a writer that listens to the
// document and directs a PDF-stream to a file

Writer := PdfWriter.GetInstance(MyDoc, TFileStream.Create('C:\Chap0101.pdf', fmCreate));
// step 3: we open the document
MyDoc.Open();
// step 4: we Add a paragraph to the document
MyDoc.Add(Paragraph.Create('Hello World'));
except
ShowMessage('Oops... exception');
end;
// step 5: we close the document

MyDoc.Close();
end;

{Note that the file "Chap0101.pdf" is written to the root of the C: disk; change this as you like}

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • BlinkList
  • blogmarks
  • blogtercimlap
  • connotea
  • DotNetKicks
  • Fark
  • Fleck
  • Gwar
  • Haohao
  • IndianPad
  • Internetmedia
  • LinkaGoGo
  • MyShare
  • Netscape
  • NewsVine
  • Rec6
  • Reddit
  • Scoopeo
  • Slashdot
  • StumbleUpon
  • Technorati
  • Webride

让Pdftk 支持中日韩路径

Pdftk是一个简单的、命令行的PDF编辑软件,可以合并/分割PDF文档、解开必要的输入密码、输出加密、给PDF文档加水印、从PDF文档中解出附件、将PDF文档变成一页等等,能够做到编辑PDF文档的所有事情。

不了解Pdftk的朋友可以到下面的相关资源里找找答案,这里就不费口舌了。

Pdftk 是非常好用,但不支持中日韩路径,这点让我们很多中国朋友很头疼(虽然有变通的解决办法)。
现在喜欢Pdftk的朋友不用急了,我这里提供了可支持中日韩路径的修改版本,包括了修改后的源代码和Windows 程序(4.08M).

相关资源:

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • BlinkList
  • blogmarks
  • blogtercimlap
  • connotea
  • DotNetKicks
  • Fark
  • Fleck
  • Gwar
  • Haohao
  • IndianPad
  • Internetmedia
  • LinkaGoGo
  • MyShare
  • Netscape
  • NewsVine
  • Rec6
  • Reddit
  • Scoopeo
  • Slashdot
  • StumbleUpon
  • Technorati
  • Webride

A easy way to let application compiled by gcj support Chinese path

I have developed some java program and compiled with GCJ, feel free to visit
download center
but they does not support Chinese path now.

pdftk is a very famous software, developed in java and compiled with GCJ, does not support Chinese path,
of course I have release a modified version that supports Chinese path.

so how to let it support Chinese(Japanese, Korean) path? the answer is CNI(the Compiled Native Interface) can help you, and the related source code is


static int writeFile (java::ByteArrayOutputStream* ofs_p, const char* fileName)
{

FILE * pFile;
long lSize;
lSize=ofs_p->size();
char* buffer = (char*) malloc (sizeof(char)*lSize);
jbyteArray out_pdf=ofs_p->toByteArray();
//printf("%d",lSize);
//ofs_p->write(out_pdf, 0, (jint)lSize);

memcpy(buffer,
(char*)(elements(out_pdf)),
lSize );

pFile = fopen ( fileName , "wb" );
//printf(fileName);
fwrite (buffer , sizeof(buffer[0]) , lSize , pFile );
fclose (pFile);
ofs_p->close();
return 0;
}

static jbyteArray readFile(const char* fileName)
{
FILE * pFile;
long lSize;
char * buffer;
size_t result;
//printf(fileName);
pFile = fopen (fileName , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);

// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
//printf ("%d",lSize);

// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

jbyteArray in_pdf= JvNewByteArray(lSize);
memcpy( (char*)(elements(in_pdf)),
buffer,
lSize );

/* the whole file is now loaded in the memory buffer. */

// terminate
fclose (pFile);
free (buffer);
return in_pdf;
}

Related Resource

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • BlinkList
  • blogmarks
  • blogtercimlap
  • connotea
  • DotNetKicks
  • Fark
  • Fleck
  • Gwar
  • Haohao
  • IndianPad
  • Internetmedia
  • LinkaGoGo
  • MyShare
  • Netscape
  • NewsVine
  • Rec6
  • Reddit
  • Scoopeo
  • Slashdot
  • StumbleUpon
  • Technorati
  • Webride