Crop one inch of PDF file - c#

I need to resize a single page PDF from 8X11 inch to 8X9 inch (or any size), without resizing the content at all.
How can I do this in C# ?
Thanks

This should be possible using any decent general purpose PDF library.
For example, to crop 1 inch from the right using iText 7:
using (PdfReader reader = new PdfReader(SOURCE_PDF))
using (PdfWriter writer = new PdfWriter(TARGET_PDF))
using (PdfDocument document = new PdfDocument(reader, writer))
{
for (int i = 1; i <= document.GetNumberOfPages(); i++)
{
PdfPage page = document.GetPage(i);
Rectangle cropBox = page.GetCropBox();
cropBox.SetWidth(cropBox.GetWidth() - 72);
page.SetCropBox(cropBox);
}
}
If, as you mention in a comment, you actually want to cut the right part of the PDF to have a PDF of 88mm of width, keeping the same height, replace
cropBox.SetWidth(cropBox.GetWidth() - 72);
by
cropBox.SetWidth(88f * 72f / 25.4f);
The crop box dimensions are given in default user space units which in turn default to 1⁄72 inch. Thus, to set a dimension given in mm one has to multiply that number by (72/25.4) first.
Two remarks:
Actually the default user space unit may differ if the page UserUnit property is set which is specified as
a positive number that shall give the size of default user space units, in multiples of 1⁄72 inch. The range of supported values shall be implementation-dependent.
Default value: 1.0 (user space unit is 1⁄72 inch).
(ISO 32000-1, Table 30 – Entries in a page object)
This property is seldom used, though, in particular because of the "The range of supported values shall be implementation-dependent" bit, so I ignored it above.
If you don't want to crop but instead to enlarge the page area, you'll probably not only need to enlarge the CropBox but also the MediaBox.
The crop, bleed, trim, and art boxes shall not ordinarily extend beyond the boundaries of the media box. If they do, they are effectively reduced to their intersection with the media box.
(ISO 32000-1, section 14.11.2.1 Page Boundaries / General)
PdfPage has analogous methods GetMediaBox and SetMediaBox that can be used for enlarging the MediaBox.

Related

Resolution of the generated pdf file

I have a problem with the resolution in a PDF file generated on the basis of HTML
I am using HtmlToPdfConverter from Syncfusion together with WebKitConverter
The A4 paper size is set to 595px x 842px
I worked on Windows 7 with an older screen. Everything was fine
When run on a Windows 10 system with a different screen, the paper content is larger than paper. The text is too big, the pictures are too big
The size of the text is also set in pixels.
For sure the difference is in PPI
A code snippet with generator settings:
PdfMargins margins = new PdfMargins();
margins.Left = 50;
margins.Right = 50;
margins.Top = 0;
margins.Bottom = 0;
settings.Margin = margins;
settings.Orientation = PdfPageOrientation.Portrait;
settings.WebKitViewPort = new System.Drawing.Size(595, 0);
settings.PdfPageSize = new SizeF(595, 842);
Is it possible to set in some way that it will always work?
The content size of PDF document may different based on machine/application where the conversion take place. If you need a same output in all the machine/application then please set a fixed viewport size in WebKitViewPort property. WebKit HTML converter internally using system viewport size for converting HTML to PDF. The system viewport size may differs based on the system and application.
Even the fixed WebKitViewPort size does not helps, please provide us the complete code snippet for the conversion, so that it will helpful for us to analyze and assist you further on this.
Please refer below links for more details about WebKit viewport.
UG: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/webkit#viewport
KB: https://www.syncfusion.com/kb/7412/
Note: I work for Syncfusion
Try setting the size in em instead of px for you text sizing. You can set the pictures in percentage. You want to make everything scaled to the paper size.
“Ems” (em): The “em” is a scalable unit that is used in web document
media. An em is equal to the current font-size, for instance, if the
font-size of the document is 12pt, 1em is equal to 12pt. Ems are
scalable in nature, so 2em would equal 24pt, .5em would equal 6pt, etc
Meet the Units
“Ems” (em): The “em” is a scalable unit that is used in web document
media. An em is equal to the current font-size, for instance, if the
font-size of the document is 12pt, 1em is equal to 12pt. Ems are
scalable in nature, so 2em would equal 24pt, .5em would equal 6pt,
etc. Ems are becoming increasingly popular in web documents due to
scalability and their mobile-device-friendly nature.
Pixels (px): Pixels are fixed-size units that are used in screen
media (i.e. to be read on the computer screen). One pixel is equal
to one dot on the computer screen (the smallest division of your
screen’s resolution). Many web designers use pixel units in web
documents in order to produce a pixel-perfect representation of
their site as it is rendered in the browser. One problem with the
pixel unit is that it does not scale upward for visually-impaired
readers or downward to fit mobile devices.
Points (pt): Points are traditionally used in print media (anything
that is to be printed on paper, etc.). One point is equal to 1/72 of
an inch. Points are much like pixels, in that they are fixed-size
units and cannot scale in size.
Percent (%): The percent unit is much like the “em” unit, save for a
few fundamental differences. First and foremost, the current
font-size is equal to 100% (i.e. 12pt = 100%). While using the
percent unit, your text remains fully scalable for mobile devices
and for accessibility.
So, What’s the Difference?
It’s easy to understand the difference between font-size units when you see them in action. Generally, 1em = 12pt = 16px = 100%. When using these font-sizes, let’s see what happens when you increase the base font size (using the body CSS selector) from 100% to 120%
As you can see, both the em and percent units get larger as the base font-size increases, but pixels and points do not. It can be easy to set an absolute size for your text, but it’s much easier on your visitors to use scalable text that can display on any device or any machine. For this reason, the em and percent units are preferred for web document text.
https://kyleschaeffer.com/css-font-size-em-vs-px-vs-pt-vs-percent

