How to draw Matrix Code with PdfSharp? - vb.net

I need to make a PDF report via PdfSharp. The report must include a QRCode, or data matrix code, but I can't seem to be able to draw it on the page.
The values it's asking for are value as String and length as Integer so here's what I'm doing:
Dim myNewCode As New PdfSharp.Drawing.BarCodes.CodeDataMatrix("1234567890", 10)
Then I try to draw it:
gfx.DrawMatrixCode(myNewCode, myXPoint)
It asks for an XPoint location so I set it to this:
Dim myXPoint As New XPoint(500,500)
Which only needs values for x and y.
It compiles OK but when I try to open the file I get the next error
An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem
My Acrobat version is 11.0.5, and there is no problem opening other PDF files which already contain these kind of codes.

Specify the size to get a correct PDF file:
var myXSize = new XSize(100, 100);
var myNewCode = new PdfSharp.Drawing.BarCodes.CodeDataMatrix("1234567890", 10, myXSize);
var myXPoint = new XPoint(200, 300);
gfx.DrawMatrixCode(myNewCode, myXPoint);
Please note that due to legal reasons, the open source version of PDFsharp does not include the implementation of the Data Matrix Code and shows dummy images instead.

Another option would be to use a 3rd party library (ZXing) to generate the QR Code bitmap and draw it as a bitmap with DrawImage() on the PDF.

Related

How do I use iTextSharp (or iText) to crop and copy a page from one PDF to another

I've written code to do the following:
Take a PDF of a certain page size (e.g., 8.5" x 11")
Create a new PDF with a larger page size (e.g., 17" x 11")
Impose the original PDF onto the new one (e.g., 2-up such that the resulting new PDF has the original PDF side-by-side)
To do this, I use the PdfWriter.GetImportedPage method to get the current page from the original PDF, then use the PdfContentByte.AddTemplate(page, x, y) method to place the original page onto the current page of the new PDF.
My new challenge is that I need to crop the original PDF before adding it to the new PDF. For example, imagine I want to crop 2" off of the original PDF before imposing it. The input PDF would still be 8.5" x 11" and the new PDF would still be 17" x 11", but the two "copies" of the original PDF in the new one would have had 2" removed from its top, right, bottom and left sides.
Hopefully these images can make this clearer. Here's what I have now, doing a 2-up imposition. (This is working swimmingly.)
But here's what I need to do:
I know that I can alter the display of the PDF in a viewer by using the MediaBox or CropBox settings, but those settings aren't respected by AddTemplate. I know that with AddTemplate I can use a transform matrix to position the page or to scale or rotate it, but I don't want to shrink the original PDF, I want to crop it.
Thanks
I found that I can use the BoundingBox of the imported page to crop it prior to adding it to the new PDF (via AddTemplate).
So my code looks something like this:
PdfImportedPage page = writer.GetImportedPage(pageNumber);
// Crop!
page.BoundingBox = new Rectangle(llx, lly, urx, ury);
// Add to new PDF
writer.DirectContent.AddTemplate(page, x, y);
That does the trick!

How to create different file names for every new document in Photoshop?

I have an A4 size document with 6 Photos, while I am trying to cut one photo from the original and create a new document the new file name is created as "Untitled-1".
Since I am using an Action to cut from original and paste it into the new document, every new file is opened with same name as "Untitled-1" and while saving it replaces the existing.
Is there any way to make every new document with sequence number or any different names for each file while create new document in Photoshop?
Or else please suggest if any script is available for the same.
Please find below screenshot for your clear understanding.
thanks in advance.
Full page image
Every new document name
Are you familiar with slices? It's beneath the crop tool - You can slice up your document the way you want it, then Export > Save for web, then each slice will save as an individual file.

Actually cropping a PDF with PDF Clown

My objective is actually cropping a PDF file with PdfClown.
There are a lot of tools/library that allow cropping PDF, changing the PDF cropBox. This permits hiding contents outside a rectangular area, but content is still there, it might be accessed through a PDF parser and PDF size does not change.
On the contrary what I need is creating a new page containing only the contents inside the rectangular area.
So far I've tried scanning contents and selectively cloning them. But I didn't succeed yet. Any suggestions on using PdfClown for that?
I've seen someone is trying something similar with PdfBox Cropping a region from a PDF page with PDFBox not succeeding yet.
A bit late, but maybe it helps someone;
I am sucessfully doing what you are asking for - but with other libraries.
Required libraries : iText 4 or 5 and Ghostscript
Step 1 with pseudo code
Using iText, Create a PDFWRITER instance with a blank Doc. Open a PDFREADER object to the original file you want to crop. Import the Page, get a PDFTemplate Object from the source, set its .boundingBox property to the desired cropbox, wrap the template into an iText Image object and paste it onto the new page at an absolute position.
Dim reader As New PdfReader(sourcefile)
Dim doc As New Document()
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New System.IO.FileStream(outputfilename, System.IO.FileMode.Create))
//get the source page as an Imported Page
Dim page As PdfImportedPage = writer.GetImportedPage(reader, indexOfPageToGet) page
//create PDFTemplate Object at original size from source - see iText in Action book Page 91 for full details
Dim pdftemp As PdfTemplate = page.CreateTemplate(page.Width, page.Height)
//paste the original page onto the template object, see iText documentation what those parameters do (scaling, mirroring)
pdftemp.AddTemplate(page, 1, 0, 0, 1, 0, 0)
//now the critical part - set .boundingBox property on the template. This makes all objects outside the rectangle invisible
pdftemp.boundingBox = {iText Rectangle Structure with new Cropbox}
//template not needed anymore
writer.ReleaseTemplate(pdftemp)
//create an iText IMAGE object as wrapper to the template - with this img object absolute positionion on the final page is much easier
dim img as iTextSharp.Text.Image = Image.GetInstance(pdftemp)
// set img position
img.SetAbsolutePosition(x, y)
//set optional Rotation if needed
img.RotationDegrees = 0
//finally, this adds the actual content to the new document
doc.Add(img)
//cleanup
doc.Close()
reader.Close()
writer.Close()
The output file will visually look cropped. But the objects are still present in the PDF Stream. Filesize will probably remain very little changed yet.
Step 2:
Using Ghostscript and output device pdfwrite, combined with the correct command line parameters you can re-process the PDF from Step 1. This will give you a much smaller PDF. See Ghostscript documentation for the arguments https://www.ghostscript.com/doc/9.52/Use.htm
This steps actually gets rid of objects that are outside the bounding box - the requirement you asked for in your OP, at least for files that I deal with.
Optional Step 3:
Using MUTOOL with the -g option you can clean up unused XREF objects. Your original PDF probably had a lot of Xrefs, which increase filesize. After cropping some of them may not be needed anymore.
https://mupdf.com/docs/manual-mutool-clean.html
PDF Format is a tricky thing, normally I would agree with #Tilman Hausherr, my suggestion may not work for all files and covers the 'almost impossible' scenario, but it works for all cases that I deal with.

