iTextSharp set document landscape (horizontal) A4 - c#

How can I set an A4 document in landscape (horizontal) format in iTextSharp?

You can set the page size to a rotated A4. E.g. (assuming PDF, but should apply regardless):
iTextSharp.text.Document doc;
// ...initialize 'doc'...
// Set the page size
doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
I've done this with PDF without trouble, haven't tried it with other doc types.

You can initialize a new document like that:
Document doc = new Document(iTextSharp.text.PageSize.A4.Rotate(), 10, 10, 10, 10);
In this mode all pages will be in landscape mode.
To change the layout of the page inside the document you can use:
doc.SetPageSize(iTextSharp.text.PageSize.A4); // for vertical layout
doc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate()); // for horizontal layout

Related

How to get the remaining page size using syncfusion pdf

i am creating pdf using synfusion pdf plugin for xamarin forms and what happenes if i print my pdf then there is many remaining space on the page so how i can get that remaining space of pdf page using syncfusion pdf
We can get the blank space region in the page of PDF document by using PdfLayoutResult. Please refer the below code snippet for more details,
C#:
//Create a text element with the text and font
PdfTextElement textElement = new PdfTextElement(text, font);
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Layout = PdfLayoutType.Paginate;
//Draw the paragraph
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0,
page.GetClientSize().Width, page.GetClientSize().Height), layoutFormat);
//Get the blank space of PDF using PdfLayoutResult and page height
float blankSpaceHeight = page.GetClientSize().Height - result.Bounds.Bottom;
We have created the sample for the same which can be downloaded from below link,
Sample to get the remaining page size of PDF document

EvoPDF page margins set into the HTML

I'm using the EvoPDF with which I save HTML to PDF file. The HTML contains long text (can contains lists, tables, etc.). I want to add pages margins, but I don't want to use the pdfConverter.PdfDocumentOptions.{Bottom/Top}Margin - properties, I want to set pages margins into the HTML (something like in Microsoft Word, where I set the page size and margins and text auto move to next page, which has previously set margins). I previewed their Help page, but I couldn't find info about that.
My Convert code is:
EvoPdf.HtmlToPdf.PdfConverter pdfConverter = new EvoPdf.HtmlToPdf.PdfConverter();
pdfConverter.LicenseKey = System.Configuration.ConfigurationManager.AppSettings["EvoHtmlToPdfLicence"];
pdfConverter.PdfDocumentOptions.PdfPageSize = EvoPdf.HtmlToPdf.PdfPageSize.Letter;
pdfConverter.PdfDocumentOptions.PdfPageOrientation = EvoPdf.HtmlToPdf.PdfPageOrientation.Portrait;
byte[] pdf = pdfConverter.GetPdfBytesFromHtmlString(htmlText);
Thank you!
The Margin properties are global thus header, footer, body are affected by this.
If you want to effect the rendered HTML solely, you can use the following settings:
Documentation
HTML Content Destination in PDF. The HTML content destination is given by the X and Y coordinates where to start rendering in first PDF page and by the destination rectangle width and height. All the values are expressed in points. 1 point is 1/72 inches. If you don't set any destination rectangle then by default the converter will start rendering in the top left corner of the first page, will try to use the entire PDF page width for rendering and will auto determine the destination rectangle height such that the entire HTML content is visible. The properties you can set in your code to control the HTML content destination in PDF are X, Y, Width and Height. [...]
HTML Content Top and Bottom Spacing. Using these options you can set a top and a bottom padding for the HTML content. This can be useful for example when you want to introduce a spacing between the PDF page header or footer and the main content. The properties you can set in your code to control the top and bottom spacing are TopSpacing and BottomSpacing. [...]
Code example:
// Category: HTML Content Destination and Spacing Options
// Set HTML content destination in PDF page
if (xLocationTextBox.Text.Length > 0)
htmlToPdfConverter.PdfDocumentOptions.X = float.Parse(xLocationTextBox.Text);
if (yLocationTextBox.Text.Length > 0)
htmlToPdfConverter.PdfDocumentOptions.Y = float.Parse(yLocationTextBox.Text);
if (contentWidthTextBox.Text.Length > 0)
htmlToPdfConverter.PdfDocumentOptions.Width = float.Parse(contentWidthTextBox.Text);
if (contentHeightTextBox.Text.Length > 0)
htmlToPdfConverter.PdfDocumentOptions.Height = float.Parse(contentHeightTextBox.Text);
// Set HTML content top and bottom spacing or leave them not set to have no spacing for the HTML content
htmlToPdfConverter.PdfDocumentOptions.TopSpacing = float.Parse(topSpacingTextBox.Text);
htmlToPdfConverter.PdfDocumentOptions.BottomSpacing = float.Parse(bottomSpacingTextBox.Text);

Header and Footer in ITextSharp