Create a way in .NET to determine X,Y locations on a PDF for printing

I have a PDF that is a template I will use to print onto special paper stock in a printer. I want to create a way to use the mouse to capture x,y locations when i click on certain areas of the pdf. The pdf template is based on an 8 1/2 x 11 piece of paper.
One of my ideas was to convert the page in the pdf to a jpg and load it into a picturebox using c# and a windows forms application. Then I can capture the x,y when i click the mouse but I'm not sure how that will translate to the pdf.
I'm open to opensource suggestions but I also have both the Aspose Words and Aspose PDF products. I'd also prefer a .NET solution.
Rather than storing a direct X/Y pixel co-ordinate then, you could store them as a % of the page.
So go down your jpeg idea, get the pixel X,Y of your location, then divide those by the Width,Height of the Jpeg to give you X%,Y%
When you come to do your printing, get the paper size and multiply that by your X%,Y% to get your offset within the print document
In the end what worked for me was finding out that 1 inch = 72 points. So as far as the Aspose library was concerned that conversion factor worked for me. THEN I created a jpeg that was 1 pixel by 1 pixel and would stamp that on the PDF using the XIndent and the YIndent. This allowed me to stamp a pixel in the upper left corner of pdf and then using the width and height of the page and the conversion factor I was able to deduce the other 3 corners!

How to calculate the number of characters that can be accommodated in a page of a given font and size

I was creating a C# Console Application that fills in the details in a certificate template (pdf) file using ITextSharp. The text that needs to be filled is dynamically generated.
If the dynamically generated text exceeds a certain size the field name gets truncated. I have already thought of an algorithm to solve this problem for which I require the maximum number of characters that can be accommodated in a page with the given font and font-size. The width of the page where the text has to be accommodated is given to us already.
I used the following code:
string text = "This is the test string that needs to be accommodated";
// Registering a font
FontFactory.Register("somefont.ttf", "script");
var script = FontFactory.GetFont("script", 20f); // registers the font with the given type and size.
Phrase phrase_text = new Phrase();
phrase_text.Add(new Chunk(text, script));
ColumnText.ShowTextAligned(pbover, Element.ALIGN_CENTER, new Phrase(phrase_text), PageSize.A4.Height/2, 140, 0);
This code seems to work fine, but when I am adding a bigger text in the screen, the text gets truncated with the alignment still in the center (in the horizontal sense).
I was reading something on using the Graphics library, but could not resolve the issue.
You say that you are filling out a template. That assumes that you have at least one AcroForm field in your document. Filling out fields is explained in the answer to this question: itextsharp setting the stamper FormFlatttening=true results in no output
Usually, the field has a property that defines the font size that needs to be used, but you are right: if the text doesn't fit the rectangle defined by the form field, the text gets truncated. You can change this by setting the font size to "auto". This way, the font size will decrease if the text doesn't fit. This is explained here: Set AcroField Text Size to Auto
Or you could use a multiline field. See How to get the row count of a multiline field?
If you don't have a form field (which would be strange, because that means that you have to program the coordinates, e.g. PageSize.A4.Height/2, 140), you shouldn't use the showTextAligned() method. If there is no way you can define the location using a form field (but I can't think of any argument why you couldn't), you should take a look at the MovieAds example (taken from my book iText in Action).
I have a method AddParagraph that looks like this:
public bool AddParagraph(Paragraph p, PdfContentByte canvas,
AcroFields.FieldPosition f, bool simulate)
{
ColumnText ct = new ColumnText(canvas);
ct.SetSimpleColumn(
f.position.Left, f.position.GetBottom(2),
f.position.GetRight(2), f.position.Top
);
ct.AddElement(p);
return ColumnText.HasMoreText(ct.Go(simulate));
}
In this example, I create a ColumnText object for which I define a column. I add a paragraph, and I render that paragraph. If simulate is true, I don't add it for real, I add it virtually to see if the paragraph fits. If it fits, I call the method anew for real (with simulate = false).
I repeat this as many time as needed, decreasing the font with 0.2 user units at every try. Only if the font gets smaller than 6pt, I allow iText to truncate the content.
while (AddParagraph(CreateMovieParagraph(movie, size),
canvas, f, true) && size > 6)
{
size -= 0.2f;
}