Adding a checkbox symbol using OOo API VB.NET and UNO

I've been searching for a solution for this for quite some time now but with no luck.
I have a PDF that I am generating using VB.NET, the OpenOffice API and UNO. I am generating a text document and I need to be able to insert a checkbox in code.
One possible solution is to change the font to Wingdings and just type 'o' but that solution is neither elegant nor very easy to implement given my environment (using company created code for text document creation and manipulation, have a Write command that will write to the document (strings)).
If it's possible to just add the checkbox to a string of text then that would be perfect.
Have you tried to use an image of a checkbox (one checked and the other unchecked) and then inject that into your PDF, like this:
Caveat - I have worked with iTextSharp so the following code is relevant to iTextSharp, but the concept should translate to whatever PDF generating library/framework you are using
Method #1 - Put checkbox image into table cell
Dim imgCheckBoxChecked As Image = Image.GetInstance(HttpContext.Current.Server.MapPath("checkbox_checked.gif"))
Dim imgCheckBoxUnChecked As Image = Image.GetInstance(HttpContext.Current.Server.MapPath("checkbox_unchecked.gif"))
Dim table As Table
Dim cell As Cell
cell = New Cell(New Paragraph("", font))
'' Add checked or unchecked here
cell.AddElement(imgCheckBoxChecked)
cell.AddElement(imgCheckBoxUnChecked)
table.AddCell(cell)
Method #2 - Put checkbox image into the document
'' Add checked or unchecked here
pdfDoc.Add(imgCheckBoxChecked)
pdfDoc.Add(imgCheckBoxUnChecked)
Note: You will obviously need to find or create the .gif images for checked and unchecked.

VB.NET: Preserve image metadata when moved to clipboard as an image

Visual Studio 2010, .NET 4, VB.NET
Hello,
I am writing a little program to convert LaTeX snippets to images which can be pasted into whatever program one can paste images into. It's working alright but the next obvious step is to include the source LaTeX code as a piece of metadata in the image so that the results can be modified without having to retype everything.
I have succeeded in adding a title PropertyItem with the latex encoded as an ASCII byte array as its value (id=800, type=2, value=System.Text.Encoding.ASCII.GetBytes(codestring)). I verify that the PropertyItem is really there before trying to put the image on the clipboard.
Then I do Clipboard.SetImage(myImage). The result is all of the PropertyItems are removed (my title plus anything else that was there)! I check this by doing MsgBox(Clipboard.GetImage.PropertyItems.Count.ToString) which gives zero.
This makes me very sad. Anyone know what's up?
Thanks in advance!
Brian
Update: I have figured out how to move the image onto the clipboard and then back off while preserving the PropertyItems like so:
Format = DataFormats.GetFormat(GetType(Image).FullName)
Dim dataObject As New DataObject
dataObject.SetData(Format.Name, image)
Clipboard.SetDataObject(dataObject)
Dim copiedImage As Image = CType(Clipboard.GetDataObject.GetData(Format.Name), Image)
This way, the copiedImage has the same PropertyItems as the original. However, new problem:
Other programs don't recognize what's on the clipboard as an image anymore, which defeats the whole purpose. I.e., if I put an image on the clipboard this way, when I try pasting into some context that accepts pasted images, nothing happens.
What to do?!
I believe the Windows clipboard image has no metadata. If you change the format of the image to add metadata, it is no longer a clipboard image. If the other programs can accept it, you could copy and paste the image file (instead of the image) to the clipboard, and the metadata will of course be intact when it's read by the target app.
Have you tried Clipboard.SetData or Clipboard.SetDataObject? SetImage only copies the image in bitmap format, so I am not surprised that it strips the property items. You might try:
Clipboard.SetData(DataFormats.EnhancedMetafile, myImage)
or
Clipboard.SetData(DataFormats.MetafilePict, myImage)