I know this question has been asked a thousand times, but I am yet to find a straight forward answer. I am relatively new to ITextSharp, so please explain as if you were talking to a toddler. How can I add a simple text only header and footer to the document I am creating?
I am creating a simple pdf document with the following code:
void Button1Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.LEDGER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(#"File Path", FileMode.Create));
doc.Open();
Paragraph par1 = new Paragraph("Hello World!");
doc.Add(par1);
//Code to add header/footer
doc.Close();
MessageBox.Show("Your PDF has been created!");
}
I have done a lot of research on how to add headers and footers, but they all have to do with complicated page events. Is there an easier way? If there isn't, can you walk me through the process step by step? I would really appreciate any help that you all can give. Thanks!
You are creating your Document like this:
Document doc = new Document(iTextSharp.text.PageSize.LEDGER, 10, 10, 42, 35);
This means that you have a top margin of 42 user units and a bottom margin of 35 user units. You can use this margin to add extra content in a page event.
The official web site has plenty of examples and a comprehensive Q&A section. All examples and answers are tagged. If you click on the header tag, you can find plenty of examples.
As already indicated by others in the comments, you need to create a PdfPageEvent implementation. The easiest way to do this, is by extending the PdfPageEventHelper class.
class MyHeaderFooterEvent : PdfPageEventHelper {
Font FONT = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
public override void OnEndPage(PdfWriter writer, Document document) {
PdfContentByte canvas = writer.DirectContent;
ColumnText.ShowTextAligned(
canvas, Element.ALIGN_LEFT,
new Phrase("Header", FONT), 10, 810, 0
);
ColumnText.ShowTextAligned(
canvas, Element.ALIGN_LEFT,
new Phrase("Footer", FONT), 10, 10, 0
);
}
}
Important to know:
It is forbidden to add content in the OnStartPage() event. Add your header and footer when all the content has been added to the page just before iTextSharp moves to a new page. More specifically: add content in the OnEndPage() event.
It is forbidden to add content to the Document object that is passed to the event. This object can be used for read-only purposes only.
If you examine the OnEndPage() method in the MyHeaderFooterEvent class, you see that we get the DirectContent from the writer and that we add content to this canvas using ShowTextAligned methods. There are many other ways to add content, but you explicitly asked for the easiest way. This way has its limitations, but it's easy.
I used a couple of hard-coded values: I used 10 as the x value for the header and the footer. That's because you defined a left margin of 10 user units. The header and footer are left aligned with the actual content you're adding to the page. I used 810 for the y value of the header because you're creating an A4 page with a top margin of 42. The top y coordinate of your page is 842. The top y coordinate of the top margin is 842 - 42 = 800. I added 10 user units so that your header isn't glued to the actual content. The bottom y coordinate of the page is 0 in your case and the bottom y coordinate of the margin in 35. I used 10 for the base line of the footer.
You created wri and right after creating this PdfWriter instance, you open the Document instance. For the page event to take effect, you should add the following link, right before opening the Document:
wri.PageEvent = new MyHeaderFooterEvent();
Now the OnEndPage() method will be invoked every time your main process finalizes a page.
Important: you added //Code to add header/footer at the wrong place. iTextSharp will try to flush the content of a page as soon as possible. If you add code to add a header/footer after you've added content, you can not go back to add a header and footer to pages that were already flushed to the output stream.

iTextSharp A4 size paper printing to 2 pages

So a ITextSharp created 1 page PDF is printing to two pages using ITextSharp and I don't know why.
This is how I declare my document using ITextSharp:
var document = new Document(PageSize.A4, 0, 0, 0, 0);
Whether there is content added or not, if you go to print the PDF it prints two pages but whether you view in Adobe Acrobat, Adobe Reader or Chrome PDF viewer it is a one page document.

How Can I make my PDF exactly fit within 4x6 inches? currently It is printing in regular A4 document

I am creating Shipping Label using iTextSharp.
What I am doing is Creating a Label in PDF so I can format it in any way I want and then send it to my THERMAL PRINTER.
My problem is, My labels are of size 4x6 (standard shipping label). These are the labels which we see on UPS & Fedex Packages. How Can i make my PDF exactly fit within 4x6 inches? currently It is printing in regular A4 document.
I am using following:
Dim document As New Document()
document.SetPageSize(PageSize.A4_LANDSCAPE)
Set a Custom Page Size:
Dim pgSize As New iTextSharp.text.Rectangle(myWidth, myHeight)
Dim doc As New iTextSharp.text.Document(pgSize, leftMargin, rightMargin, topMargin, bottomMargin)
iTextSharp uses 72 pixels per inch, so if you know the height and width of your desired page size in inches, just multiply those numbers by 72 to get myWidth and myHeight.
https://stackoverflow.com/a/2503476/102937
I would recommend producing raw printer language. Thermal bar code printers all have a native language. Languages such as ZPLII (Zebra Printer Language 2) or DPL (Datamax Printer Language). You can build them as a string and pass them directly to the printer. Searching the printer manufactures website you can quickly find the printer language manual for the printer you are using.
The great advantage to this method is control and speed. As Zebras and Datamax printers do not actually care about a page size you can focus on rendering the data you want in the size and orientation you want.
You may also be able to take advantage of some of the extra logic that the printers have. This is especially useful for serialized tags with sequential numbering. A single string sent to the printer can produce dozens to hundreds of labels. If you are going to do a lot of thermal bar code printing I strongly recommend understanding the power these printers contain in their native languages.
To Set document size use like this:-
Document doc = new Document(new iTextSharp.text.Rectangle(295f, 420f), 0f, 0f, 0f, 0f);
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
-----------
-----
---------
For font here is code:-
iTextSharp.text.Font myFont1 = new iTextSharp.text.Font() { Size = 4.5f };
PdfPTable header1 = new PdfPTable(2);
header1.AddCell(new PdfPCell(new Phrase("", myFont1 )) { UseAscender = true, PaddingTop = 0, Border = 0, HorizontalAlignment = 0 });
i have just added other property's for you information future use.
happy coding!!

Categories

Resources