How to convert a string size into inches in C#

I want to know the printing size of a string in inches. i.e. what will be the length (and width) of string "Test String" when printed on a paper. I can get the length in pixels using the Graphics object and MeasureString method. But don't know how to convert pixels into inches on paper.
I checked this SO question but can't find the solution.
The resolution of a digital graphic is its pixel dimensions.
The size of the printed graphic is dependent of the printer settings.
If you print a picture of 300 x 300
pixels at 300dpi on a printer the
picture becomes 1 x 1 inch. ( 300 / 300 )
If you print a picture of 300 x 300
pixels at 10dpi on a printer the
picture becomes 30 x 30 inch. ( 300 / 10 )
If you print a picture of 300 x 300
pixels at 1000dpi on a printer the
picture becomes 0,3 x 0,3 inch. (300 / 1000 )
However, the exact size depends on the accuracy of the printer (paper feeding, servo of the printer head etc.)
An excellent reference is found here: DPI has NOTHING to do with digital image quality!.
The Dpi of the display has NOTHING to do with this
I know this is an old question, but a quazi solution for it. I have tested my solution a few times and it seems to be accurate but not precise. The basis of this code is directly from an MSDN example and this answer on SO, the only thing I am doing differently is changing the units from points to inches.
MSDN Graphics.MeasureString()
private float GetTextWidth(string fontFace, float fontSize, string text)
{
SizeF size = new SizeF();
Font font = new Font(fontFace, fontSize);
//Using a Bitmap object for frame of reference in order to get to a Graphics object
using (Bitmap b = new Bitmap(1, 1))
{
//Graphics object allows string measurement
using (Graphics g = Graphics.FromImage(b))
{
//change the units to inches
g.PageUnit = GraphicsUnit.Inch;
//Perform the measurement
size = g.MeasureString(text, font);
}
}
//Print to console if you want
//Console.WriteLine(size.Width);
return size.Width;
}
I tested this out with the following inputs:
("Arial", 10, "Page 1 of 1") and I got 0.777972 inches
If you read the Remarks of the MSDN link above you will see that there is a little extra space padded onto that figure. You have to be aware that this happens:
The MeasureString method is designed for use with individual strings
and includes a small amount of extra space before and after the string
to allow for overhanging glyphs.
When I measured this on a piece of paper end to end I got about 0.625, that is a difference of 0.152972 which is essentially ~20% padding. I'm guessing it is distributed on either side evenly.
Also be careful with which Fonts you use - for example Helvetica is not a supported font by default, therefore the Font object will default to Arial if it can't find your font.
This is not possible, unless you know both the printer size and display size in hard numbers.
Say you're printing a full screen handout. On a 30 inch monitor, 1 pixel may be 1 unit large on paper. But on a 7 inch netbook, a pixel will be 2 units large on paper - twice the size, even though they are both one pixel on the electronic display.
Simply put, this measurement is not possible unless you are working with something fixed, like an iPhone's display size (which is well known), or your own hardware whose screen size you have exact measurements to.

iTextSharp self-expanding page width

I'm writing a web application to generate labels. The label printer that I'm targeting utilizes 12mm tape and the customer specified that they want to limit the labels to 3.25". I know that with iTextSharp I can specify the size of the document I want to create, however it appears that I have to specify both a width and height.
Document document = new Document(new iTextSharp.text.Rectangle(234f, 33.84f));
234 is 3.25" converted to points, and 33.84 is 12mm converted to points, so this sets the document to the maximum size allowed. Is there any way to set just the height and let the document auto-expand to the amount of content? With that, is there a way to determine if the expanded width of the document exceeds to maximum allowed by the customer? Thanks in advance for any help you can offer.
No, that is not possible in iText. It's not a particularly good idea anywhere else either. As Redman said, PDF is a print-based format. It's not HTML.
There are some tricks you can do to get around this to some extent:
Create your page with the maximum legal height. 200" * 72dpi = 14400 points.
Add a "generic tag" to your paragraphs.
create a tag event handler that tracks where the bottom of your last paragraph was drawn.
Save the PDF.
Open it again with a PDFStamper
Set the bottom of the page to match the location of that last paragraph. Remember that the bottom left starts off at 0,0, and the top right will be
Save the final PDF
This trick will only work if your total output is less that 200" high. If you go over that, you'll still get a second page, and your "where the bottom is" code had better be prepared for it.
PS: I don't see what's wrong with having a number of 12mm x 3.25" pages... won't that perfectly fit the labels they want printed?
As far as I know, because PDF is a print format, the document width and height must be set.
This is why reporting tools generally default page sizes to the default printer page size.
You best bet to try and calculate the size of the content and create the PDF accordingly.
Depending on the volume and nature of the workflow, you could provide a preview for the customer to check before printing.

Categories

Resources